'use client'
import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
import { useTranslation } from 'react-i18next';
import { MFADeviceType } from '@dynamic-labs/sdk-api-core';
import 'react';
import { ReactComponent as SvgSecureAccount } from '../../assets/secure-account.js';
import '@dynamic-labs/utils';
import '../../components/Accordion/components/AccordionItem/AccordionItem.js';
import { ReactComponent as SvgChevronLeft } from '../../shared/assets/chevron-left.js';
import '@dynamic-labs/iconic';
import { useViewContext } from '../../context/ViewContext/ViewContext.js';
import { Button } from '../../components/Button/Button.js';
import '../../components/Alert/Alert.js';
import '../../events/dynamicEvents.js';
import '../../../../_virtual/_tslib.js';
import { useDynamicContext } from '../../context/DynamicContext/useDynamicContext/useDynamicContext.js';
import '../../context/DynamicContext/DynamicContext.js';
import '@dynamic-labs/wallet-connector-core';
import '../../context/CaptchaContext/CaptchaContext.js';
import '../../context/ErrorContext/ErrorContext.js';
import { logger } from '../../shared/logger.js';
import '@dynamic-labs/wallet-book';
import '../../utils/constants/colors.js';
import '../../utils/constants/values.js';
import '../../shared/consts/index.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 '@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 '../../store/state/loadingAndLifecycle/loadingAndLifecycle.js';
import '../../context/VerificationContext/VerificationContext.js';
import 'react-dom';
import '../../utils/functions/compareChains/compareChains.js';
import '../Passkey/utils/findPrimaryEmbeddedChain/findPrimaryEmbeddedChain.js';
import '../../context/ThemeContext/ThemeContext.js';
import '../../utils/hooks/useUserUpdateRequest/useUpdateUser/userFieldsSchema.js';
import 'bs58';
import { usePromise } from '../../utils/hooks/usePromise/usePromise.js';
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 '../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 { Typography } from '../../components/Typography/Typography.js';
import '../../context/FooterAnimationContext/index.js';
import '../../components/ShadowDOM/ShadowDOM.js';
import '../../components/Transition/ZoomTransition/ZoomTransition.js';
import '../../components/Transition/SlideInUpTransition/SlideInUpTransition.js';
import '../../components/Transition/OpacityTransition/OpacityTransition.js';
import '../../components/OverlayCardBase/OverlayCardTarget/OverlayCardTarget.js';
import '../../context/WalletGroupContext/WalletGroupContext.js';
import '../../widgets/DynamicWidget/components/DynamicWidgetHeader/DynamicWidgetHeader.js';
import 'react-focus-lock';
import '../../widgets/DynamicWidget/context/DynamicWidgetContext.js';
import '../../components/IconButton/IconButton.js';
import { ModalHeader } from '../../components/ModalHeader/ModalHeader.js';
import '../../components/MenuList/Dropdown/Dropdown.js';
import 'formik';
import '../../utils/hooks/useSubdomainCheck/useSubdomainCheck.js';
import '../../store/state/sendBalances.js';
import '../../components/Input/Input.js';
import '../../components/OverlayCard/OverlayCard.js';
import '../TransactionConfirmationView/TransactionConfirmationView.js';
import '../../components/PasskeyCreatedSuccessBanner/PasskeyCreatedSuccessBanner.js';
import '../../context/PasskeyContext/PasskeyContext.js';
import '../../widgets/DynamicWidget/components/PasskeyCard/PasskeyCard.js';
import '../../widgets/DynamicWidget/views/CryptoComOnramp/CryptoComOnramp.js';
import '../../context/OnrampContext/OnrampContext.js';
import '../../../index.js';
import '../../widgets/DynamicWidget/helpers/convertExchangeKeyAndProviderEnum.js';
import { TextButton } from '../../components/TextButton/TextButton.js';
import 'qrcode';
import '../../widgets/DynamicWidget/views/ReceiveWalletFunds/ReceiveWalletFunds.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 '../ExchangeWhitelistWarning/ExchangeWhitelistWarning.js';
import '../../context/ErrorContext/hooks/useErrorText/useErrorText.js';
import { useGetMfaOptions } from './useGetMfaOptions/useGetMfaOptions.js';
import '../../store/state/connectorsInitializing/connectorsInitializing.js';
import '../../store/state/tokenBalances.js';
import '../../store/state/multichainBalances.js';
import { useAuthenticatePasskeyMFA } from '../../utils/hooks/useAuthenticatePasskeyMFA/useAuthenticatePasskeyMFA.js';
import '../../shared/utils/functions/getInitialUrl/getInitialUrl.js';
import { useRegisterPasskey } from '../../utils/hooks/useRegisterPasskey/useRegisterPasskey.js';
import '../../components/InlineWidget/InlineWidget.js';
import '../../components/IsBrowser/IsBrowser.js';
import '../../components/Popper/Popper/Popper.js';
import '../../components/Popper/PopperContext/PopperContext.js';

