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

export const login = (params: LoginParams) => {
  return resultify(
    axios.post<LoginResponse>(API_URL.login(), params),
  );
};

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

  return {
    ...mutation,
  };
};
