'use client'
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var React = require('react');
require('@dynamic-labs/iconic');
require('@dynamic-labs/wallet-connector-core');
require('react/jsx-runtime');
require('../../ViewContext/ViewContext.cjs');
var logger = require('../../../shared/logger.cjs');
require('@dynamic-labs/wallet-book');
require('@dynamic-labs/utils');
require('../../../utils/constants/colors.cjs');
require('../../../utils/constants/values.cjs');
require('@dynamic-labs/sdk-api-core');
require('../../../shared/consts/index.cjs');
var isAuthenticatedWithAWallet = require('../../../utils/functions/isAuthenticatedWithAWallet/isAuthenticatedWithAWallet.cjs');
require('@dynamic-labs-sdk/client/core');
require('eventemitter3');
require('@dynamic-labs-sdk/client');
require('../../../config/ApiEndpoint.cjs');
require('../../../../../_virtual/_tslib.cjs');
require('@dynamic-labs/multi-wallet');
require('react-international-phone');
require('../../../store/state/nonce/nonce.cjs');
require('@dynamic-labs-wallet/browser-wallet-client');
require('@dynamic-labs/locale');
require('../../../store/state/dynamicContextProps/dynamicContextProps.cjs');
require('../../../store/state/primaryWalletId/primaryWalletId.cjs');
require('../../../store/state/connectedWalletsInfo/connectedWalletsInfo.cjs');
require('../../../utils/functions/getWaasAddressTypeLabel/getWaasAddressTypeLabel.cjs');
require('../../../events/dynamicEvents.cjs');
var useProjectSettings = require('../../../client/extension/projectSettings/useProjectSettings/useProjectSettings.cjs');
var isExpectingEmbeddedWallet = require('./isExpectingEmbeddedWallet/isExpectingEmbeddedWallet.cjs');

const useCustomerCallbacks = ({ callbacks: { onAuthSuccess }, handleLogOut, primaryWallet, user, isAuthenticated, }) => {
    const [callbackQueue, setCallbackQueue] = React.useState([]);
    const handleNextCallback = React.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.logger.error('Error calling onAuthSuccess: ', e);
        }
    }, [handleLogOut, isAuthenticated, onAuthSuccess, primaryWallet]);
    const projectSettings = useProjectSettings.useProjectSettings();
    React.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.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.isExpectingEmbeddedWallet(projectSettings) && !primaryWallet) {
            return;
        }
        handleNextCallback(callbackQueue[0], user);
        setCallbackQueue((prev) => {
            const [resolvedCallback, ...updatedQueue] = prev;
            if (resolvedCallback) {
                logger.logger.debug('Resolved callback: ', resolvedCallback.callback);
                logger.logger.debug('Callbacks in queue: ', updatedQueue.map((c) => c.callback));
            }
            return updatedQueue;
        });
    }, [callbackQueue, handleNextCallback, primaryWallet, projectSettings, user]);
    const setCallback = React.useCallback((callback, params) => {
        setCallbackQueue((prev) => [...prev, { callback, params }]);
    }, []);
    return { setCallback };
};

exports.useCustomerCallbacks = useCustomerCallbacks;
