import axios from 'axios';
import { API_URL } from '@/shared/api/urls';
import { resultify } from '@/shared/lib/resultify';
import { NiceAxiosError } from '@/shared/@types/error';
import { 
  LoginParams,
  LoginResponse
} from '../model/types';
import { 
  useMutation,
  UseMutationOptions
} from '@tanstack/react-query';
import { getHeaderUserAgent } from '@/shared/constants/url';

export const login = (params: LoginParams) => {
  let headerOptions = {
    menuId: 0,
    apiType: 'SELECT'
  };
  let options = {
    headers: {
      'X-User-Agent': getHeaderUserAgent(headerOptions)
    }
  };
  return resultify(
    axios.post<LoginResponse>(API_URL.login(), params, options),
  );
};

export const useLoginMutation = (options?: UseMutationOptions<LoginResponse, NiceAxiosError, LoginParams>) => {
  const mutation = useMutation<LoginResponse, NiceAxiosError, LoginParams>({
    ...options,
    mutationFn: (params: LoginParams) => login(params),
  });

  return {
    ...mutation,
  };
};
