import { ApplicationSettingResponse } from '@/types/globalSettings.type';
import axios, { AxiosInstance, AxiosResponse } from 'axios';

const getUser = (): Record<string, string> =>
  JSON.parse(localStorage.getItem('user') ?? '{}');

const API = ({ headers = {}, params = {} } = {}): AxiosInstance => {
  const user = getUser();

  const BASE_URL = import.meta.env.VITE_APP_TAGSAMURAI_API;

  const instance = axios.create({
    baseURL: `${BASE_URL}/global-settings`,
    headers: {
      'Content-type': 'application/json',
      'Authorization': `Bearer ${user.token}`,
      ...headers,
    },
    params,
  });

  return instance;
};

const GlobalSettingsServices = {
  getApplicationSetting: (): Promise<
    AxiosResponse<ApplicationSettingResponse>
  > => {
    return API().get('/application-setting');
  },
};

export default GlobalSettingsServices;
