'use client'
import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { ModalHeader } from '../../../components/ModalHeader/ModalHeader.js';
import { Typography } from '../../../components/Typography/Typography.js';
import { TypographyButton } from '../../../components/TypographyButton/TypographyButton.js';
import '../../../context/DynamicContext/DynamicContext.js';
import '../../../store/state/loadingAndLifecycle/loadingAndLifecycle.js';
import '@dynamic-labs/iconic';
import '@dynamic-labs/wallet-connector-core';
import { ReactComponent as SvgMultipleAccountSecurity } from '../../../shared/assets/multiple-account-security.js';
import { ReactComponent as SvgPasskeyAccountSecurity } from '../../../shared/assets/passkey-account-security.js';
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 '../../../events/dynamicEvents.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 { getEnabledEmbeddedWalletsAuthenticators } from '../../../utils/functions/getEnabledEmbeddedWalletsAuthenticators/getEnabledEmbeddedWalletsAuthenticators.js';
import '@dynamic-labs-sdk/client/core';
import '../../../client/client.js';
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 '../../../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 '../utils/findPrimaryEmbeddedChain/findPrimaryEmbeddedChain.js';
import '../../../context/ThemeContext/ThemeContext.js';
import { useSmartWallets } from '../../../utils/hooks/useSmartWallets/useSmartWallets.js';
import '../../../utils/hooks/useUserUpdateRequest/useUpdateUser/userFieldsSchema.js';
import { usePasskeyRecovery } from '../../../utils/hooks/usePasskeyRecovery/usePasskeyRecovery.js';
import 'bs58';
import '@dynamic-labs/types';
import '../../../context/SocialRedirectContext/SocialRedirectContext.js';
import '../../../context/LoadingContext/LoadingContext.js';
import '../../../context/WalletContext/WalletContext.js';
import { useIsTurnkeyWallet } from '../../../utils/hooks/useIsTurnkeyWallet/useIsTurnkeyWallet.js';
import '../../../utils/hooks/useEmbeddedWallet/useSecureEnclaveEmbeddedWallet/constants.js';
import 'yup';
import '../../../context/MockContext/MockContext.js';
import '../../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 '../../../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 { SupportLinks } from '../../../components/SupportLinks/SupportLinks.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 'formik';
import '../../../utils/hooks/useSubdomainCheck/useSubdomainCheck.js';
import '../../../context/WalletGroupContext/WalletGroupContext.js';
import '../../../context/IpConfigurationContext/IpConfigurationContext.js';
import '../../../context/ConnectWithOtpContext/ConnectWithOtpContext.js';
import '../../../widgets/DynamicBridgeWidget/views/WalletsView/components/SecondaryWallets/SecondaryWallets.js';
import '@hcaptcha/react-hcaptcha';
import '../../../widgets/DynamicWidget/context/DynamicWidgetContext.js';
import '../../../widgets/DynamicWidget/helpers/convertExchangeKeyAndProviderEnum.js';
import '../../ExchangeWhitelistWarning/ExchangeWhitelistWarning.js';
import '../../../context/ErrorContext/hooks/useErrorText/useErrorText.js';
import '../../../context/FooterAnimationContext/index.js';
import '../../MfaChooseDeviceView/useGetMfaOptions/useGetMfaOptions.js';
import '../../../context/PasskeyContext/PasskeyContext.js';
import { EmbeddedAuthChoiceCards } from '../EmbeddedAuthChoiceCards/EmbeddedAuthChoiceCards.js';
import { AuthChoiceInfo } from './AuthChoiceInfo/AuthChoiceInfo.js';
import '../../../context/OnrampContext/OnrampContext.js';
import '../../../store/state/sendBalances.js';
import '../../../store/state/connectorsInitializing/connectorsInitializing.js';
import '../../../components/OverlayCardBase/OverlayCardTarget/OverlayCardTarget.js';
import '../../../widgets/DynamicWidget/components/DynamicWidgetHeader/DynamicWidgetHeader.js';
import '../../TransactionConfirmationView/TransactionConfirmationView.js';
import '../../../widgets/DynamicWidget/components/PasskeyCard/PasskeyCard.js';
import '../../../widgets/DynamicWidget/views/CryptoComOnramp/CryptoComOnramp.js';
import '../../../../index.js';
import '../../../widgets/DynamicWidget/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 titleDescriptionMap = {
    helpSession: {
        description: 'dyn_embedded_authenticator.need_help_description',
        title: 'dyn_embedded_authenticator.need_help_title',
    },
    reveal: {
        description: 'dyn_embedded_authenticator.reveal_description',
        title: 'dyn_embedded_authenticator.reveal_title',
    },
    sendTransaction: {
        description: 'dyn_embedded_authenticator.description',
        title: 'dyn_embedded_authenticator.title',
    },
    signMessage: {
        description: 'dyn_embedded_authenticator.description',
        title: 'dyn_embedded_authenticator.title',
    },
    signTransaction: {
        description: 'dyn_embedded_authenticator.description',
        title: 'dyn_embedded_authenticator.title',
    },
};
const EmbeddedAuthChoice = ({ currentAction, }) => {
    var _a, _b, _c;
    const { t } = useTranslation();
    const { primaryWallet, projectSettings, setShowAuthFlow } = useInternalDynamicContext();
    const { getEOAWallet } = useSmartWallets();
    const wallet = (_a = (primaryWallet && getEOAWallet(primaryWallet))) !== null && _a !== void 0 ? _a : primaryWallet;
    const { initPasskeyRecoveryProcess } = usePasskeyRecovery();
    const { isTurnkeyWallet } = useIsTurnkeyWallet();
    const supportedAuthenticators = useMemo(() => {
        var _a;
        const supportedAuths = getEnabledEmbeddedWalletsAuthenticators((_a = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.sdk.embeddedWallets) === null || _a === void 0 ? void 0 : _a.supportedSecurityMethods);
        const supportedMethods = Object.keys(supportedAuths);
        if (!currentAction ||
            !['helpSession', 'reveal'].includes(currentAction.type)) {
            return supportedMethods.filter((auth) => auth !== 'email');
        }
        return supportedMethods;
    }, [projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.sdk.embeddedWallets]);
    const isEmailAuthSessionActive = () => {
        if (!wallet || !isTurnkeyWallet) {
            return false;
        }
        const turnkeyAuthenticatorHandler = wallet.connector.getAuthenticatorHandler();
        return turnkeyAuthenticatorHandler.isSessionActive();
    };
    const renderIcon = () => supportedAuthenticators.length > 1 ? (jsx(SvgMultipleAccountSecurity, {})) : (jsx(SvgPasskeyAccountSecurity, {}));
    const buttonCopyKey = isEmailAuthSessionActive()
        ? 'dyn_embedded_authenticator.passkey.auth_info.start_button'
        : 'dyn_embedded_authenticator.passkey.auth_info.add_button';
    const { title, description } = titleDescriptionMap[(currentAction === null || currentAction === void 0 ? void 0 : currentAction.type) || 'signMessage'];
    const supportUrls = (_b = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.general) === null || _b === void 0 ? void 0 : _b.supportUrls;
    const supportEmail = (_c = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.general) === null || _c === void 0 ? void 0 : _c.supportEmail;
    if (!(supportedAuthenticators === null || supportedAuthenticators === void 0 ? void 0 : supportedAuthenticators.length)) {
        return null;
    }
    return (jsxs(Fragment, { children: [jsx(ModalHeader, { children: jsx(Typography, { as: 'h1', variant: 'title', color: 'primary', "data-testid": 'dynamic-auth-modal-heading', className: 'header__typography', copykey: title, children: t(title) }) }), jsxs("div", { className: 'embedded-auth-choice', children: [jsx("div", { className: 'embedded-auth-choice__header', children: renderIcon() }), jsx(Typography, { variant: 'body_normal', weight: 'regular', color: 'secondary', className: 'embedded-auth-choice__subtitle', copykey: description, children: t(description) }), supportedAuthenticators.length > 1 ? (jsx(EmbeddedAuthChoiceCards, { currentAction: currentAction, supportedAuthenticators: supportedAuthenticators })) : (jsxs(Fragment, { children: [jsx(AuthChoiceInfo, { choice: 'passkey' }), jsx(TypographyButton, { dataTestId: 'passkey-only-choice-button', buttonVariant: 'brand-primary', expanded: true, onClick: () => initPasskeyRecoveryProcess('passkey'), copykey: buttonCopyKey, typographyProps: {
                                    color: 'inherit',
                                }, children: t(buttonCopyKey) })] })), (currentAction === null || currentAction === void 0 ? void 0 : currentAction.type) !== 'reveal' && (jsx(TypographyButton, { buttonVariant: 'primary', buttonClassName: 'embedded-auth-choice__skip-button', expanded: true, onClick: () => {
                            var _a, _b, _c;
                            ((_a = currentAction === null || currentAction === void 0 ? void 0 : currentAction.settings) === null || _a === void 0 ? void 0 : _a.goBack)
                                ? (_c = (_b = currentAction === null || currentAction === void 0 ? void 0 : currentAction.settings) === null || _b === void 0 ? void 0 : _b.goBack) === null || _c === void 0 ? void 0 : _c.call(_b)
                                : setShowAuthFlow(false);
                        }, copykey: 'dyn_embedded_authenticator.skip', children: t('dyn_embedded_authenticator.skip') })), (currentAction === null || currentAction === void 0 ? void 0 : currentAction.type) === 'helpSession' &&
                        (supportUrls || supportEmail) && (jsx("div", { className: 'embedded-auth-choice__support-links', children: jsx(SupportLinks, { supportEmail: supportEmail, supportUrls: supportUrls }) }))] })] }));
};

export { EmbeddedAuthChoice };
