'use client'
import { useState, useCallback, useEffect } from 'react';
import '@dynamic-labs/iconic';
import '@dynamic-labs/wallet-connector-core';
import 'react/jsx-runtime';
import '../../ViewContext/ViewContext.js';
import { logger } from '../../../shared/logger.js';
import '@dynamic-labs/wallet-book';
import '@dynamic-labs/utils';
import '../../../utils/constants/colors.js';
import '../../../utils/constants/values.js';
import '@dynamic-labs/sdk-api-core';
import '../../../shared/consts/index.js';
import { isAuthenticatedWithAWallet } from '../../../utils/functions/isAuthenticatedWithAWallet/isAuthenticatedWithAWallet.js';
import '@dynamic-labs-sdk/client/core';
import 'eventemitter3';
import '@dynamic-labs-sdk/client';
import '../../../config/ApiEndpoint.js';
import '../../../../../_virtual/_tslib.js';
import '@dynamic-labs/multi-wallet';
import 'react-international-phone';
import '../../../store/state/nonce/nonce.js';
import '@dynamic-labs-wallet/browser-wallet-client';
import '@dynamic-labs/locale';
import '../../../store/state/dynamicContextProps/dynamicContextProps.js';
import '../../../store/state/primaryWalletId/primaryWalletId.js';
import '../../../store/state/connectedWalletsInfo/connectedWalletsInfo.js';
import '../../../utils/functions/getWaasAddressTypeLabel/getWaasAddressTypeLabel.js';
import '../../../events/dynamicEvents.js';
import { useProjectSettings } from '../../../client/extension/projectSettings/useProjectSettings/useProjectSettings.js';
import { isExpectingEmbeddedWallet } from './isExpectingEmbeddedWallet/isExpectingEmbeddedWallet.js';

const useCustomerCallbacks = ({ callbacks: { onAuthSuccess }, handleLogOut, primaryWallet, user, isAuthenticated, }) => {
    const [callbackQueue, setCallbackQueue] = useState([]);
    const handleNextCallback = useCallback((next, user) => {
        if ((next === null || next === void 0 ? void 0 : next.callback) !== 'authSuccess')
            return;
        try {
            onAuthSuccess === null || onAuthSuccess === void 0 ? void 0 : onAuthSuccess({
                handleLogOut,
                isAuthenticated,
                primaryWallet,
                user,
            });
        }
        catch (e) {
            logger.error('Error calling onAuthSuccess: ', e);
        }
    }, [handleLogOut, isAuthenticated, onAuthSuccess, primaryWallet]);
    const projectSettings = useProjectSettings();
    useEffect(() => {
        if (!callbackQueue.length || !user || !projectSettings)
            return;
        // checks if user logged in with a wallet, so we need to wait
        // for the primary wallet state to be set
        // if user logged in with email without embedded wallet, then
        // there is no wallet so we can go ahead and run the callback
        if (isAuthenticatedWithAWallet(user) && !primaryWallet) {
            return;
        }
        // For embedded wallets, when auto creating wallets, it will take a while
        // before the wallet is available, so we need to wait
        if (isExpectingEmbeddedWallet(projectSettings) && !primaryWallet) {
            return;
        }
        handleNextCallback(callbackQueue[0], user);
        setCallbackQueue((prev) => {
            const [resolvedCallback, ...updatedQueue] = prev;
            if (resolvedCallback) {
                logger.debug('Resolved callback: ', resolvedCallback.callback);
                logger.debug('Callbacks in queue: ', updatedQueue.map((c) => c.callback));
            }
            return updatedQueue;
        });
    }, [callbackQueue, handleNextCallback, primaryWallet, projectSettings, user]);
    const setCallback = useCallback((callback, params) => {
        setCallbackQueue((prev) => [...prev, { callback, params }]);
    }, []);
    return { setCallback };
};

export { useCustomerCallbacks };
