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 { 
  ChangePasswordParams,
  ChangePasswordResponse
} from '../model/types';
import { 
  useMutation,
  UseMutationOptions
} from '@tanstack/react-query';
import { getHeaderUserAgent } from '@/shared/constants/url';

export const userChangePassword = (params: ChangePasswordParams) => {
  let headerOptions = {
    menuId: 46,
    apiType: 'UPDATE'
  };
  let options = {
    headers: {
      'X-User-Agent': getHeaderUserAgent(headerOptions)
    }
  };
  return resultify(
    axios.post<ChangePasswordResponse>(API_URL_USER.changePassword(), params, options),
  );
};

export const useUserChangePasswordMutation = (options?: UseMutationOptions<ChangePasswordResponse, NiceAxiosError, ChangePasswordParams>) => {
  const mutation = useMutation<ChangePasswordResponse, NiceAxiosError, ChangePasswordParams>({
    ...options,
    mutationFn: (params: ChangePasswordParams) => userChangePassword(params),
  });

  return {
    ...mutation,
  };
};
