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

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

export const useLoginWithPassword = (
	_options?: UseMutationOptions<
		unknown,
		unknown,
		WidgetProvider.LoginWithPasswordOptions
	>
) => {
	const { loginWithPassword } = useWidget();

	const mutationKey: MutationKey = ['login', 'password'];
	const isLoading = useIsMutating(mutationKey) > 0;

	const options = {
		mutationKey,
		mutationFn: (params) => loginWithPassword(params),
		..._options,
	} as UseMutationOptions<
		unknown,
		unknown,
		WidgetProvider.LoginWithPasswordOptions
	>;

	const mutation = useMutation(options);

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