import { useMutation, useQueryClient } from '@tanstack/react-query';

import { usersApi } from '@/backend/users/api';
import { IUserProfile, IUserSignupData } from '@/models/user';

import { DataCallback } from '../apiTransport';

export const useSignup = () => {
  const queryClient = useQueryClient();
  const mutation = useMutation({
    mutationKey: ['signup'],
    mutationFn: usersApi.signup,
    onSuccess: () => queryClient.invalidateQueries({ queryKey: [usersApi.baseKey] })
  });
  return {
    signup: (data: IUserSignupData, onSuccess?: DataCallback<IUserProfile>) =>
      mutation.mutate(data, { onSuccess: response => onSuccess?.(response.data as IUserProfile) }),
    isPending: mutation.isPending,
    error: mutation.error,
    reset: mutation.reset
  };
};
