'use client'
import { __awaiter } from '../../../../../../../_virtual/_tslib.js';
import { jsxs, jsx } from 'react/jsx-runtime';
import { useCallback, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { WaasBackupOptionsEnum } from '@dynamic-labs/sdk-api-core';
import { isSessionKeyCompatibleWallet } from '@dynamic-labs/wallet-connector-core';
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 { ReactComponent as SvgChevronLeft } from '../../../../../shared/assets/chevron-left.js';
import { ReactComponent as SvgExportPrivateKey } from '../../../../../shared/assets/export-private-key.js';
import { ReactComponent as SvgExportRecoveryPhrase } from '../../../../../shared/assets/export-recovery-phrase.js';
import '../../../../../context/ViewContext/ViewContext.js';
import { ReactComponent as SvgBackupArrow } from '../../../../../shared/assets/backupArrow.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 '../../../../../shared/consts/index.js';
import '../../../../../events/dynamicEvents.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 '@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 { useThemeContext } from '../../../../../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 '../../../../../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 '../../../../../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 '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 { useWidgetContext } from '../../../context/DynamicWidgetContext.js';
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 { useEmbeddedReveal } from '../../../../../utils/hooks/useEmbeddedReveal/useEmbeddedReveal.js';
import '../../CryptoComOnramp/CryptoComOnramp.js';
import '../../../../../../index.js';
import '../../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 EmbeddedWalletExportSection = () => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
    const { t } = useTranslation();
    const { theme } = useThemeContext();
    const { primaryWallet, user, projectSettings } = useInternalDynamicContext();
    const { setDynamicWidgetView } = useWidgetContext();
    const { getEOAWallet } = useSmartWallets();
    const { initPasskeyRecoveryProcess, shouldInitRecovery } = usePasskeyRecovery();
    const { initExportProcess } = useEmbeddedReveal();
    const eoaWallet = primaryWallet && getEOAWallet(primaryWallet);
    const wallet = eoaWallet !== null && eoaWallet !== void 0 ? eoaWallet : primaryWallet;
    const isEmbeddedWallet = Boolean(wallet &&
        (((_b = (_a = wallet === null || wallet === void 0 ? void 0 : wallet.connector) === null || _a === void 0 ? void 0 : _a.key) === null || _b === void 0 ? void 0 : _b.startsWith('turnkey')) ||
            ((_d = (_c = wallet === null || wallet === void 0 ? void 0 : wallet.connector) === null || _c === void 0 ? void 0 : _c.key) === null || _d === void 0 ? void 0 : _d.startsWith('dynamicwaas'))));
    const turnkeyWalletProperties = (_f = (_e = user === null || user === void 0 ? void 0 : user.verifiedCredentials) === null || _e === void 0 ? void 0 : _e.find(({ walletName, id }) => (walletName === null || walletName === void 0 ? void 0 : walletName.startsWith('turnkey')) &&
        ((primaryWallet === null || primaryWallet === void 0 ? void 0 : primaryWallet.id) ? id === (primaryWallet === null || primaryWallet === void 0 ? void 0 : primaryWallet.id) : true))) === null || _f === void 0 ? void 0 : _f.walletProperties;
    const isTurnkeyHDWallet = Boolean(turnkeyWalletProperties === null || turnkeyWalletProperties === void 0 ? void 0 : turnkeyWalletProperties.turnkeyHDWalletId);
    const isSessionKeyCompatible = (primaryWallet && isSessionKeyCompatibleWallet(primaryWallet)) ||
        (eoaWallet && isSessionKeyCompatibleWallet(eoaWallet));
    const handleExportClick = useCallback((recoveryPhrase) => __awaiter(void 0, void 0, void 0, function* () {
        if (!isSessionKeyCompatible) {
            if (yield shouldInitRecovery()) {
                yield initPasskeyRecoveryProcess(undefined, {
                    type: 'reveal',
                });
            }
        }
        return initExportProcess(recoveryPhrase);
    }), [
        shouldInitRecovery,
        initPasskeyRecoveryProcess,
        initExportProcess,
        isSessionKeyCompatible,
    ]);
    const handleExportButtonClick = useCallback(() => handleExportClick(), [handleExportClick]);
    const handleExportWithPhraseClick = useCallback(() => handleExportClick(true), [handleExportClick]);
    const exportIconClasses = useMemo(() => `settings-view__body__section__button__icon${theme.theme.name === 'dark' ? '__stroke' : ''}`, [theme.theme.name]);
    const isV3WaasWallet = Boolean(wallet && ((_h = (_g = wallet === null || wallet === void 0 ? void 0 : wallet.connector) === null || _g === void 0 ? void 0 : _g.key) === null || _h === void 0 ? void 0 : _h.startsWith('dynamicwaas')));
    const isGoogleDriveBackupEnabled = Boolean((_l = (_k = (_j = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.sdk) === null || _j === void 0 ? void 0 : _j.waas) === null || _k === void 0 ? void 0 : _k.backupOptions) === null || _l === void 0 ? void 0 : _l.includes(WaasBackupOptionsEnum.GoogleDrive));
    const shouldShowBackupOption = isV3WaasWallet && isGoogleDriveBackupEnabled;
    const handleBackupClick = useCallback(() => {
        setDynamicWidgetView('waas-backup');
    }, [setDynamicWidgetView]);
    if (!isEmbeddedWallet) {
        return null;
    }
    return (jsxs("div", { className: 'settings-view__body__section', children: [jsx("div", { className: 'settings-view__body__section__title', children: jsx(Typography, { copykey: 'dyn_settings.export_section.title', color: 'secondary', weight: 'medium', children: t('dyn_settings.export_section.title') }) }), jsx(TypographyButton, { dataTestId: 'embedded-reveal-account-button', onClick: handleExportButtonClick, buttonClassName: 'settings-view__body__section__button', startSlot: 
                // eslint-disable-next-line react/jsx-wrap-multilines
                jsxs("div", { className: 'settings-view__body__section__button__start-slot', children: [jsx(SvgExportPrivateKey, { className: exportIconClasses }), jsx(Typography, { color: 'primary', weight: 'medium', copykey: 'dyn_settings.export_section.private_key_button', children: t('dyn_settings.export_section.private_key_button') })] }), endSlot: jsx(SvgChevronLeft, { className: 'settings-view__body__section__button__icon-secondary--rotate' }), showInternalLoading: false }), isTurnkeyHDWallet && (jsx(TypographyButton, { dataTestId: 'embedded-reveal-button', onClick: handleExportWithPhraseClick, buttonClassName: 'settings-view__body__section__button', startSlot: 
                // eslint-disable-next-line react/jsx-wrap-multilines
                jsxs("div", { className: 'settings-view__body__section__button__start-slot', children: [jsx(SvgExportRecoveryPhrase, { className: exportIconClasses }), jsx(Typography, { color: 'primary', weight: 'medium', copykey: 'dyn_settings.export_section.srp_button', children: t('dyn_settings.export_section.srp_button') })] }), endSlot: jsx(SvgChevronLeft, { className: 'settings-view__body__section__button__icon-secondary--rotate' }), showInternalLoading: false })), shouldShowBackupOption && (jsx(TypographyButton, { dataTestId: 'backup-button', onClick: handleBackupClick, buttonClassName: 'settings-view__body__section__button', startSlot: 
                // eslint-disable-next-line react/jsx-wrap-multilines
                jsxs("div", { className: 'settings-view__body__section__button__start-slot', children: [jsx(SvgBackupArrow, { className: exportIconClasses }), jsx(Typography, { color: 'primary', weight: 'medium', copykey: 'dyn_settings.export_section.backup_button', children: t('dyn_settings.export_section.backup_button') })] }), endSlot: jsx(SvgChevronLeft, { className: 'settings-view__body__section__button__icon-secondary--rotate' }), showInternalLoading: false }))] }));
};

export { EmbeddedWalletExportSection };
