import { ShortFetchResponse } from '@/components/v2/DataTable/DataTable.vue.d';
import {
  CustomFieldQueryParams,
  CustomFieldTypeParams,
} from '@/dto/customField.dto';
import {
  CustomField,
  DepreciationCategory,
} from '@/types/settingsAttribute.type';
import axios, { AxiosInstance, AxiosResponse } from 'axios';

const API = ({ headers = {}, params = {} } = {}): AxiosInstance => {
  const user = JSON.parse(localStorage.getItem('user') ?? '{}');
  const BASE_URL = import.meta.env.VITE_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 SettingsAttributeServices = {
  getGeneralCustomFields: (): Promise<
    AxiosResponse<ShortFetchResponse<CustomField[]>>
  > => {
    return API({ params: { type: 'General Information' } }).get(
      '/custom-field/used-by-assets',
    );
  },

  getDepreciationCategory: (): Promise<
    AxiosResponse<ShortFetchResponse<DepreciationCategory[]>>
  > => {
    return API().get('/depreciation-category');
  },

  getAvailableAssetNameList: (
    type: CustomFieldTypeParams,
    queryParams?: CustomFieldQueryParams,
  ): Promise<AxiosResponse> => {
    return API({ params: queryParams }).get(
      `/custom-field/${type}/asset-name/available`,
    );
  },
};

export default SettingsAttributeServices;
