'use client'
'use strict';

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

var jsxRuntime = require('react/jsx-runtime');
var React = require('react');
require('../../../context/DynamicContext/DynamicContext.cjs');
require('../../../store/state/loadingAndLifecycle/loadingAndLifecycle.cjs');
require('@dynamic-labs/iconic');
require('@dynamic-labs/wallet-connector-core');
require('../../../context/ViewContext/ViewContext.cjs');
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 useDynamicEvents = require('../../../utils/hooks/events/useDynamicEvents/useDynamicEvents.cjs');
require('../../../../../_virtual/_tslib.cjs');
require('../../../context/CaptchaContext/CaptchaContext.cjs');
require('../../../context/ErrorContext/ErrorContext.cjs');
require('@dynamic-labs/multi-wallet');
require('react-international-phone');
require('../../../store/state/nonce/nonce.cjs');
require('@dynamic-labs-sdk/client/core');
require('../../../client/client.cjs');
require('@dynamic-labs-sdk/client');
require('../../../config/ApiEndpoint.cjs');
require('../../../events/dynamicEvents.cjs');
require('@dynamic-labs/locale');
require('../../../store/state/dynamicContextProps/dynamicContextProps.cjs');
require('../../../store/state/primaryWalletId/primaryWalletId.cjs');
require('../../../store/state/connectedWalletsInfo/connectedWalletsInfo.cjs');
require('../../../context/AccessDeniedContext/AccessDeniedContext.cjs');
require('../../../context/AccountExistsContext/AccountExistsContext.cjs');
require('../../../context/UserWalletsContext/UserWalletsContext.cjs');
require('../../../store/state/authMode/authMode.cjs');
require('../../../context/VerificationContext/VerificationContext.cjs');
require('react-dom');
require('../../../utils/functions/compareChains/compareChains.cjs');
require('../../../views/Passkey/utils/findPrimaryEmbeddedChain/findPrimaryEmbeddedChain.cjs');
require('../../../context/ThemeContext/ThemeContext.cjs');
require('../../../utils/hooks/useUserUpdateRequest/useUpdateUser/userFieldsSchema.cjs');
var useKYCFlag = require('../../../utils/hooks/useKYCFlag/useKYCFlag.cjs');
require('bs58');
require('@dynamic-labs/types');
require('../../../context/SocialRedirectContext/SocialRedirectContext.cjs');
require('../../../context/LoadingContext/LoadingContext.cjs');
require('../../../context/WalletContext/WalletContext.cjs');
require('../../../utils/hooks/useEmbeddedWallet/useSecureEnclaveEmbeddedWallet/constants.cjs');
require('yup');
require('../../../context/MockContext/MockContext.cjs');
require('../../../views/CollectUserDataView/useFields.cjs');
require('../../../context/FieldsStateContext/FieldsStateContext.cjs');
require('../../../context/UserFieldEditorContext/UserFieldEditorContext.cjs');
require('@dynamic-labs/rpc-providers');
require('../../../store/state/walletOptions/walletOptions.cjs');
require('react-i18next');
require('../../../components/Accordion/components/AccordionItem/AccordionItem.cjs');
require('../../../components/Alert/Alert.cjs');
require('../../../components/ShadowDOM/ShadowDOM.cjs');
require('../../../components/IconButton/IconButton.cjs');
require('../../../components/InlineWidget/InlineWidget.cjs');
require('../../../components/Input/Input.cjs');
require('../../../components/IsBrowser/IsBrowser.cjs');
require('../../../components/MenuList/Dropdown/Dropdown.cjs');
require('../../../components/OverlayCard/OverlayCard.cjs');
require('../../../components/Transition/ZoomTransition/ZoomTransition.cjs');
require('../../../components/Transition/SlideInUpTransition/SlideInUpTransition.cjs');
require('../../../components/Transition/OpacityTransition/OpacityTransition.cjs');
require('../../../components/PasskeyCreatedSuccessBanner/PasskeyCreatedSuccessBanner.cjs');
require('../../../components/Popper/Popper/Popper.cjs');
require('../../../components/Popper/PopperContext/PopperContext.cjs');
require('react-focus-lock');
require('qrcode');
var useViewController = require('../hooks/useViewController/useViewController.cjs');
require('formik');
require('../../../utils/hooks/useSubdomainCheck/useSubdomainCheck.cjs');
require('../../../context/WalletGroupContext/WalletGroupContext.cjs');
require('../../../context/IpConfigurationContext/IpConfigurationContext.cjs');
require('../../../context/ConnectWithOtpContext/ConnectWithOtpContext.cjs');
require('../../DynamicBridgeWidget/views/WalletsView/components/SecondaryWallets/SecondaryWallets.cjs');
require('@hcaptcha/react-hcaptcha');
require('../helpers/convertExchangeKeyAndProviderEnum.cjs');
require('../../../views/ExchangeWhitelistWarning/ExchangeWhitelistWarning.cjs');
require('../../../context/ErrorContext/hooks/useErrorText/useErrorText.cjs');
require('../../../context/FooterAnimationContext/index.cjs');
require('../../../views/MfaChooseDeviceView/useGetMfaOptions/useGetMfaOptions.cjs');
require('../../../context/PasskeyContext/PasskeyContext.cjs');
require('../../../context/OnrampContext/OnrampContext.cjs');
require('../../../store/state/sendBalances.cjs');
require('../../../store/state/connectorsInitializing/connectorsInitializing.cjs');
require('../../../components/OverlayCardBase/OverlayCardTarget/OverlayCardTarget.cjs');
require('../components/DynamicWidgetHeader/DynamicWidgetHeader.cjs');
require('../../../views/TransactionConfirmationView/TransactionConfirmationView.cjs');
require('../components/PasskeyCard/PasskeyCard.cjs');
require('../views/CryptoComOnramp/CryptoComOnramp.cjs');
require('../../../../index.cjs');
require('../views/ReceiveWalletFunds/ReceiveWalletFunds.cjs');
require('../../../store/state/tokenBalances.cjs');
require('../../../store/state/multichainBalances.cjs');
require('../../../shared/utils/functions/getInitialUrl/getInitialUrl.cjs');
var useInternalDynamicContext = require('../../../context/DynamicContext/useDynamicContext/useInternalDynamicContext/useInternalDynamicContext.cjs');

