import axios from 'axios';
import { API_URL_MERCHANT } from '@/shared/api/api-url-merchant';
import { resultify } from '@/shared/lib/resultify';
import { NiceAxiosError } from '@/shared/@types/error';
import { 
  MerchantMidParams,
  MerchantMidResponse
} from '../model/types';
import { 
  useMutation,
  UseMutationOptions
} from '@tanstack/react-query';
import { getHeaderUserAgent } from '@/shared/constants/url';

export const merchantMid = (params: MerchantMidParams) => {
  let headerOptions = {
    menuId: 39,
    apiType: 'SELECT'
  };
  let options = {
    headers: {
      'X-User-Agent': getHeaderUserAgent(headerOptions)
    }
  };
  return resultify(
    axios.post<MerchantMidResponse>(API_URL_MERCHANT.merchantMid(params.mid), params, options),
  );
};

export const useMerchantMidMutation = (options?: UseMutationOptions<MerchantMidResponse, NiceAxiosError, MerchantMidParams>) => {
  const mutation = useMutation<MerchantMidResponse, NiceAxiosError, MerchantMidParams>({
    ...options,
    mutationFn: (params: MerchantMidParams) => merchantMid(params),
  });

  return {
    ...mutation,
  };
};
