import axios, { AxiosInstance } from "axios";
const baseURL = process.env.NEXT_PUBLIC_API_BASE_URL;

export function getAxiosClient(access_token: string | null | undefined) {
  const client = axios.create({
    baseURL: baseURL,
  });

  client.interceptors.request.use((config) => {
    config.headers["Content-Type"] = "application/json";
    config.headers["Accept"] = "application/json";
    if (access_token) {
      config.headers["Authorization"] = `Bearer ${access_token}`;
    }
    return config;
  });
  return client;
}

export async function makeGetRequest(client: AxiosInstance, endpoint: string) {
  try {
    const response = await client.get(endpoint);
    return response.data;
  } catch (error: any) {
    return {
      ...error?.response?.data,
      status_code: error?.response?.status,
    };
  }
}
