import { DataTableColumnConfig } from '@/components/v2/CustomColumn/CustomColumn.vue.d';
import axios, { AxiosInstance, AxiosResponse } from 'axios';

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

export 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 ColumnServices = {
  getColumnSetup: (
    tableId: string,
  ): Promise<AxiosResponse<{ data: DataTableColumnConfig[] }>> => {
    const user = getUser();
    return API().get(`/columns/${user._id}/${tableId}`);
  },

  editColumnSetup: (
    tableId: string,
    body: DataTableColumnConfig[],
  ): Promise<AxiosResponse> => {
    const user = getUser();
    return API().put(`/columns/${user._id}/${tableId}`, { data: body });
  },
};

export default ColumnServices;
