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

import { useWidget } from './useWidget';

import type { MutationKey, UseMutationOptions } from '@tanstack/react-query';

const useVerifyMagicLink = () => {
	const { verifyMagicLink } = useWidget();

	const key = ['login', 'magic_link', 'verify'] as MutationKey;
	const isLoading = useIsMutating(key) > 0;

	const options = {
		mutationKey: key,
		mutationFn: (params) => verifyMagicLink(params),
	} as UseMutationOptions<
		unknown,
		Error,
		WidgetProvider.VerifyMagicLinkProps
	>;

	const mutation = useMutation(options);

	return { ...mutation, isLoading };
};

export default useVerifyMagicLink;
