'use client'
'use strict';

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

var _tslib = require('../../../../_virtual/_tslib.cjs');
var jsxRuntime = require('react/jsx-runtime');
var React = require('react');
var reactI18next = require('react-i18next');
var utils = require('@dynamic-labs/utils');
var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
var Badge = require('../../components/Badge/Badge.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 NeedHelpSection = require('../../components/NeedHelpSection/NeedHelpSection.cjs');
var Typography = require('../../components/Typography/Typography.cjs');
var TypographyButton = require('../../components/TypographyButton/TypographyButton.cjs');
require('../../context/DynamicContext/useDynamicContext/useDynamicContext.cjs');
var useInternalDynamicContext = require('../../context/DynamicContext/useDynamicContext/useInternalDynamicContext/useInternalDynamicContext.cjs');
var ViewContext = require('../../context/ViewContext/ViewContext.cjs');
var arrowLeft = require('../../shared/assets/arrow-left.cjs');
var close = require('../../shared/assets/close.cjs');
var exportEmbeddedHero = require('../../shared/assets/export-embedded-hero.cjs');
var filledQuestionMark = require('../../shared/assets/filled-question-mark.cjs');
require('@dynamic-labs/iconic');
var logger = require('../../shared/logger.cjs');
var useSmartWallets = require('../../utils/hooks/useSmartWallets/useSmartWallets.cjs');
require('@dynamic-labs/sdk-api-core');
require('@dynamic-labs-sdk/client/core');
require('eventemitter3');
require('@dynamic-labs-sdk/client');
require('../../config/ApiEndpoint.cjs');
require('@dynamic-labs/wallet-book');
require('../../utils/constants/colors.cjs');
require('../../utils/constants/values.cjs');
require('../../shared/consts/index.cjs');
require('@dynamic-labs/multi-wallet');
require('react-international-phone');
require('../../store/state/nonce/nonce.cjs');
require('@dynamic-labs-wallet/browser-wallet-client');
require('@dynamic-labs/locale');
require('../../store/state/dynamicContextProps/dynamicContextProps.cjs');
require('../../store/state/primaryWalletId/primaryWalletId.cjs');
require('../../store/state/connectedWalletsInfo/connectedWalletsInfo.cjs');
require('../../utils/functions/getWaasAddressTypeLabel/getWaasAddressTypeLabel.cjs');
require('../../events/dynamicEvents.cjs');
var utils$1 = require('../../utils/hooks/useTransactionSimulation/utils/utils.cjs');
require('bs58');

const parseThresholdScheme = (scheme) => {
    if (!scheme)
        return { required: 2, total: 3 };
    const schemeMap = {
        THREE_OF_THREE: { required: 3, total: 3 },
        TWO_OF_THREE: { required: 2, total: 3 },
        TWO_OF_TWO: { required: 2, total: 2 },
    };
    return schemeMap[scheme] || { required: 2, total: 3 };
};
const ExportSharesView = ({ passcode }) => {
    const { t } = reactI18next.useTranslation();
    const { primaryWallet, setShowAuthFlow, user } = useInternalDynamicContext.useInternalDynamicContext();
    const { getEOAWallet } = useSmartWallets.useSmartWallets();
    const { goBack, canGoBack } = ViewContext.useViewContext();
    const [isLoading, setIsLoading] = React.useState(false);
    const [error, setError] = React.useState();
    const [isAcknowledged, setIsAcknowledged] = React.useState(false);
    const eoaWallet = primaryWallet && getEOAWallet(primaryWallet);
    const wallet = eoaWallet !== null && eoaWallet !== void 0 ? eoaWallet : primaryWallet;
    const walletCredential = React.useMemo(() => {
        var _a;
        if (!user || !primaryWallet)
            return undefined;
        return (_a = user.verifiedCredentials) === null || _a === void 0 ? void 0 : _a.find((cred) => cred.walletName === 'dynamicwaas' &&
            cred.address === primaryWallet.address);
    }, [user, primaryWallet]);
    const thresholdInfo = React.useMemo(() => {
        var _a;
        const scheme = (_a = walletCredential === null || walletCredential === void 0 ? void 0 : walletCredential.walletProperties) === null || _a === void 0 ? void 0 : _a.thresholdSignatureScheme;
        return parseThresholdScheme(scheme);
    }, [walletCredential]);
    const handleExportShares = React.useCallback(() => _tslib.__awaiter(void 0, void 0, void 0, function* () {
        setError(undefined);
        setIsLoading(true);
        try {
            if (!wallet) {
                throw new utils.DynamicError('No wallet found', 'no_wallet_found');
            }
            if (walletConnectorCore.isDynamicWaasConnector(wallet.connector)) {
                yield wallet.connector.exportClientKeyshares({
                    accountAddress: wallet.address,
                    password: passcode,
                });
                setShowAuthFlow(false);
                return;
            }
            if (utils$1.isZeroDevConnector(wallet.connector)) {
                const { eoaConnector } = wallet.connector;
                if (eoaConnector && walletConnectorCore.isDynamicWaasConnector(eoaConnector)) {
                    yield eoaConnector.exportClientKeyshares({
                        accountAddress: wallet.address,
                        password: passcode,
                    });
                    setShowAuthFlow(false);
                    return;
                }
            }
            throw new utils.DynamicError('Not a supported connector type for export shares', 'unsupported_connector_for_export');
        }
        catch (err) {
            logger.logger.error('Error exporting shares', { error: err });
            setError(t('dyn_export_shares.error'));
        }
        finally {
            setIsLoading(false);
        }
    }), [wallet, passcode, setShowAuthFlow, t]);
    const bulletPoints = [
        { key: 'dyn_export_shares.bullet_1' },
        {
            key: 'dyn_export_shares.bullet_2',
            values: { required: thresholdInfo.required },
        },
        { key: 'dyn_export_shares.bullet_3' },
    ];
    const backButton = canGoBack && (jsxRuntime.jsx(IconButton.IconButton, { onClick: goBack, type: 'button', children: jsxRuntime.jsx(arrowLeft.ReactComponent, {}) }));
    const closeButton = (jsxRuntime.jsx(IconButton.IconButton, { onClick: () => {
            setShowAuthFlow(false);
        }, type: 'button', children: jsxRuntime.jsx(close.ReactComponent, {}) }));
    return (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx(ModalHeader.ModalHeader, { leading: backButton, trailing: closeButton, children: jsxRuntime.jsx(Typography.Typography, { as: 'h1', variant: 'title', color: 'primary', "data-testid": 'dynamic-auth-modal-heading', className: 'header__typography', children: t('dyn_export_shares.title') }) }), jsxRuntime.jsx("div", { className: 'embedded-reveal-view', children: jsxRuntime.jsxs("div", { className: 'embedded-reveal-view__body', children: [jsxRuntime.jsx("div", { className: 'embedded-reveal-view__body__description', children: jsxRuntime.jsx("div", { className: 'embedded-reveal-view__header', children: jsxRuntime.jsx("div", { className: 'embedded-reveal-view__header__hero', children: jsxRuntime.jsx(exportEmbeddedHero.ReactComponent, {}) }) }) }), 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_export_shares.sensitive_info'), className: 'embedded-reveal-view__body__badge' }) }), jsxRuntime.jsx(Typography.Typography, { variant: 'body_normal', color: 'primary', weight: 'regular', copykey: 'dyn_export_shares.threshold_description', children: t('dyn_export_shares.threshold_description', {
                                        required: thresholdInfo.required,
                                        total: thresholdInfo.total,
                                    }) })] }), error && jsxRuntime.jsx(ErrorContainer.ErrorContainer, { children: error }), 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(filledQuestionMark.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', weight: 'medium', copykey: 'dyn_export_shares.how_to_use_title', children: t('dyn_export_shares.how_to_use_title') }) })] }), jsxRuntime.jsx("ul", { className: 'embedded-reveal-view__body__bullet-list', children: bulletPoints.map((bullet) => (jsxRuntime.jsx("li", { children: jsxRuntime.jsx(Typography.Typography, { variant: 'body_small', color: 'primary', copykey: bullet.key, children: t(bullet.key, bullet.values) }) }, bullet.key))) })] }), jsxRuntime.jsx("div", { className: 'embedded-reveal-view__body__confirm_card', children: jsxRuntime.jsxs("button", { className: 'embedded-reveal-view__body__card__acknowledgement', onClick: () => setIsAcknowledged(!isAcknowledged), children: [jsxRuntime.jsx("div", { children: jsxRuntime.jsx(Checkbox.Checkbox, { checked: isAcknowledged, onChange: () => setIsAcknowledged(!isAcknowledged), className: 'embedded-reveal-view__body__card__statement__checkbox', id: 'export-shares-checkbox' }) }), jsxRuntime.jsx("div", { children: jsxRuntime.jsx(Typography.Typography, { variant: 'body_small', color: 'primary', style: { letterSpacing: '-0.15px' }, copykey: 'dyn_export_shares.acknowledge', children: t('dyn_export_shares.acknowledge') }) })] }) }), jsxRuntime.jsx(NeedHelpSection.NeedHelpSection, { isExport: true }), jsxRuntime.jsx("div", { className: 'embedded-reveal-view__body__button_section', children: jsxRuntime.jsx(TypographyButton.TypographyButton, { buttonPadding: 'medium', buttonVariant: 'brand-primary', typographyProps: {
                                    color: 'inherit',
                                }, onClick: handleExportShares, disabled: !isAcknowledged, loading: isLoading, dataTestId: 'export-shares-submit-button', copykey: 'dyn_export_shares.download_button', style: { width: '100%' }, className: 'embedded-reveal-view__body__button', expanded: true, children: t('dyn_export_shares.download_button') }) })] }) })] }));
};

exports.ExportSharesView = ExportSharesView;
