import axios from "axios";

const DEFAULT_BASE_URL = "http://localhost:8000";

export const baseURL =
  (typeof import.meta !== "undefined" &&
    typeof import.meta.env !== "undefined" &&
    (import.meta.env as { VITE_API_BASE_URL?: string }).VITE_API_BASE_URL) ||
  DEFAULT_BASE_URL;

export const http = axios.create({
  baseURL,
  headers: {
    "Content-Type": "application/json",
  },
});

export async function request<T>(path: string, init?: { method?: string; data?: unknown; params?: Record<string, unknown> }): Promise<T> {
  const method = init?.method || "GET";
  const response = await http.request<T>({
    url: path,
    method,
    params: init?.params,
    data: init?.data,
  });
  return response.data;
}
