import axios, { type AxiosRequestConfig } from "axios";

// použij tohle pro veřejné API nevyžadující autentizaci
export const publicApi = axios.create({
  withCredentials: false, // veřejné API NEPOSÍLÁ cookies
});

// Set baseURL at runtime (using Function to hide from orval's esbuild)
try {
  const getEnv = new Function('return import.meta.env.VITE_BACKEND_URL');
  publicApi.defaults.baseURL = getEnv() || "http://localhost:8000";
} catch {
  publicApi.defaults.baseURL = "http://localhost:8000";
}


// ⬇⬇⬇ TOHLE JE TEN MUTATOR ⬇⬇⬇
export const publicMutator = async <T>(
  config: AxiosRequestConfig
): Promise<T> => {
  const response = await publicApi.request<T>(config);
  return response.data;
};