import { getBaseURL } from '@/utils';
import axios, { AxiosInstance, AxiosResponse } from 'axios';

const API = ({ headers = {}, params = {} } = {}): AxiosInstance => {
  const user = JSON.parse(localStorage.getItem('user') as string) ?? {};
  const BASE_URL = getBaseURL('APP_TAGSAMURAI_API');

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

  return instance;
};

const DataLockService = {
  getDataLock: (): Promise<AxiosResponse> => {
    return API().get('/data-lock');
  },
  postDataLock: (): Promise<AxiosResponse> => {
    return API().post('/data-lock');
  },
  deleteDataLock: (): Promise<AxiosResponse> => {
    return API().delete('/data-lock', { fetchOptions: { keepalive: true } });
  },
};

export default DataLockService;