const MfaChooseDeviceView = ({ isInitialSetup = true, createMfaToken = false, }) => {
    const { handleLogOut } = useDynamicContext();
    const { t } = useTranslation();
    const { pushView } = useViewContext();
    const registerPasskey = useRegisterPasskey();
    const authenticatePasskeyMFA = useAuthenticatePasskeyMFA();
    const getMfaOptions = useGetMfaOptions(isInitialSetup);
    const { data: mfaOptions } = usePromise(getMfaOptions, {
        deps: [isInitialSetup],
        initialData: [],
    });
    const handlePasskeyClick = () => {
        if (isInitialSetup) {
            // we're calling registerPasskey here because we don't want
            // it to be called twice by the usePromise in the setupPasskey view
            const registerPasskeyPromise = registerPasskey();
            pushView('passkey-setup', { registerPasskeyPromise });
            return;
        }
        const createMfaTokenOptions = createMfaToken
            ? {
                singleUse: true,
            }
            : undefined;
        // we're calling authenticatePasskeyMFA here because we don't want
        // it to be called twice by the usePromise in the confirmPasskey view
        const authenticatePasskeyPromise = authenticatePasskeyMFA({
            createMfaToken: createMfaTokenOptions,
        });
        pushView('passkey-confirm', {
            authenticatePasskeyPromise,
            createMfaToken,
        });
    };
    const handleTotpClick = () => {
        if (isInitialSetup) {
            pushView('mfa-secure-device', {
                isInitialSetup: true,
                type: MFADeviceType.Totp,
            });
            return;
        }
        pushView('mfa-verification', {
            createMfaToken,
            isInitialSetup,
            showBackButton: true,
            type: MFADeviceType.Totp,
        });
    };
    const handleClick = (option) => {
        switch (option.type) {
            case 'backupCode':
                pushView('mfa-recovery');
                break;
            case MFADeviceType.Passkey:
                handlePasskeyClick();
                break;
            case MFADeviceType.Totp:
                handleTotpClick();
                break;
            default:
                logger.error(`No handler found for selected MFA option: ${option.type}`);
                break;
        }
    };
    return (jsxs(Fragment, { children: [jsx(ModalHeader, { alignContent: 'bottom', children: jsx("div", { className: 'otp-verification-view__status-header', children: jsx(Typography, { weight: 'medium', variant: 'title', color: 'primary', className: 'otp-verification-view__title', children: isInitialSetup
                            ? t('dyn_mfa.choose_device_view.title')
                            : t('dyn_mfa.choose_device_view.choose_another_method') }) }) }), jsxs("div", { className: 'mfa-choose-device-view__body', "data-testid": 'mfa-choose-device-view', children: [jsx(SvgSecureAccount, {}), jsx(Typography, { className: 'mfa-choose-device-view__copy', variant: 'body_normal', weight: 'regular', color: 'secondary', children: isInitialSetup
                            ? t('dyn_mfa.choose_device_view.select_options')
                            : t('dyn_mfa.choose_device_view.choose_another_method_description') }), mfaOptions.map((option) => (jsxs(Button, { buttonClassName: 'mfa-choose-device-view__button', expanded: true, onClick: () => handleClick(option), children: [jsxs("div", { className: 'mfa-choose-device-view__button__header', "data-testid": `mfa-device-header-${option.type}`, children: [jsx(Typography, { copykey: option.labelKey, color: 'primary', children: t(option.labelKey) }), jsx(SvgChevronLeft, { className: 'mfa-choose-device-view__icon--rotate' })] }), jsx(Typography, { className: 'mfa-choose-device-view__button__description', color: 'secondary', copykey: option.descriptionKey, children: t(option.descriptionKey) })] }, option.type))), jsx(TextButton, { className: 'mfa-choose-device-view__logout', onClick: handleLogOut, copykey: 'dyn_mfa.choose_device_view.logout', children: t('dyn_mfa.choose_device_view.logout') })] })] }));
};

export { MfaChooseDeviceView };
