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

const DynamicWidgetContext = createContext(undefined);
const DynamicWidgetContextProvider = ({ children, }) => {
    const widgetRef = useRef(null);
    const inlineControlsRef = useRef(null);
    const availableWalletsContainerRef = useRef(null);
    const { setShowDynamicUserProfile, showDynamicUserProfile, multiWallet } = useInternalDynamicContext();
    const isKYCEnabled = useKYCFlag();
    const { view: dynamicWidgetView, setView, goToInitialView: goToInitialDynamicWidgetView, } = useViewController(isKYCEnabled && !multiWallet ? 'profile' : 'wallets');
    const [headerAlert, _setHeaderAlert] = useState(null);
    const headerAlertTimeoutRef = useRef(null);
    const clearHeaderAlert = useCallback(() => {
        _setHeaderAlert(null);
        if (headerAlertTimeoutRef.current) {
            clearTimeout(headerAlertTimeoutRef.current);
            headerAlertTimeoutRef.current = null;
        }
    }, []);
    const setHeaderAlert = 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] = useState({});
    const setDynamicWidgetView = useCallback((view, props) => {
        setView(view);
        setViewProps(props || {});
        clearHeaderAlert();
    }, [clearHeaderAlert, setView]);
    const goToProfileView = useCallback(() => {
        if (isKYCEnabled)
            return setDynamicWidgetView('profile');
        setDynamicWidgetView('wallets');
    }, [isKYCEnabled, setDynamicWidgetView]);
    const overridenWidgetView = 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]);
    useInternalDynamicEvents('logout', goToInitialDynamicWidgetView);
    useInternalDynamicEvents('userProfileClose', () => {
        // Use a timeout to avoid flicker
        setTimeout(goToInitialDynamicWidgetView, 20);
    });
    const value = 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 (jsx(DynamicWidgetContext.Provider, { value: value, children: children }));
};
const useWidgetContext = () => {
    const context = useContext(DynamicWidgetContext);
    if (context === undefined) {
        throw new Error('usage of useWidgetContext not wrapped in `DynamicWidgetContextProvider`.');
    }
    return context;
};

export { DynamicWidgetContext, DynamicWidgetContextProvider, useWidgetContext };
