'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 sdkApiCore = require('@dynamic-labs/sdk-api-core');
var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
var CopyButton = require('../../../../components/CopyButton/CopyButton.cjs');
var IconButton = require('../../../../components/IconButton/IconButton.cjs');
var ModalHeader = require('../../../../components/ModalHeader/ModalHeader.cjs');
var QRCode = require('../../../../components/QRCode/QRCode.cjs');
var Typography = require('../../../../components/Typography/Typography.cjs');
require('../../../../context/DynamicContext/useDynamicContext/useDynamicContext.cjs');
var useInternalDynamicContext = require('../../../../context/DynamicContext/useDynamicContext/useInternalDynamicContext/useInternalDynamicContext.cjs');
var arrowLeft = require('../../../../shared/assets/arrow-left.cjs');
var copy = require('../../../../shared/assets/copy.cjs');
var embeddedWalletIcon = require('../../../../shared/assets/embedded-wallet-icon.cjs');
require('@dynamic-labs/iconic');
require('../../../../context/ViewContext/ViewContext.cjs');
var shortenWalletAddress = require('../../../../shared/utils/functions/shortenWalletAddress/shortenWalletAddress.cjs');
var classNames = require('../../../../utils/functions/classNames/classNames.cjs');
var getEmbeddedBitcoinActiveAddress = require('../../../../utils/functions/getEmbeddedBitcoinActiveAddress/getEmbeddedBitcoinActiveAddress.cjs');
var getWalletAdditionalAddressByType = require('../../../../utils/functions/getWalletAdditionalAddressByType/getWalletAdditionalAddressByType.cjs');
var usePromise = require('../../../../utils/hooks/usePromise/usePromise.cjs');
var DynamicWidgetContext = require('../../context/DynamicWidgetContext.cjs');
var useFetchChain = require('../../hooks/useFetchChain/useFetchChain.cjs');

