import axios from 'axios';
import { API_URL_USER } from '@/shared/api/api-url-user';
import { resultify } from '@/shared/lib/resultify';
import { NiceAxiosError } from '@/shared/@types/error';
import { 
  UserFindParams,
  UserFindResponse
} from '../model/types';
import { 
  useMutation,
  UseMutationOptions
} from '@tanstack/react-query';
import { getHeaderUserAgent } from '@/shared/constants/url';

export const userFind = (params: UserFindParams) => {
  let headerOptions = {
    menuId: 45,
    apiType: 'SELECT'
  };
  let options = {
    headers: {
      'X-User-Agent': getHeaderUserAgent(headerOptions)
    }
  };
  return resultify(
    axios.post<UserFindResponse>(API_URL_USER.findUser(), params, options),
  );
};

export const useUserFindMutation = (options?: UseMutationOptions<UserFindResponse, NiceAxiosError, UserFindParams>) => {
  const mutation = useMutation<UserFindResponse, NiceAxiosError, UserFindParams>({
    ...options,
    mutationFn: (params: UserFindParams) => userFind(params),
  });

  return {
    ...mutation,
  };
};
