'use client'
'use strict';

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

var React = require('react');
var dynamicEvents = require('../../../../events/dynamicEvents.cjs');
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 ErrorContext = require('../../../ErrorContext/ErrorContext.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-sdk/client/core');
require('eventemitter3');
require('@dynamic-labs-sdk/client');
require('../../../../config/ApiEndpoint.cjs');
require('@dynamic-labs/locale');
require('../../../../store/state/dynamicContextProps/dynamicContextProps.cjs');
require('../../../../store/state/primaryWalletId/primaryWalletId.cjs');
var hasAllRequiredWalletsConnected = require('../../../../utils/functions/hasAllRequiredWalletsConnected/hasAllRequiredWalletsConnected.cjs');
require('../../../../utils/functions/getWaasAddressTypeLabel/getWaasAddressTypeLabel.cjs');

/**
 * There are a multitude of scenarios where we want to ignore or insert functionality to calls
 * to setShowAuthFlow. This hook wraps it with an options parameter to allow encapsulating these
 * different scenarios and makes it especially easy to customize exactly which version of this method
 * is being passed to useDynamicContext.
 */
const useShowAuthFlow = ({ isRenderingEmbeddedAuthFlow, isMultiWalletEnabled, setShowDynamicUserProfile, }) => {
    const [showAuthFlow, _setShowAuthFlow] = React.useState(false);
    const { setError } = ErrorContext.useErrorContext();
    const setShowAuthFlow = React.useCallback((value, options = {}) => {
        const { emitCancelAuth, ignoreIfIsEmbeddedWidget, initializeWalletConnect, performMultiWalletChecks, clearErrors, } = Object.assign({ clearErrors: true, emitCancelAuth: false, ignoreIfIsEmbeddedWidget: true, initializeWalletConnect: true, performMultiWalletChecks: false }, options);
        const areAllRequiredWalletsConnected = hasAllRequiredWalletsConnected.hasAllRequiredWalletsConnected();
        logger.logger.logVerboseTroubleshootingMessage('setShowAuthFlow', {
            areAllRequiredWalletsConnected,
            clearErrors,
            emitCancelAuth,
            ignoreIfIsEmbeddedWidget,
            initializeWalletConnect,
            performMultiWalletChecks,
        });
        if (!showAuthFlow && value) {
            dynamicEvents.dynamicEvents.emit('authFlowOpen');
        }
        else if (showAuthFlow && !value) {
            dynamicEvents.dynamicEvents.emit('authFlowClose');
        }
        if (value) {
            if (ignoreIfIsEmbeddedWidget && isRenderingEmbeddedAuthFlow)
                return;
            if (performMultiWalletChecks && areAllRequiredWalletsConnected) {
                if (!isMultiWalletEnabled) {
                    return setShowDynamicUserProfile(true);
                }
                logger.logger.warn('Use setShowLinkNewWalletModal to render the wallet list instead. For more information, see: https://docs.dynamic.xyz/react-sdk/hooks/usedynamiccontext');
                return;
            }
        }
        else if (emitCancelAuth && showAuthFlow)
            dynamicEvents.dynamicEvents.emit('authFlowCancelled');
        if (clearErrors)
            setError(undefined);
        _setShowAuthFlow(value);
    }, [
        isMultiWalletEnabled,
        isRenderingEmbeddedAuthFlow,
        setError,
        setShowDynamicUserProfile,
        showAuthFlow,
    ]);
    return [showAuthFlow, setShowAuthFlow];
};

exports.useShowAuthFlow = useShowAuthFlow;