const DepositView = () => {
    var _a, _b;
    const { t } = reactI18next.useTranslation();
    // Per-network address: embedded Bitcoin swaps its active address on network
    // switch (context `network` changes re-render this view).
    const { primaryWallet, getNameService, network } = useInternalDynamicContext.useInternalDynamicContext();
    const walletConnector = primaryWallet === null || primaryWallet === void 0 ? void 0 : primaryWallet.connector;
    const { chain } = useFetchChain.useFetchChain(walletConnector);
    const { setDynamicWidgetView } = DynamicWidgetContext.useWidgetContext();
    const [midnightAddressType, setMidnightAddressType] = React.useState('unshielded');
    const { data: ens } = usePromise.usePromise(() => getNameService(primaryWallet === null || primaryWallet === void 0 ? void 0 : primaryWallet.address), { deps: [primaryWallet === null || primaryWallet === void 0 ? void 0 : primaryWallet.address, network], initialData: undefined });
    const ensName = (_a = ens === null || ens === void 0 ? void 0 : ens.name) !== null && _a !== void 0 ? _a : null;
    if (!primaryWallet) {
        return null;
    }
    // Midnight is one wallet exposing a per-network address for each type. The
    // connector resolves the selected network so the deposit QR/address matches
    // the chosen network — depositing to the wrong network's encoding would
    // strand funds. `network` from context drives re-render on switch.
    const isMidnight = walletConnectorCore.isMidnightWallet(primaryWallet);
    const midnightNetworkTag = walletConnectorCore.getMidnightNetworkTag(primaryWallet.connector);
    const shieldedAddress = isMidnight
        ? getWalletAdditionalAddressByType.getWalletAdditionalAddressByType(sdkApiCore.WalletAddressType.MidnightShielded, primaryWallet, midnightNetworkTag)
        : undefined;
    const unshieldedAddress = isMidnight
        ? (_b = getWalletAdditionalAddressByType.getWalletAdditionalAddressByType(sdkApiCore.WalletAddressType.MidnightUnshielded, primaryWallet, midnightNetworkTag)) !== null && _b !== void 0 ? _b : primaryWallet.address
        : primaryWallet.address;
    const showMidnightToggle = Boolean(shieldedAddress);
    // Per-network address: the deposit QR/address must match the selected
    // network — depositing to the wrong network's encoding would strand funds.
    const bitcoinActiveAddress = getEmbeddedBitcoinActiveAddress.getEmbeddedBitcoinActiveAddress(primaryWallet.connector);
    const depositAddress = showMidnightToggle && midnightAddressType === 'shielded' && shieldedAddress
        ? shieldedAddress
        : bitcoinActiveAddress !== null && bitcoinActiveAddress !== void 0 ? bitcoinActiveAddress : unshieldedAddress;
    const backButton = (jsxRuntime.jsx(IconButton.IconButton, { type: 'button', id: 'back-button', "data-testid": 'back-button', onClick: () => setDynamicWidgetView('choose-wallet-funding-method'), children: jsxRuntime.jsx(arrowLeft.ReactComponent, {}) }));
    return (jsxRuntime.jsxs("div", { className: 'deposit-view', children: [jsxRuntime.jsx(ModalHeader.ModalHeader, { leading: backButton, children: jsxRuntime.jsx(Typography.Typography, { variant: 'title', copykey: 'dyn_deposit.title', children: t('dyn_deposit.title') }) }), jsxRuntime.jsxs("div", { className: 'deposit-view__content', children: [showMidnightToggle && (jsxRuntime.jsxs("div", { className: 'deposit-view__content__toggle', children: [jsxRuntime.jsx("button", { type: 'button', "data-testid": 'deposit-toggle-unshielded', className: classNames.classNames('deposit-view__content__toggle__option', {
                                    'deposit-view__content__toggle__option--active': midnightAddressType === 'unshielded',
                                }), "data-copykey": 'dyn_active_wallet_info.midnight_unshielded_address', onClick: () => setMidnightAddressType('unshielded'), children: t('dyn_active_wallet_info.midnight_unshielded_address') }), jsxRuntime.jsx("button", { type: 'button', "data-testid": 'deposit-toggle-shielded', className: classNames.classNames('deposit-view__content__toggle__option', {
                                    'deposit-view__content__toggle__option--active': midnightAddressType === 'shielded',
                                }), "data-copykey": 'dyn_active_wallet_info.midnight_shielded_address', onClick: () => setMidnightAddressType('shielded'), children: t('dyn_active_wallet_info.midnight_shielded_address') })] })), jsxRuntime.jsx(QRCode.QRCode, { className: 'deposit-view__content__qr-code', value: depositAddress, Icon: jsxRuntime.jsx(embeddedWalletIcon.ReactComponent, {}), logoSize: 40, logoMargin: 64, size: 215 }), jsxRuntime.jsxs("div", { className: 'deposit-view__content__address', children: [ensName && (jsxRuntime.jsx(Typography.Typography, { className: 'deposit-view__content__address__ens', variant: 'title', children: ensName })), jsxRuntime.jsxs("div", { className: 'deposit-view__content__address__wrapper', children: [jsxRuntime.jsx(Typography.Typography, { "data-testid": 'deposit-view-address', variant: 'title', weight: 'medium', className: 'deposit-view__content__address__wrapper__text', children: shortenWalletAddress.shortenWalletAddress(depositAddress) }), jsxRuntime.jsx(CopyButton.CopyButton, { variant: 'text', textToCopy: depositAddress, className: 'deposit-view__content__address__wrapper__copy', children: jsxRuntime.jsx(copy.ReactComponent, { "data-testid": 'copy-address' }) })] })] }), jsxRuntime.jsxs(Typography.Typography, { className: 'deposit-view__content__network', variant: 'body_small', color: 'secondary', copykey: 'dyn_deposit.receive_funds_on', children: [t('dyn_deposit.receive_funds_on'), chain === null || chain === void 0 ? void 0 : chain.name] })] })] }));
};

exports.DepositView = DepositView;