const DynamicWidgetContext = React.createContext(undefined);
const DynamicWidgetContextProvider = ({ children, }) => {
    const widgetRef = React.useRef(null);
    const inlineControlsRef = React.useRef(null);
    const availableWalletsContainerRef = React.useRef(null);
    const { setShowDynamicUserProfile, showDynamicUserProfile, multiWallet } = useInternalDynamicContext.useInternalDynamicContext();
    const isKYCEnabled = useKYCFlag.useKYCFlag();
    const { view: dynamicWidgetView, setView, goToInitialView: goToInitialDynamicWidgetView, } = useViewController.useViewController(isKYCEnabled && !multiWallet ? 'profile' : 'wallets');
    const [headerAlert, _setHeaderAlert] = React.useState(null);
    const headerAlertTimeoutRef = React.useRef(null);
    const clearHeaderAlert = React.useCallback(() => {
        _setHeaderAlert(null);
        if (headerAlertTimeoutRef.current) {
            clearTimeout(headerAlertTimeoutRef.current);
            headerAlertTimeoutRef.current = null;
        }
    }, []);
    const setHeaderAlert = React.useCallback((alert) => {
        var _a;
        const timeout = (_a = alert === null || alert === void 0 ? void 0 : alert.timeout) !== null && _a !== void 0 ? _a : 10000;
        _setHeaderAlert(alert);
        headerAlertTimeoutRef.current = setTimeout(clearHeaderAlert, timeout);
    }, [clearHeaderAlert]);
    const [viewProps, setViewProps] = React.useState({});
    const setDynamicWidgetView = React.useCallback((view, props) => {
        setView(view);
        setViewProps(props || {});
        clearHeaderAlert();
    }, [clearHeaderAlert, setView]);
    const goToProfileView = React.useCallback(() => {
        if (isKYCEnabled)
            return setDynamicWidgetView('profile');
        setDynamicWidgetView('wallets');
    }, [isKYCEnabled, setDynamicWidgetView]);
    const overridenWidgetView = React.useMemo(() => {
        if (dynamicWidgetView === 'manage-passkeys' ||
            dynamicWidgetView === 'manage-totp-mfa' ||
            dynamicWidgetView === 'manage-passkeys-mfa' ||
            dynamicWidgetView === 'account-and-security-settings')
            return 'settings';
        if (!isKYCEnabled &&
            !multiWallet &&
            dynamicWidgetView !== 'settings' &&
            dynamicWidgetView !== 'profile')
            return 'wallets';
        if (dynamicWidgetView === 'edit-profile')
            return 'profile';
        if (dynamicWidgetView === 'global-wallet')
            return 'wallets';
        return dynamicWidgetView;
    }, [dynamicWidgetView, isKYCEnabled, multiWallet]);
    useDynamicEvents.useInternalDynamicEvents('logout', goToInitialDynamicWidgetView);
    useDynamicEvents.useInternalDynamicEvents('userProfileClose', () => {
        // Use a timeout to avoid flicker
        setTimeout(goToInitialDynamicWidgetView, 20);
    });
    const value = React.useMemo(() => ({
        availableWalletsContainerRef,
        dynamicWidgetView,
        goToInitialDynamicWidgetView,
        goToProfileView,
        headerAlert,
        inlineControlsRef,
        isOpen: showDynamicUserProfile,
        overridenWidgetView,
        setDynamicWidgetView,
        setHeaderAlert,
        setIsOpen: setShowDynamicUserProfile,
        viewProps,
        widgetRef,
    }), [
        dynamicWidgetView,
        goToInitialDynamicWidgetView,
        goToProfileView,
        showDynamicUserProfile,
        setDynamicWidgetView,
        setShowDynamicUserProfile,
        overridenWidgetView,
        viewProps,
        headerAlert,
        setHeaderAlert,
    ]);
    return (jsxRuntime.jsx(DynamicWidgetContext.Provider, { value: value, children: children }));
};
const useWidgetContext = () => {
    const context = React.useContext(DynamicWidgetContext);
    if (context === undefined) {
        throw new Error('usage of useWidgetContext not wrapped in `DynamicWidgetContextProvider`.');
    }
    return context;
};

exports.DynamicWidgetContext = DynamicWidgetContext;
exports.DynamicWidgetContextProvider = DynamicWidgetContextProvider;
exports.useWidgetContext = useWidgetContext;
