import { useMutation } from '@apollo/client';
import RELEASE_ABACUS_ACCOUNT_PAYEE from './release-abacus-account-payee.gql';
import type {
    ReleaseAbacusAccountPayeeMutationVariables,
    ReleaseAbacusAccountPayeeMutation,
} from './__generated__/release-abacus-account-payee';
import type { FetchResult, MutationFunctionOptions } from '@apollo/client';
export interface UseCreatePayoneerRegistrationLinkHookReturn {
    releasePayeeMutationResponse: {
        accountPayeeId?: string | null;
    } | null;
    loading: boolean;
    error?: Error | string;
    releaseAccountPayee?: (
        options?: MutationFunctionOptions<
            ReleaseAbacusAccountPayeeMutation,
            ReleaseAbacusAccountPayeeMutationVariables
        >
    ) => Promise<FetchResult<ReleaseAbacusAccountPayeeMutation>>;
}

export const useReleaseAbacusAccountPayeeHook =
    (): UseCreatePayoneerRegistrationLinkHookReturn => {
        const [releaseAccountPayee, state] = useMutation<
            ReleaseAbacusAccountPayeeMutation,
            ReleaseAbacusAccountPayeeMutationVariables
        >(RELEASE_ABACUS_ACCOUNT_PAYEE);

        const { data, error, loading } = state;

        if (loading) {
            return {
                releasePayeeMutationResponse: null,
                loading,
            };
        }

        if (error) {
            return {
                releasePayeeMutationResponse: null,
                releaseAccountPayee,
                loading,
                error,
            };
        }

        const hookData = {
            releasePayeeMutationResponse: {
                accountPayeeId: data?.releaseAbacusAccountPayee.accountPayeeId,
            },
            releaseAccountPayee,
            loading,
        };

        return hookData;
    };
