import axios, { AxiosInstance } from "axios";
import { createApiClient } from "../../auth/src";

/**
 * We expose a singleton-like getter/setter for the API clients
 */
let _api: AxiosInstance | null = null;
let _auth: AxiosInstance | null = null;

export const api = {
  get: (...args: Parameters<AxiosInstance["get"]>) => {
    if (!_api) throw new Error("API client not initialized");
    return _api.get(...args);
  },
  post: (...args: Parameters<AxiosInstance["post"]>) => {
    if (!_api) throw new Error("API client not initialized");
    return _api.post(...args);
  },
  put: (...args: Parameters<AxiosInstance["put"]>) => {
    if (!_api) throw new Error("API client not initialized");
    return _api.put(...args);
  },
  delete: (...args: Parameters<AxiosInstance["delete"]>) => {
    if (!_api) throw new Error("API client not initialized");
    return _api.delete(...args);
  },
};

export const auth = {
  post: (...args: Parameters<AxiosInstance["post"]>) => {
    if (!_auth) throw new Error("Auth client not initialized");
    return _auth.post(...args);
  },
  get: (...args: Parameters<AxiosInstance["get"]>) => {
    if (!_auth) throw new Error("Auth client not initialized");
    return _auth.get(...args);
  },
};

export function initializeApiClients(baseUrl: string, authBaseUrl: string) {
  _api = createApiClient(baseUrl);
  _auth = createApiClient(authBaseUrl);
}
