import axios from 'axios';
import { API_URL_ADDITIONAL_SERVICE } from '@/shared/api/api-url-additional-service';
import { resultify } from '@/shared/lib/resultify';
import { NiceAxiosError } from '@/shared/@types/error';
import {
  ExtensionListParams,
  ExtensionListResponse
} from '../model/types';
import {
  useMutation,
  UseMutationOptions
} from '@tanstack/react-query';
import { getHeaderUserAgent } from '@/shared/constants/url';

export const extensionList = (params: ExtensionListParams) => {
  let headerOptions = {
    menuId: 51,
    apiType: 'SELECT'
  };
  let options = {
    headers: {
      'X-User-Agent': getHeaderUserAgent(headerOptions)
    }
  };
  return resultify(
    axios.post<ExtensionListResponse>(API_URL_ADDITIONAL_SERVICE.extensionList(), params, options),
  );
};

export const useExtensionListMutation = (options?: UseMutationOptions<ExtensionListResponse, NiceAxiosError, ExtensionListParams>) => {
  const mutation = useMutation<ExtensionListResponse, NiceAxiosError, ExtensionListParams>({
    ...options,
    mutationFn: (params: ExtensionListParams) => extensionList(params),
  });

  return {
    ...mutation,
  };
};
