'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 walletBook = require('@dynamic-labs/wallet-book');
require('../../components/Accordion/components/AccordionItem/AccordionItem.cjs');
require('@dynamic-labs/iconic');
var ViewContext = require('../../context/ViewContext/ViewContext.cjs');
require('@dynamic-labs/wallet-connector-core');
var logger = require('../../shared/logger.cjs');
var localStorage = require('../../utils/constants/localStorage.cjs');
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');
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');
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');
require('../../context/AccessDeniedContext/AccessDeniedContext.cjs');
require('../../context/AccountExistsContext/AccountExistsContext.cjs');
require('../../context/UserWalletsContext/UserWalletsContext.cjs');
var getSiweStatement = require('../../utils/functions/getSiweStatement/getSiweStatement.cjs');
require('../../context/VerificationContext/VerificationContext.cjs');
require('react-dom');
require('../../utils/functions/compareChains/compareChains.cjs');
require('../Passkey/utils/findPrimaryEmbeddedChain/findPrimaryEmbeddedChain.cjs');
require('../../context/ThemeContext/ThemeContext.cjs');
require('../../utils/hooks/useUserUpdateRequest/useUpdateUser/userFieldsSchema.cjs');
require('bs58');
var usePromise = require('../../utils/hooks/usePromise/usePromise.cjs');
require('@dynamic-labs/types');
require('../../context/SocialRedirectContext/SocialRedirectContext.cjs');
var LoadingContext = require('../../context/LoadingContext/LoadingContext.cjs');
require('../../context/WalletContext/WalletContext.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 shortenWalletAddress = require('../../shared/utils/functions/shortenWalletAddress/shortenWalletAddress.cjs');
require('../../widgets/DynamicWidget/context/DynamicWidgetContext.cjs');
var Checkbox = require('../../components/Checkbox/Checkbox.cjs');
require('../../components/IconButton/IconButton.cjs');
var TypographyButton = require('../../components/TypographyButton/TypographyButton.cjs');
require('../../components/MenuList/Dropdown/Dropdown.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');
require('../../context/IpConfigurationContext/IpConfigurationContext.cjs');
require('../../context/ConnectWithOtpContext/ConnectWithOtpContext.cjs');
require('../../widgets/DynamicBridgeWidget/views/WalletsView/components/SecondaryWallets/SecondaryWallets.cjs');
require('@hcaptcha/react-hcaptcha');
require('../ExchangeWhitelistWarning/ExchangeWhitelistWarning.cjs');
require('../../context/ErrorContext/hooks/useErrorText/useErrorText.cjs');
require('../MfaChooseDeviceView/useGetMfaOptions/useGetMfaOptions.cjs');
var useTransferWallet = require('../../utils/hooks/useTransferWallet/useTransferWallet.cjs');
require('../../store/state/connectorsInitializing/connectorsInitializing.cjs');
require('../../store/state/tokenBalances.cjs');
require('../../store/state/multichainBalances.cjs');
require('../../shared/utils/functions/getInitialUrl/getInitialUrl.cjs');
require('../../components/InlineWidget/InlineWidget.cjs');
require('../../components/IsBrowser/IsBrowser.cjs');
require('../../components/Popper/Popper/Popper.cjs');
require('../../components/Popper/PopperContext/PopperContext.cjs');

const WalletUsedView = () => {
    const [isChecked, setIsChecked] = React.useState(false);
    const { t } = reactI18next.useTranslation();
    const { loading, setLoading } = LoadingContext.useLoadingContext();
    const { pushView } = ViewContext.useViewContext();
    const { setShowAuthFlow, appName, siweStatement, setMultiWalletWidgetState, connectWallet, selectedWalletConnector: walletConnector, isSingleWalletAccount, handleLogOut, } = useInternalDynamicContext.useInternalDynamicContext();
    const { data: walletAddress } = usePromise.usePromise(() => _tslib.__awaiter(void 0, void 0, void 0, function* () {
        const accounts = yield (walletConnector === null || walletConnector === void 0 ? void 0 : walletConnector.getConnectedAccounts());
        return accounts === null || accounts === void 0 ? void 0 : accounts[0];
    }), {
        deps: [walletConnector],
    });
    const shortenedWalletAddress = shortenWalletAddress.shortenWalletAddress(walletAddress);
    const completeWalletTransfer = useTransferWallet.useTransferWallet();
    const handleWalletTransfer = () => _tslib.__awaiter(void 0, void 0, void 0, function* () {
        if (!walletConnector)
            return;
        setMultiWalletWidgetState('awaiting_signature', undefined, 'transferring_wallet');
        setLoading(true);
        pushView('wallet-sign');
        const siweStatementToUse = getSiweStatement.getSiweStatement({ appName, siweStatement });
        // TODO refactor this block
        try {
            const connectionResult = yield connectWallet(walletConnector);
            if (!(connectionResult === null || connectionResult === void 0 ? void 0 : connectionResult.address)) {
                return;
            }
            // We need to set to LS when about to transfer phantom wallet on mobile
            // because the deeplink interrupts this code execution and then redirects back on a new tab.
            // This resets all state (except for LS obviously). When we receive the phantom signature in this new tab,
            // we can't tell whether it's supposed to be used to sign a message, verify a wallet sign in/link,
            // or conclude the transfer, which is why we need this LS variable: it will indicate whether it
            // is a signature, authentication or transfer.
            // Check method handleUserResponse in file useResponseHandlers, case 'signMessage'.
            if (walletConnector.key === 'phantom' && utils.isMobile()) {
                utils.StorageService.setItem(localStorage.PHANTOM_SIGNATURE_STATE, {
                    loseOriginalAccountOnTransfer: isChecked,
                    submittedAtTimestamp: new Date().toString(),
                    verificationType: 'awaiting_transfer',
                });
            }
            yield completeWalletTransfer({
                overrideSiweStatement: siweStatementToUse,
                publicWalletAddress: connectionResult.address,
                skipEmptyAccountCheck: isChecked,
                walletConnector,
            });
            setShowAuthFlow(false);
        }
        catch (error) {
            logger.logger.error(error);
            setShowAuthFlow(false, { emitCancelAuth: true });
        }
        finally {
            setLoading(false);
            setMultiWalletWidgetState('idle');
        }
    });
    return (jsxRuntime.jsxs("div", { className: 'wallet-used-view__container', children: [jsxRuntime.jsx("div", { className: 'wallet-used-view__icon', children: jsxRuntime.jsx(walletBook.WalletIcon, { icon: walletConnector === null || walletConnector === void 0 ? void 0 : walletConnector.metadata.icon, walletKey: walletConnector === null || walletConnector === void 0 ? void 0 : walletConnector.key, width: 64, height: 64 }) }), jsxRuntime.jsx(Typography.Typography, { className: 'wallet-used-view__shorten-wallet-address', variant: 'body_normal', weight: 'medium', color: 'primary', children: shortenedWalletAddress }), jsxRuntime.jsx(Typography.Typography, { className: 'wallet-used-view__copy', variant: 'body_normal', color: 'secondary', weight: 'regular', copykey: 'dyn_wallet_link.existent_account.warning', children: t('dyn_wallet_link.existent_account.warning') }), isSingleWalletAccount && (jsxRuntime.jsxs("label", { htmlFor: 'skipEmptyAccountCheck', className: 'wallet-used-view__checkbox-label', children: [jsxRuntime.jsx(Checkbox.Checkbox, { id: 'skipEmptyAccountCheck', className: 'wallet-used-view__checkbox-container', defaultChecked: isChecked, onChange: () => setIsChecked(!isChecked), ariaLabel: 'skip empty account check' }), jsxRuntime.jsx(Typography.Typography, { variant: 'body_small', color: 'secondary', copykey: 'dyn_wallet_link.existent_account.acceptance', children: t('dyn_wallet_link.existent_account.acceptance') })] })), jsxRuntime.jsx(TypographyButton.TypographyButton, { buttonClassName: 'wallet-used-view__accept-button', buttonVariant: 'primary', expanded: true, buttonPadding: 'large', onClick: () => handleWalletTransfer(), disabled: (!isChecked && isSingleWalletAccount) || loading, dataTestId: 'wallet-used-view-transfer-wallet', copykey: 'dyn_wallet_link.confirm_button', children: t('dyn_wallet_link.confirm_button') }), jsxRuntime.jsx(TypographyButton.TypographyButton, { dataTestId: 'wallet-used-view-log-out', buttonPadding: 'small', buttonClassName: 'wallet-used-view__logout', onClick: handleLogOut, copykey: 'dyn_wallet_link.log_out_button', children: t('dyn_wallet_link.log_out_button') })] }));
};

exports.WalletUsedView = WalletUsedView;
