'use client'
'use strict';

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

var jsxRuntime = require('react/jsx-runtime');
var React = require('react');
var reactI18next = require('react-i18next');
var utils = require('@dynamic-labs/utils');
require('../../../components/Accordion/components/AccordionItem/AccordionItem.cjs');
var close = require('../../../shared/assets/close.cjs');
var exportEmbeddedHero = require('../../../shared/assets/export-embedded-hero.cjs');
var eyeOffIcon = require('../../../shared/assets/eye-off-icon.cjs');
var info = require('../../../shared/assets/info.cjs');
var walletV2 = require('../../../shared/assets/wallet-v2.cjs');
require('@dynamic-labs/iconic');
var ViewContext = require('../../../context/ViewContext/ViewContext.cjs');
require('../../../../../_virtual/_tslib.cjs');
require('@dynamic-labs/wallet-connector-core');
var logger = require('../../../shared/logger.cjs');
require('@dynamic-labs/wallet-book');
require('../../../utils/constants/colors.cjs');
require('../../../utils/constants/values.cjs');
require('@dynamic-labs/sdk-api-core');
require('../../../shared/consts/index.cjs');
require('../../../components/Alert/Alert.cjs');
var dynamicEvents = require('../../../events/dynamicEvents.cjs');
require('../../../context/DynamicContext/DynamicContext.cjs');
require('../../../store/state/loadingAndLifecycle/loadingAndLifecycle.cjs');
require('../../../store/state/authMode/authMode.cjs');
var useInternalDynamicContext = require('../../../context/DynamicContext/useDynamicContext/useInternalDynamicContext/useInternalDynamicContext.cjs');
require('../../../context/CaptchaContext/CaptchaContext.cjs');
require('../../../context/ErrorContext/ErrorContext.cjs');
var errors = require('../../../utils/constants/errors.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('@dynamic-labs/locale');
require('../../../store/state/dynamicContextProps/dynamicContextProps.cjs');
require('../../../store/state/primaryWalletId/primaryWalletId.cjs');
require('../../../store/state/connectedWalletsInfo/connectedWalletsInfo.cjs');
var exportWalletCredential = require('../../../utils/functions/exportWalletCredential/exportWalletCredential.cjs');
require('../../../context/AccessDeniedContext/AccessDeniedContext.cjs');
require('../../../context/AccountExistsContext/AccountExistsContext.cjs');
require('../../../context/UserWalletsContext/UserWalletsContext.cjs');
require('../../../context/VerificationContext/VerificationContext.cjs');
require('react-dom');
require('../../../utils/functions/compareChains/compareChains.cjs');
require('../../Passkey/utils/findPrimaryEmbeddedChain/findPrimaryEmbeddedChain.cjs');
var getTurnkeyWalletId = require('../../../utils/functions/getTurnkeyWalletId/getTurnkeyWalletId.cjs');
require('../../../context/ThemeContext/ThemeContext.cjs');
var useSmartWallets = require('../../../utils/hooks/useSmartWallets/useSmartWallets.cjs');
require('../../../utils/hooks/useUserUpdateRequest/useUpdateUser/userFieldsSchema.cjs');
var useMutation = require('../../../utils/hooks/useMutation/useMutation.cjs');
var useIsTurnkeyWallet = require('../../../utils/hooks/useIsTurnkeyWallet/useIsTurnkeyWallet.cjs');
require('bs58');
require('@dynamic-labs/types');
require('../../../context/SocialRedirectContext/SocialRedirectContext.cjs');
require('../../../context/LoadingContext/LoadingContext.cjs');
require('../../../context/WalletContext/WalletContext.cjs');
var turnkeyExport = require('../utils/turnkeyExport/turnkeyExport.cjs');
require('../../../utils/hooks/useEmbeddedWallet/useSecureEnclaveEmbeddedWallet/constants.cjs');
require('yup');
require('../../../context/MockContext/MockContext.cjs');
require('../../CollectUserDataView/useFields.cjs');
require('../../../context/FieldsStateContext/FieldsStateContext.cjs');
require('../../../context/UserFieldEditorContext/UserFieldEditorContext.cjs');
require('@dynamic-labs/rpc-providers');
require('../../../store/state/walletOptions/walletOptions.cjs');
var Typography = require('../../../components/Typography/Typography.cjs');
require('../../../context/FooterAnimationContext/index.cjs');
require('../../../components/ShadowDOM/ShadowDOM.cjs');
require('../../../components/Transition/ZoomTransition/ZoomTransition.cjs');
require('../../../components/Transition/SlideInUpTransition/SlideInUpTransition.cjs');
require('../../../components/Transition/OpacityTransition/OpacityTransition.cjs');
require('../../../components/OverlayCardBase/OverlayCardTarget/OverlayCardTarget.cjs');
require('../../../context/WalletGroupContext/WalletGroupContext.cjs');
require('../../../widgets/DynamicWidget/components/DynamicWidgetHeader/DynamicWidgetHeader.cjs');
require('react-focus-lock');
var DynamicWidgetContext = require('../../../widgets/DynamicWidget/context/DynamicWidgetContext.cjs');
var Checkbox = require('../../../components/Checkbox/Checkbox.cjs');
var ErrorContainer = require('../../../components/ErrorContainer/ErrorContainer.cjs');
var IconButton = require('../../../components/IconButton/IconButton.cjs');
var ModalHeader = require('../../../components/ModalHeader/ModalHeader.cjs');
var TypographyButton = require('../../../components/TypographyButton/TypographyButton.cjs');
var getProperErrorMessage = require('../../../modals/SignMessageConfirmationModal/getProperErrorMessage.cjs');
require('../../../components/MenuList/Dropdown/Dropdown.cjs');
var Badge = require('../../../components/Badge/Badge.cjs');
require('formik');
require('../../../utils/hooks/useSubdomainCheck/useSubdomainCheck.cjs');
require('../../../store/state/sendBalances.cjs');
require('../../../components/Input/Input.cjs');
require('../../../components/OverlayCard/OverlayCard.cjs');
require('../../TransactionConfirmationView/TransactionConfirmationView.cjs');
require('../../../components/PasskeyCreatedSuccessBanner/PasskeyCreatedSuccessBanner.cjs');
require('../../../context/PasskeyContext/PasskeyContext.cjs');
require('../../../widgets/DynamicWidget/components/PasskeyCard/PasskeyCard.cjs');
require('../../../widgets/DynamicWidget/views/CryptoComOnramp/CryptoComOnramp.cjs');
require('../../../context/OnrampContext/OnrampContext.cjs');
require('../../../../index.cjs');
require('../../../widgets/DynamicWidget/helpers/convertExchangeKeyAndProviderEnum.cjs');
require('qrcode');
require('../../../widgets/DynamicWidget/views/ReceiveWalletFunds/ReceiveWalletFunds.cjs');
var NeedHelpSection = require('../../../components/NeedHelpSection/NeedHelpSection.cjs');
require('../../../context/IpConfigurationContext/IpConfigurationContext.cjs');
require('../../../context/ConnectWithOtpContext/ConnectWithOtpContext.cjs');
require('../../../widgets/DynamicBridgeWidget/views/WalletsView/components/SecondaryWallets/SecondaryWallets.cjs');
require('@hcaptcha/react-hcaptcha');
var waasExport = require('../utils/waasExport/waasExport.cjs');
require('../../ExchangeWhitelistWarning/ExchangeWhitelistWarning.cjs');
require('../../../context/ErrorContext/hooks/useErrorText/useErrorText.cjs');
require('../../MfaChooseDeviceView/useGetMfaOptions/useGetMfaOptions.cjs');
require('../../../store/state/connectorsInitializing/connectorsInitializing.cjs');
require('../../../store/state/tokenBalances.cjs');
require('../../../store/state/multichainBalances.cjs');
var useSyncEmbeddedWalletFlow = require('../../../utils/hooks/useSyncEmbeddedWalletFlow/useSyncEmbeddedWalletFlow.cjs');
require('../../../shared/utils/functions/getInitialUrl/getInitialUrl.cjs');
var EmbeddedWalletExport = require('../../../components/EmbeddedWalletExport/EmbeddedWalletExport.cjs');
require('../../../components/InlineWidget/InlineWidget.cjs');
require('../../../components/IsBrowser/IsBrowser.cjs');
require('../../../components/Popper/Popper/Popper.cjs');
require('../../../components/Popper/PopperContext/PopperContext.cjs');

const EmbeddedRevealView = ({ exportPrivateKey, isPromptForExport = false, }) => {
    var _a, _b, _c;
    const { primaryWallet, user, setShowAuthFlow, environmentId } = useInternalDynamicContext.useInternalDynamicContext();
    const { handleAcknowledgeExportPrompt } = useSyncEmbeddedWalletFlow.useSyncEmbeddedWalletFlow();
    const { isTurnkeyWallet } = useIsTurnkeyWallet.useIsTurnkeyWallet();
    const isWaasWallet = Boolean(user === null || user === void 0 ? void 0 : user.verifiedCredentials.find((vc) => { var _a; return (_a = vc.walletName) === null || _a === void 0 ? void 0 : _a.startsWith('dynamicwaas'); }));
    const { setDynamicWidgetView } = DynamicWidgetContext.useWidgetContext();
    const { pushView } = ViewContext.useViewContext();
    const { getEOAWallet, isSmartWallet } = useSmartWallets.useSmartWallets();
    const [acknowledgement1, setAcknowledgement1] = React.useState(false);
    const [iframeContainerRef, setIframeContainerRef] = React.useState(null);
    if (!primaryWallet) {
        throw new utils.DynamicError('No primary wallet found', errors.INVALID_WALLET_DATA);
    }
    let primaryTurnkeyWalletId;
    try {
        primaryTurnkeyWalletId = getTurnkeyWalletId.getTurnkeyWalletId(primaryWallet.id, (user === null || user === void 0 ? void 0 : user.verifiedCredentials) || []);
    }
    catch (error) {
        // waas wallet
    }
    const walletProperties = (_b = (_a = user === null || user === void 0 ? void 0 : user.verifiedCredentials) === null || _a === void 0 ? void 0 : _a.find(({ walletName, id }) => (walletName === null || walletName === void 0 ? void 0 : walletName.startsWith('turnkey')) && id === primaryTurnkeyWalletId)) === null || _b === void 0 ? void 0 : _b.walletProperties;
    const isTurnkeyHDWallet = walletProperties === null || walletProperties === void 0 ? void 0 : walletProperties.turnkeyHDWalletId;
    const wallet = (_c = (primaryWallet && getEOAWallet(primaryWallet))) !== null && _c !== void 0 ? _c : primaryWallet;
    // eslint-disable-next-line arrow-body-style
    React.useEffect(() => {
        return () => {
            if (isTurnkeyWallet) {
                turnkeyExport.cleanupExport({
                    wallet: wallet,
                });
            }
            if (isWaasWallet) {
                waasExport.cleanupExport({
                    wallet: wallet,
                });
            }
        };
        // only run once on unmount
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, []);
    const { mutate: handleExportWallet, isLoading, error, data, } = useMutation.useMutation(() => exportWalletCredential.exportWalletCredential({
        environmentId,
        exportPrivateKey,
        handleAcknowledgeExportPrompt,
        iframeContainerRef: iframeContainerRef,
        isTurnkeyWallet,
        isWaasWallet,
        user,
        wallet,
    }), {
        onFailure: (err) => {
            logger.logger.error('Failed to export', err);
            dynamicEvents.dynamicEvents.emit('embeddedWalletRevealFailed', err);
        },
    });
    const errorText = React.useMemo(() => {
        if (!error) {
            return undefined;
        }
        if (error instanceof utils.DynamicError) {
            return error.message;
        }
        try {
            return getProperErrorMessage.getProperErrorMessage(error, user);
        }
        catch (e) {
            if (e instanceof utils.AccessBlockedError) {
                pushView('access-blocked');
            }
            return;
        }
    }, [error, pushView, user]);
    const { t } = reactI18next.useTranslation();
    const contentHeader = (jsxRuntime.jsxs("div", { children: [primaryWallet &&
                isSmartWallet(primaryWallet) &&
                !isLoading &&
                data &&
                !error && (jsxRuntime.jsxs("div", { className: 'embedded-reveal-view__zerodev-warning', children: [jsxRuntime.jsxs("div", { className: 'embedded-reveal-view__zerodev-warning__title-row', children: [jsxRuntime.jsx(info.ReactComponent, { className: 'embedded-reveal-view__zerodev-warning__icon' }), jsxRuntime.jsx(Typography.Typography, { variant: 'body_normal', weight: 'bold', copykey: 'dyn_embedded_reveal.aa_warning.title', children: t('dyn_embedded_reveal.aa_warning.title') })] }), jsxRuntime.jsxs(Typography.Typography, { variant: 'body_normal', weight: 'regular', copykey: 'dyn_embedded_reveal.aa_warning.subtitle', children: [t('dyn_embedded_reveal.aa_warning.subtitle'), jsxRuntime.jsx("button", { onClick: () => {
                                    setShowAuthFlow(false);
                                    setDynamicWidgetView('send-balance');
                                }, className: 'embedded-reveal-view__zerodev-warning__link-button', children: jsxRuntime.jsx(Typography.Typography, { variant: 'body_normal', weight: 'regular', color: 'primary', className: 'underline', copykey: 'dyn_embedded_reveal.aa_warning.button', children: t('dyn_embedded_reveal.aa_warning.button') }) })] })] })), jsxRuntime.jsx("div", { className: 'embedded-reveal-view__body__description', children: !(!isLoading && data && !error) && (jsxRuntime.jsx("div", { className: 'embedded-reveal-view__header', children: jsxRuntime.jsx("div", { className: 'embedded-reveal-view__header__hero', children: jsxRuntime.jsx(exportEmbeddedHero.ReactComponent, {}) }) })) })] }));
    const closeButton = (jsxRuntime.jsx(IconButton.IconButton, { onClick: () => {
            setShowAuthFlow(false);
        }, type: 'button', children: jsxRuntime.jsx(close.ReactComponent, {}) }));
    const hasInjectedCredential = !isLoading && data && !error;
    const credentialTitle = !isTurnkeyHDWallet || exportPrivateKey
        ? t('dyn_embedded_reveal.private_key_title')
        : t('dyn_embedded_reveal.recovery_phrase_title');
    const getTitle = () => {
        if (hasInjectedCredential) {
            return credentialTitle;
        }
        if (isPromptForExport) {
            return t('dyn_embedded_reveal.prompt_for_export_title');
        }
        return t('dyn_embedded_reveal.agreement_title');
    };
    const title = getTitle();
    const handleClickUnlink = () => {
        pushView('embedded-delete-view');
    };
    // Extract nested ternaries into separate functions for clarity
    const getBodyClassName = () => {
        const baseClass = 'embedded-reveal-view__body';
        if (isWaasWallet && hasInjectedCredential && title === credentialTitle) {
            return `${baseClass} ${baseClass}--waas-credentials`;
        }
        return baseClass;
    };
    return (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx(ModalHeader.ModalHeader, { trailing: isPromptForExport ? null : closeButton, children: jsxRuntime.jsx(Typography.Typography, { as: 'h1', variant: 'title', color: 'primary', "data-testid": 'dynamic-auth-modal-heading', className: 'header__typography', children: title }) }), jsxRuntime.jsx("div", { className: 'embedded-reveal-view', children: jsxRuntime.jsxs("div", { className: getBodyClassName(), children: [(!isWaasWallet ||
                            !hasInjectedCredential ||
                            title !== credentialTitle) &&
                            contentHeader, (isTurnkeyWallet || isWaasWallet) && (jsxRuntime.jsx(EmbeddedWalletExport.EmbeddedWalletExport, { wallet: wallet, isTurnkeyWallet: isTurnkeyWallet, isWaasWallet: isWaasWallet, data: data, onIframeContainerRef: setIframeContainerRef, isVisible: !title.includes('Agree') })), hasInjectedCredential ? (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx(TypographyButton.TypographyButton, { buttonPadding: 'medium', buttonVariant: 'brand-primary', onClick: () => {
                                        setShowAuthFlow(false);
                                    }, loading: isLoading, copykey: 'dyn_embedded_reveal.done_button_label', typographyProps: {
                                        color: 'inherit',
                                    }, children: t('dyn_embedded_reveal.done_button_label') }), !exportPrivateKey && (jsxRuntime.jsx(TypographyButton.TypographyButton, { buttonClassName: 'embedded-reveal-view__body__unlink_button', buttonVariant: 'tertiary', buttonPadding: 'none', copykey: 'dyn_embedded_reveal.unlink', onClick: handleClickUnlink, typographyProps: {
                                        color: 'secondary',
                                        variant: 'button_tertiary',
                                    }, children: t('dyn_embedded_reveal.unlink') }))] })) : (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsxs("div", { className: 'embedded-reveal-view__body__description', children: [jsxRuntime.jsx("div", { className: 'embedded-reveal-view__body__badge-container', children: jsxRuntime.jsx(Badge.Badge, { text: t('dyn_embedded_reveal.badge_label'), className: 'embedded-reveal-view__body__badge' }) }), jsxRuntime.jsx(Typography.Typography, { variant: 'body_normal', color: 'primary', weight: 'regular', copykey: 'dyn_embedded_reveal.prompt_for_export_description', children: t('dyn_embedded_reveal.prompt_for_export_description') })] }), errorText && jsxRuntime.jsx(ErrorContainer.ErrorContainer, { children: errorText }), jsxRuntime.jsxs("div", { className: 'embedded-reveal-view__body__card', children: [jsxRuntime.jsxs("div", { className: 'embedded-reveal-view__body__card__statement', children: [jsxRuntime.jsx("div", { className: 'embedded-reveal-view__body__card__icon', children: jsxRuntime.jsx(walletV2.ReactComponent, { height: 16, width: 16 }) }), jsxRuntime.jsxs("div", { className: 'embedded-reveal-view__body__card__statement__text', children: [jsxRuntime.jsxs(Typography.Typography, { variant: 'body_small', color: 'primary', copykey: 'dyn_embedded_reveal.statement_1.title', children: [t('dyn_embedded_reveal.statement_1.title'), ":"] }), jsxRuntime.jsx(Typography.Typography, { variant: 'body_small', color: 'primary', copykey: 'dyn_embedded_reveal.statement_1.description', children: t('dyn_embedded_reveal.statement_1.description') })] })] }), jsxRuntime.jsxs("div", { className: 'embedded-reveal-view__body__card__statement', children: [jsxRuntime.jsx("div", { className: 'embedded-reveal-view__body__card__icon', children: jsxRuntime.jsx(eyeOffIcon.ReactComponent, { height: 16, width: 16 }) }), jsxRuntime.jsx("div", { className: 'embedded-reveal-view__body__card__statement__text', children: jsxRuntime.jsx(Typography.Typography, { variant: 'body_small', color: 'primary', copykey: 'dyn_embedded_reveal.statement_2.title', children: t('dyn_embedded_reveal.statement_2.title') }) })] })] }), jsxRuntime.jsx("div", { className: 'embedded-reveal-view__body__confirm_card', children: jsxRuntime.jsxs("button", { className: 'embedded-reveal-view__body__card__acknowledgement', onClick: () => setAcknowledgement1(!acknowledgement1), children: [jsxRuntime.jsx("div", { children: jsxRuntime.jsx(Checkbox.Checkbox, { checked: acknowledgement1, onChange: () => setAcknowledgement1(!acknowledgement1), className: 'embedded-reveal-view__body__card__statement__checkbox', id: 'embedded-reveal-checkbox-1' }) }), jsxRuntime.jsx("div", { children: jsxRuntime.jsx(Typography.Typography, { variant: 'body_small', color: 'primary', style: { letterSpacing: '-0.15px' }, copykey: 'dyn_embedded_reveal.checkbox_label', children: t('dyn_embedded_reveal.checkbox_label') }) })] }) }), jsxRuntime.jsx(NeedHelpSection.NeedHelpSection, { isExport: true }), jsxRuntime.jsxs("div", { className: 'embedded-reveal-view__body__button_section', children: [isPromptForExport && (jsxRuntime.jsx(TypographyButton.TypographyButton, { buttonPadding: 'medium', buttonVariant: 'primary', onClick: () => {
                                                handleAcknowledgeExportPrompt();
                                                setShowAuthFlow(false);
                                            }, dataTestId: 'embedded-reveal-button', copykey: 'dyn_embedded_reveal.skip_button_label', expanded: true, buttonClassName: 'embedded-reveal-view__body__button', children: t('dyn_embedded_reveal.skip_button_label') })), jsxRuntime.jsx(TypographyButton.TypographyButton, { buttonPadding: 'medium', buttonVariant: 'brand-primary', typographyProps: {
                                                color: 'inherit',
                                            }, onClick: () => handleExportWallet(), disabled: !acknowledgement1, loading: isLoading, dataTestId: 'embedded-reveal-button', copykey: 'dyn_embedded_reveal.reveal_button_label', style: { width: '100%' }, className: 'embedded-reveal-view__body__button', expanded: true, children: isPromptForExport
                                                ? t('dyn_embedded_reveal.backup_button_label')
                                                : t('dyn_embedded_reveal.reveal_button_label') })] })] }))] }) })] }));
};

exports.EmbeddedRevealView = EmbeddedRevealView;
