import axios, { AxiosInstance, AxiosResponse } from 'axios';

const getBaseURL = (env: string): string | undefined => {
  if (window.Cypress) {
    return Cypress.env('CY_' + env); // When running Cypress Intercept
  } else if (import.meta.env) {
    return import.meta.env['VITE_' + env]; // When running in development mode with Vite
  }

  return process.env['VUE_' + env]; // When building for production with Vue CLI
};

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/damage-repair-ticketing/v2/damage`,
    headers: {
      'Content-type': 'application/json',
      'Authorization': `Bearer ${user.token}`,
      ...headers,
    },
    params,
  });

  return instance;
};

const AssetDamageServices = {
  getDetail: (id: string): Promise<AxiosResponse> => {
    return API().get(`/${id}`);
  },
};

export default AssetDamageServices;
