'use client'
import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
import { useState, useEffect, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { DynamicError, AccessBlockedError } from '@dynamic-labs/utils';
import '../../../components/Accordion/components/AccordionItem/AccordionItem.js';
import { ReactComponent as SvgClose } from '../../../shared/assets/close.js';
import { ReactComponent as SvgExportEmbeddedHero } from '../../../shared/assets/export-embedded-hero.js';
import { ReactComponent as SvgEyeOffIcon } from '../../../shared/assets/eye-off-icon.js';
import { ReactComponent as SvgInfo } from '../../../shared/assets/info.js';
import { ReactComponent as SvgWalletV2 } from '../../../shared/assets/wallet-v2.js';
import '@dynamic-labs/iconic';
import { useViewContext } from '../../../context/ViewContext/ViewContext.js';
import '../../../../../_virtual/_tslib.js';
import '@dynamic-labs/wallet-connector-core';
import { logger } from '../../../shared/logger.js';
import '@dynamic-labs/wallet-book';
import '../../../utils/constants/colors.js';
import '../../../utils/constants/values.js';
import '@dynamic-labs/sdk-api-core';
import '../../../shared/consts/index.js';
import '../../../components/Alert/Alert.js';
import { dynamicEvents } from '../../../events/dynamicEvents.js';
import '../../../context/DynamicContext/DynamicContext.js';
import '../../../store/state/loadingAndLifecycle/loadingAndLifecycle.js';
import '../../../store/state/authMode/authMode.js';
import { useInternalDynamicContext } from '../../../context/DynamicContext/useDynamicContext/useInternalDynamicContext/useInternalDynamicContext.js';
import '../../../context/CaptchaContext/CaptchaContext.js';
import '../../../context/ErrorContext/ErrorContext.js';
import { INVALID_WALLET_DATA } from '../../../utils/constants/errors.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 { exportWalletCredential } from '../../../utils/functions/exportWalletCredential/exportWalletCredential.js';
import '../../../context/AccessDeniedContext/AccessDeniedContext.js';
import '../../../context/AccountExistsContext/AccountExistsContext.js';
import '../../../context/UserWalletsContext/UserWalletsContext.js';
import '../../../context/VerificationContext/VerificationContext.js';
import 'react-dom';
import '../../../utils/functions/compareChains/compareChains.js';
import '../../Passkey/utils/findPrimaryEmbeddedChain/findPrimaryEmbeddedChain.js';
import { getTurnkeyWalletId } from '../../../utils/functions/getTurnkeyWalletId/getTurnkeyWalletId.js';
import '../../../context/ThemeContext/ThemeContext.js';
import { useSmartWallets } from '../../../utils/hooks/useSmartWallets/useSmartWallets.js';
import '../../../utils/hooks/useUserUpdateRequest/useUpdateUser/userFieldsSchema.js';
import { useMutation } from '../../../utils/hooks/useMutation/useMutation.js';
import { useIsTurnkeyWallet } from '../../../utils/hooks/useIsTurnkeyWallet/useIsTurnkeyWallet.js';
import 'bs58';
import '@dynamic-labs/types';
import '../../../context/SocialRedirectContext/SocialRedirectContext.js';
import '../../../context/LoadingContext/LoadingContext.js';
import '../../../context/WalletContext/WalletContext.js';
import { cleanupExport } from '../utils/turnkeyExport/turnkeyExport.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 { useWidgetContext } from '../../../widgets/DynamicWidget/context/DynamicWidgetContext.js';
import { Checkbox } from '../../../components/Checkbox/Checkbox.js';
import { ErrorContainer } from '../../../components/ErrorContainer/ErrorContainer.js';
import { IconButton } from '../../../components/IconButton/IconButton.js';
import { ModalHeader } from '../../../components/ModalHeader/ModalHeader.js';
import { TypographyButton } from '../../../components/TypographyButton/TypographyButton.js';
import { getProperErrorMessage } from '../../../modals/SignMessageConfirmationModal/getProperErrorMessage.js';
import '../../../components/MenuList/Dropdown/Dropdown.js';
import { Badge } from '../../../components/Badge/Badge.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 'qrcode';
import '../../../widgets/DynamicWidget/views/ReceiveWalletFunds/ReceiveWalletFunds.js';
import { NeedHelpSection } from '../../../components/NeedHelpSection/NeedHelpSection.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 { cleanupExport as cleanupExport$1 } from '../utils/waasExport/waasExport.js';
import '../../ExchangeWhitelistWarning/ExchangeWhitelistWarning.js';
import '../../../context/ErrorContext/hooks/useErrorText/useErrorText.js';
import '../../MfaChooseDeviceView/useGetMfaOptions/useGetMfaOptions.js';
import '../../../store/state/connectorsInitializing/connectorsInitializing.js';
import '../../../store/state/tokenBalances.js';
import '../../../store/state/multichainBalances.js';
import { useSyncEmbeddedWalletFlow } from '../../../utils/hooks/useSyncEmbeddedWalletFlow/useSyncEmbeddedWalletFlow.js';
import '../../../shared/utils/functions/getInitialUrl/getInitialUrl.js';
import { EmbeddedWalletExport } from '../../../components/EmbeddedWalletExport/EmbeddedWalletExport.js';
import '../../../components/InlineWidget/InlineWidget.js';
import '../../../components/IsBrowser/IsBrowser.js';
import '../../../components/Popper/Popper/Popper.js';
import '../../../components/Popper/PopperContext/PopperContext.js';

const EmbeddedRevealView = ({ exportPrivateKey, isPromptForExport = false, }) => {
    var _a, _b, _c;
    const { primaryWallet, user, setShowAuthFlow, environmentId } = useInternalDynamicContext();
    const { handleAcknowledgeExportPrompt } = useSyncEmbeddedWalletFlow();
    const { isTurnkeyWallet } = 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 } = useWidgetContext();
    const { pushView } = useViewContext();
    const { getEOAWallet, isSmartWallet } = useSmartWallets();
    const [acknowledgement1, setAcknowledgement1] = useState(false);
    const [iframeContainerRef, setIframeContainerRef] = useState(null);
    if (!primaryWallet) {
        throw new DynamicError('No primary wallet found', INVALID_WALLET_DATA);
    }
    let primaryTurnkeyWalletId;
    try {
        primaryTurnkeyWalletId = 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
    useEffect(() => {
        return () => {
            if (isTurnkeyWallet) {
                cleanupExport({
                    wallet: wallet,
                });
            }
            if (isWaasWallet) {
                cleanupExport$1({
                    wallet: wallet,
                });
            }
        };
        // only run once on unmount
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, []);
    const { mutate: handleExportWallet, isLoading, error, data, } = useMutation(() => exportWalletCredential({
        environmentId,
        exportPrivateKey,
        handleAcknowledgeExportPrompt,
        iframeContainerRef: iframeContainerRef,
        isTurnkeyWallet,
        isWaasWallet,
        user,
        wallet,
    }), {
        onFailure: (err) => {
            logger.error('Failed to export', err);
            dynamicEvents.emit('embeddedWalletRevealFailed', err);
        },
    });
    const errorText = useMemo(() => {
        if (!error) {
            return undefined;
        }
        if (error instanceof DynamicError) {
            return error.message;
        }
        try {
            return getProperErrorMessage(error, user);
        }
        catch (e) {
            if (e instanceof AccessBlockedError) {
                pushView('access-blocked');
            }
            return;
        }
    }, [error, pushView, user]);
    const { t } = useTranslation();
    const contentHeader = (jsxs("div", { children: [primaryWallet &&
                isSmartWallet(primaryWallet) &&
                !isLoading &&
                data &&
                !error && (jsxs("div", { className: 'embedded-reveal-view__zerodev-warning', children: [jsxs("div", { className: 'embedded-reveal-view__zerodev-warning__title-row', children: [jsx(SvgInfo, { className: 'embedded-reveal-view__zerodev-warning__icon' }), jsx(Typography, { variant: 'body_normal', weight: 'bold', copykey: 'dyn_embedded_reveal.aa_warning.title', children: t('dyn_embedded_reveal.aa_warning.title') })] }), jsxs(Typography, { variant: 'body_normal', weight: 'regular', copykey: 'dyn_embedded_reveal.aa_warning.subtitle', children: [t('dyn_embedded_reveal.aa_warning.subtitle'), jsx("button", { onClick: () => {
                                    setShowAuthFlow(false);
                                    setDynamicWidgetView('send-balance');
                                }, className: 'embedded-reveal-view__zerodev-warning__link-button', children: jsx(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') }) })] })] })), jsx("div", { className: 'embedded-reveal-view__body__description', children: !(!isLoading && data && !error) && (jsx("div", { className: 'embedded-reveal-view__header', children: jsx("div", { className: 'embedded-reveal-view__header__hero', children: jsx(SvgExportEmbeddedHero, {}) }) })) })] }));
    const closeButton = (jsx(IconButton, { onClick: () => {
            setShowAuthFlow(false);
        }, type: 'button', children: jsx(SvgClose, {}) }));
    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 (jsxs(Fragment, { children: [jsx(ModalHeader, { trailing: isPromptForExport ? null : closeButton, children: jsx(Typography, { as: 'h1', variant: 'title', color: 'primary', "data-testid": 'dynamic-auth-modal-heading', className: 'header__typography', children: title }) }), jsx("div", { className: 'embedded-reveal-view', children: jsxs("div", { className: getBodyClassName(), children: [(!isWaasWallet ||
                            !hasInjectedCredential ||
                            title !== credentialTitle) &&
                            contentHeader, (isTurnkeyWallet || isWaasWallet) && (jsx(EmbeddedWalletExport, { wallet: wallet, isTurnkeyWallet: isTurnkeyWallet, isWaasWallet: isWaasWallet, data: data, onIframeContainerRef: setIframeContainerRef, isVisible: !title.includes('Agree') })), hasInjectedCredential ? (jsxs(Fragment, { children: [jsx(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 && (jsx(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') }))] })) : (jsxs(Fragment, { children: [jsxs("div", { className: 'embedded-reveal-view__body__description', children: [jsx("div", { className: 'embedded-reveal-view__body__badge-container', children: jsx(Badge, { text: t('dyn_embedded_reveal.badge_label'), className: 'embedded-reveal-view__body__badge' }) }), jsx(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 && jsx(ErrorContainer, { children: errorText }), jsxs("div", { className: 'embedded-reveal-view__body__card', children: [jsxs("div", { className: 'embedded-reveal-view__body__card__statement', children: [jsx("div", { className: 'embedded-reveal-view__body__card__icon', children: jsx(SvgWalletV2, { height: 16, width: 16 }) }), jsxs("div", { className: 'embedded-reveal-view__body__card__statement__text', children: [jsxs(Typography, { variant: 'body_small', color: 'primary', copykey: 'dyn_embedded_reveal.statement_1.title', children: [t('dyn_embedded_reveal.statement_1.title'), ":"] }), jsx(Typography, { variant: 'body_small', color: 'primary', copykey: 'dyn_embedded_reveal.statement_1.description', children: t('dyn_embedded_reveal.statement_1.description') })] })] }), jsxs("div", { className: 'embedded-reveal-view__body__card__statement', children: [jsx("div", { className: 'embedded-reveal-view__body__card__icon', children: jsx(SvgEyeOffIcon, { height: 16, width: 16 }) }), jsx("div", { className: 'embedded-reveal-view__body__card__statement__text', children: jsx(Typography, { variant: 'body_small', color: 'primary', copykey: 'dyn_embedded_reveal.statement_2.title', children: t('dyn_embedded_reveal.statement_2.title') }) })] })] }), jsx("div", { className: 'embedded-reveal-view__body__confirm_card', children: jsxs("button", { className: 'embedded-reveal-view__body__card__acknowledgement', onClick: () => setAcknowledgement1(!acknowledgement1), children: [jsx("div", { children: jsx(Checkbox, { checked: acknowledgement1, onChange: () => setAcknowledgement1(!acknowledgement1), className: 'embedded-reveal-view__body__card__statement__checkbox', id: 'embedded-reveal-checkbox-1' }) }), jsx("div", { children: jsx(Typography, { variant: 'body_small', color: 'primary', style: { letterSpacing: '-0.15px' }, copykey: 'dyn_embedded_reveal.checkbox_label', children: t('dyn_embedded_reveal.checkbox_label') }) })] }) }), jsx(NeedHelpSection, { isExport: true }), jsxs("div", { className: 'embedded-reveal-view__body__button_section', children: [isPromptForExport && (jsx(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') })), jsx(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') })] })] }))] }) })] }));
};

export { EmbeddedRevealView };
