'use client'
import { useState, useCallback } from 'react';
import { dynamicEvents } from '../../../../events/dynamicEvents.js';
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 { useErrorContext } from '../../../ErrorContext/ErrorContext.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-sdk/client/core';
import 'eventemitter3';
import '@dynamic-labs-sdk/client';
import '../../../../config/ApiEndpoint.js';
import '@dynamic-labs/locale';
import '../../../../store/state/dynamicContextProps/dynamicContextProps.js';
import '../../../../store/state/primaryWalletId/primaryWalletId.js';
import { hasAllRequiredWalletsConnected } from '../../../../utils/functions/hasAllRequiredWalletsConnected/hasAllRequiredWalletsConnected.js';
import '../../../../utils/functions/getWaasAddressTypeLabel/getWaasAddressTypeLabel.js';

/**
 * 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] = useState(false);
    const { setError } = useErrorContext();
    const setShowAuthFlow = 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();
        logger.logVerboseTroubleshootingMessage('setShowAuthFlow', {
            areAllRequiredWalletsConnected,
            clearErrors,
            emitCancelAuth,
            ignoreIfIsEmbeddedWidget,
            initializeWalletConnect,
            performMultiWalletChecks,
        });
        if (!showAuthFlow && value) {
            dynamicEvents.emit('authFlowOpen');
        }
        else if (showAuthFlow && !value) {
            dynamicEvents.emit('authFlowClose');
        }
        if (value) {
            if (ignoreIfIsEmbeddedWidget && isRenderingEmbeddedAuthFlow)
                return;
            if (performMultiWalletChecks && areAllRequiredWalletsConnected) {
                if (!isMultiWalletEnabled) {
                    return setShowDynamicUserProfile(true);
                }
                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.emit('authFlowCancelled');
        if (clearErrors)
            setError(undefined);
        _setShowAuthFlow(value);
    }, [
        isMultiWalletEnabled,
        isRenderingEmbeddedAuthFlow,
        setError,
        setShowDynamicUserProfile,
        showAuthFlow,
    ]);
    return [showAuthFlow, setShowAuthFlow];
};

export { useShowAuthFlow };
