'use client'
import { __awaiter } from '../../../../../../_virtual/_tslib.js';
import { jsx, jsxs } from 'react/jsx-runtime';
import { useState, useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import { isBitcoinConnector } from '@dynamic-labs/wallet-connector-core';
import { classNames } from '../../../../utils/functions/classNames/classNames.js';
import '../../../../components/Accordion/components/AccordionItem/AccordionItem.js';
import { ReactComponent as SvgCopy } from '../../../../shared/assets/copy.js';
import { ReactComponent as SvgEyeOutline } from '../../../../shared/assets/eye-outline.js';
import { ReactComponent as SvgKey } from '../../../../shared/assets/key.js';
import { ReactComponent as SvgUnlink } from '../../../../shared/assets/unlink.js';
import { ReactComponent as SvgWalletV2 } from '../../../../shared/assets/wallet-v2.js';
import '@dynamic-labs/iconic';
import { useViewContext } from '../../../../context/ViewContext/ViewContext.js';
import { logger } from '../../../../shared/logger.js';
import '@dynamic-labs/wallet-book';
import '@dynamic-labs/utils';
import '../../../../utils/constants/colors.js';
import '../../../../utils/constants/values.js';
import '@dynamic-labs/sdk-api-core';
import '../../../../shared/consts/index.js';
import { Alert } from '../../../../components/Alert/Alert.js';
import '../../../../events/dynamicEvents.js';
import '../../../../context/DynamicContext/DynamicContext.js';
import '../../../../store/state/loadingAndLifecycle/loadingAndLifecycle.js';
import { useAuthMode } from '../../../../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 '@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 { useProjectSettings } from '../../../../client/extension/projectSettings/useProjectSettings/useProjectSettings.js';
import '@dynamic-labs/locale';
import '../../../../store/state/dynamicContextProps/dynamicContextProps.js';
import { getPrimaryWalletId } from '../../../../store/state/primaryWalletId/primaryWalletId.js';
import '../../../../store/state/connectedWalletsInfo/connectedWalletsInfo.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 '../../../../views/Passkey/utils/findPrimaryEmbeddedChain/findPrimaryEmbeddedChain.js';
import '../../../../context/ThemeContext/ThemeContext.js';
import { useSmartWallets } from '../../../../utils/hooks/useSmartWallets/useSmartWallets.js';
import '../../../../utils/hooks/useUserUpdateRequest/useUpdateUser/userFieldsSchema.js';
import { useIsTurnkeyWallet } from '../../../../utils/hooks/useIsTurnkeyWallet/useIsTurnkeyWallet.js';
import 'bs58';
import { usePromise } from '../../../../utils/hooks/usePromise/usePromise.js';
import '@dynamic-labs/types';
import '../../../../context/SocialRedirectContext/SocialRedirectContext.js';
import '../../../../context/LoadingContext/LoadingContext.js';
import '../../../../context/WalletContext/WalletContext.js';
import '../../../../utils/hooks/useEmbeddedWallet/useSecureEnclaveEmbeddedWallet/constants.js';
import 'yup';
import '../../../../context/MockContext/MockContext.js';
import '../../../../views/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 '../../../../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 { Skeleton } from '../../../../components/Skeleton/Skeleton.js';
import '../../../../context/WalletGroupContext/WalletGroupContext.js';
import '../DynamicWidgetHeader/DynamicWidgetHeader.js';
import 'react-focus-lock';
import { AuthProviderIcon } from '../../../../components/AuthProviderIcon/AuthProviderIcon.js';
import { getExportKeysOption } from '../../helpers/helpers.js';
import { useWidgetContext } from '../../context/DynamicWidgetContext.js';
import '../../../../components/IconButton/IconButton.js';
import '../../../../components/MenuList/Dropdown/Dropdown.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 '../../../../views/TransactionConfirmationView/TransactionConfirmationView.js';
import '../../../../components/PasskeyCreatedSuccessBanner/PasskeyCreatedSuccessBanner.js';
import { usePasskeyContext } from '../../../../context/PasskeyContext/PasskeyContext.js';
import '../PasskeyCard/PasskeyCard.js';
import '../../views/CryptoComOnramp/CryptoComOnramp.js';
import { useFetchNameService } from '../../hooks/useFetchNameService/useFetchNameService.js';
import '../../../../context/OnrampContext/OnrampContext.js';
import '../../../../../index.js';
import '../../helpers/convertExchangeKeyAndProviderEnum.js';
import 'qrcode';
import '../../views/ReceiveWalletFunds/ReceiveWalletFunds.js';
import { NetworkPicker } from '../../../../components/NetworkPicker/NetworkPicker.js';
import { StatusDot } from '../../../../components/StatusDot/StatusDot.js';
import '../../../../context/IpConfigurationContext/IpConfigurationContext.js';
import '../../../../context/ConnectWithOtpContext/ConnectWithOtpContext.js';
import '../../../DynamicBridgeWidget/views/WalletsView/components/SecondaryWallets/SecondaryWallets.js';
import '@hcaptcha/react-hcaptcha';
import '../../../../views/ExchangeWhitelistWarning/ExchangeWhitelistWarning.js';
import '../../../../context/ErrorContext/hooks/useErrorText/useErrorText.js';
import '../../../../views/MfaChooseDeviceView/useGetMfaOptions/useGetMfaOptions.js';
import '../../../../store/state/connectorsInitializing/connectorsInitializing.js';
import '../../../../store/state/tokenBalances.js';
import '../../../../store/state/multichainBalances.js';
import '../../../../shared/utils/functions/getInitialUrl/getInitialUrl.js';
import '../../../../components/InlineWidget/InlineWidget.js';
import '../../../../components/IsBrowser/IsBrowser.js';
import '../../../../components/Popper/Popper/Popper.js';
import '../../../../components/Popper/PopperContext/PopperContext.js';
import { ActiveWalletBalance } from '../ActiveWalletBalance/ActiveWalletBalance.js';
import { WalletDetailsCard } from '../WalletDetailsCard/WalletDetailsCard.js';
import { ActiveBitcoinWalletAddresses } from './ActiveBitcoinWalletAddresses/ActiveBitcoinWalletAddresses.js';
import { ActiveWalletAddress } from './ActiveWalletAddress/ActiveWalletAddress.js';

const ICON_SIZE = 32;
const DROPDOWN_ICON_SIZE = 16;
const ActiveWalletInformation = ({ isLoading = false, }) => {
    var _a, _b, _c;
    const { t } = useTranslation();
    /** Controls the [WalletDetailsCard] open state */
    const [detailsCardOpen, setDetailsCardOpen] = useState(false);
    const { primaryWallet, network, user, removeWallet } = useInternalDynamicContext();
    const { clearStackAndPushInitialView } = useViewContext();
    const authMode = useAuthMode();
    const projectSettings = useProjectSettings();
    const { setDynamicWidgetView, headerAlert } = useWidgetContext();
    const { isTurnkeyWalletWithoutAuthenticator } = useIsTurnkeyWallet();
    const address = primaryWallet === null || primaryWallet === void 0 ? void 0 : primaryWallet.address;
    const nameService = useFetchNameService(primaryWallet !== null && primaryWallet !== void 0 ? primaryWallet : undefined);
    const { getEOAWallet } = useSmartWallets();
    const { data: testnet } = usePromise(() => __awaiter(void 0, void 0, void 0, function* () { return Boolean(yield (primaryWallet === null || primaryWallet === void 0 ? void 0 : primaryWallet.connector.isTestnet())); }), { deps: [network], initialData: false });
    const exportKeysOption = getExportKeysOption(primaryWallet === null || primaryWallet === void 0 ? void 0 : primaryWallet.connector, projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.providers);
    const { showAlert } = usePasskeyContext();
    const wallet = (_a = (primaryWallet && getEOAWallet(primaryWallet))) !== null && _a !== void 0 ? _a : primaryWallet;
    const isBitcoin = primaryWallet && isBitcoinConnector(primaryWallet === null || primaryWallet === void 0 ? void 0 : primaryWallet.connector);
    const isEmbeddedWallet = Boolean(wallet && ((_c = (_b = wallet === null || wallet === void 0 ? void 0 : wallet.connector) === null || _b === void 0 ? void 0 : _b.key) === null || _c === void 0 ? void 0 : _c.startsWith('turnkey')));
    const openDetailsCard = useCallback(() => {
        setTimeout(() => setDetailsCardOpen(true), 0);
    }, []);
    const removePrimaryWallet = useCallback(() => {
        const primaryWalletId = getPrimaryWalletId();
        if (primaryWalletId) {
            removeWallet(primaryWalletId, clearStackAndPushInitialView);
        }
    }, [removeWallet, clearStackAndPushInitialView]);
    const options = [
        {
            Icon: jsx(SvgCopy, { width: DROPDOWN_ICON_SIZE, height: DROPDOWN_ICON_SIZE }),
            callback: () => (nameService === null || nameService === void 0 ? void 0 : nameService.name) && navigator.clipboard.writeText(nameService === null || nameService === void 0 ? void 0 : nameService.name),
            copykey: 'dyn_active_wallet_info.options.copy_ens',
            hide: !(nameService === null || nameService === void 0 ? void 0 : nameService.name),
            text: t('dyn_active_wallet_info.options.copy_ens'),
        },
        {
            Icon: (jsx(SvgEyeOutline, { width: DROPDOWN_ICON_SIZE, height: DROPDOWN_ICON_SIZE })),
            callback: openDetailsCard,
            copykey: 'dyn_active_wallet_info.options.view_address',
            hide: !address,
            text: t('dyn_active_wallet_info.options.view_address'),
        },
        {
            Icon: (jsx(SvgUnlink, { width: DROPDOWN_ICON_SIZE, height: DROPDOWN_ICON_SIZE })),
            callback: removePrimaryWallet,
            copykey: 'dyn_active_wallet_info.options.disconnect',
            hide: !address || authMode !== 'connect-only',
            text: t('dyn_active_wallet_info.options.disconnect'),
        },
        {
            Icon: (jsx(SvgWalletV2, { width: DROPDOWN_ICON_SIZE, height: DROPDOWN_ICON_SIZE })),
            callback: () => setDynamicWidgetView('settings'),
            copykey: 'dyn_active_wallet_info.options.settings',
            endSlot: showAlert() ? jsx(StatusDot, { variant: 'yellow' }) : undefined,
            hide: !isEmbeddedWallet,
            text: t('dyn_active_wallet_info.options.settings'),
        },
        {
            Icon: (jsx(SvgKey, { width: DROPDOWN_ICON_SIZE, height: DROPDOWN_ICON_SIZE })),
            callback: exportKeysOption ||
                (() => logger.info('No option to export private keys')),
            copykey: 'dyn_active_wallet_info.options.export',
            hide: !exportKeysOption,
            text: t('dyn_active_wallet_info.options.export'),
        },
    ];
    const showTestnetAlert = testnet && !isTurnkeyWalletWithoutAuthenticator;
    const shouldHideNetwork = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.sdk.hideNetworkInDynamicWidget;
    const activeWalletAddress = () => {
        if (isBitcoin) {
            return (jsx(ActiveBitcoinWalletAddresses, { wallet: primaryWallet, menuOption: options }));
        }
        else {
            return (jsx(ActiveWalletAddress, { wallet: primaryWallet, nameServiceName: (nameService === null || nameService === void 0 ? void 0 : nameService.name) || (user === null || user === void 0 ? void 0 : user.email), menuOption: options, isLoading: isLoading, fullWidth: shouldHideNetwork }));
        }
    };
    if (isLoading) {
        return (jsxs("div", { className: 'active-wallet-information-container active-wallet-information-container--loading', "data-testid": 'active-wallet-information', children: [jsx("div", { className: 'active-wallet-information active-wallet-information--loading', children: jsxs("div", { className: 'active-wallet-information__header', children: [jsx("div", { className: 'active-wallet-information__avatar', children: jsx("div", { className: 'active-wallet-information__wallet-img', children: jsx(Skeleton, { className: 'active-wallet-information__skeleton-avatar' }) }) }), jsxs("div", { className: 'active-wallet-information__details', children: [jsxs("div", { className: 'active-wallet-information__addresses-container active-wallet-information__addresses-container--loading', children: [jsx("div", { className: 'active-wallet-information__handle-row', children: jsx(Skeleton, { className: 'active-wallet-information__skeleton-handle' }) }), jsx("div", { className: 'active-wallet-information__handle-row', children: jsx(Skeleton, { className: 'active-wallet-information__skeleton-address' }) })] }), !shouldHideNetwork && (jsx("div", { className: 'active-wallet-information__network-picker', children: jsx(Skeleton, { className: 'active-wallet-information__skeleton-network' }) }))] })] }) }), jsx(ActiveWalletBalance, { isLoading: isLoading })] }));
    }
    return (jsxs("div", { className: 'active-wallet-information-container', "data-testid": 'active-wallet-information', children: [jsxs("div", { className: 'active-wallet-information', children: [jsxs("div", { className: 'active-wallet-information__header', children: [jsx("div", { className: 'active-wallet-information__avatar', children: (nameService === null || nameService === void 0 ? void 0 : nameService.avatar) ? (jsx("div", { className: 'active-wallet-information__avatar__name-service', children: jsx("img", { src: nameService === null || nameService === void 0 ? void 0 : nameService.avatar, alt: '' }) })) : (jsx("div", { className: 'active-wallet-information__wallet-img', children: jsx(AuthProviderIcon, { iconSize: ICON_SIZE }) })) }), jsxs("div", { className: 'active-wallet-information__details', children: [jsx("div", { className: classNames('active-wallet-information__addresses-container', {
                                            'active-wallet-information__addresses-container--centered': !(nameService === null || nameService === void 0 ? void 0 : nameService.name),
                                        }), children: activeWalletAddress() }), address && (jsx(WalletDetailsCard, { open: detailsCardOpen, address: address, onClose: () => setDetailsCardOpen(false) })), !shouldHideNetwork && (jsx(NetworkPicker, { currentNetwork: network, connector: primaryWallet === null || primaryWallet === void 0 ? void 0 : primaryWallet.connector, mainClassName: 'active-wallet-information__network-picker', buttonClassName: 'active-wallet-information__network-picker-button', showNetworkName: true, isLoading: isLoading }))] })] }), headerAlert && (jsx(Alert, { variant: headerAlert.variant, children: headerAlert.content })), showTestnetAlert && (jsx(Alert, { variant: 'warning', copykey: 'dyn_active_wallet_info.testnet_warning', children: t('dyn_active_wallet_info.testnet_warning') }))] }), jsx(ActiveWalletBalance, {})] }));
};

export { ActiveWalletInformation };
