'use client'
import { jsx, jsxs } from 'react/jsx-runtime';
import { useState, useCallback, useRef, useMemo } from 'react';
import '@dynamic-labs/utils';
import '../../../../../components/Accordion/components/AccordionItem/AccordionItem.js';
import 'react-i18next';
import { ReactComponent as SvgArrowDown } from '../../../../../shared/assets/arrow-down.js';
import '@dynamic-labs/iconic';
import '../../../../../context/ViewContext/ViewContext.js';
import '../../../../../../../_virtual/_tslib.js';
import { BaseButton } from '../../../../../components/BaseButton/BaseButton.js';
import '@dynamic-labs/wallet-connector-core';
import '../../../../../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 '../../../../../events/dynamicEvents.js';
import '../../../../../context/DynamicContext/DynamicContext.js';
import '../../../../../store/state/loadingAndLifecycle/loadingAndLifecycle.js';
import '../../../../../store/state/authMode/authMode.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 '@dynamic-labs/locale';
import '../../../../../store/state/dynamicContextProps/dynamicContextProps.js';
import '../../../../../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 '../../../../../utils/hooks/useUserUpdateRequest/useUpdateUser/userFieldsSchema.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 { 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 { Skeleton } from '../../../../../components/Skeleton/Skeleton.js';
import '../../../../../context/WalletGroupContext/WalletGroupContext.js';
import '../../DynamicWidgetHeader/DynamicWidgetHeader.js';
import 'react-focus-lock';
import { shortenWalletAddress } from '../../../../../shared/utils/functions/shortenWalletAddress/shortenWalletAddress.js';
import '../../../context/DynamicWidgetContext.js';
import '../../../../../components/IconButton/IconButton.js';
import { Dropdown } from '../../../../../components/MenuList/Dropdown/Dropdown.js';
import { DropdownMenu } from '../../../../../components/MenuList/DropdownMenu/DropdownMenu.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 '../../../../../context/OnrampContext/OnrampContext.js';
import '../../../../../../index.js';
import '../../../helpers/convertExchangeKeyAndProviderEnum.js';
import 'qrcode';
import '../../../views/ReceiveWalletFunds/ReceiveWalletFunds.js';
import { IconWithStatus } from '../../../../../components/IconWithStatus/IconWithStatus.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 { useWalletConnectorNetwork } from '../../../../../utils/hooks/useWalletConnectorNetwork/useWalletConnectorNetwork.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 '../../../../../store/state/connectorsInitializing/connectorsInitializing.js';

/**
 * If the wallet has no icon, returns its key, nicely formatted.
 */
const getNameIfNoIcon = (wallet) => {
    if (!wallet)
        return undefined;
    const noIcon = !wallet.connector.metadata.icon ||
        wallet.connector.metadata.icon.includes('unknown-wallet');
    return noIcon
        ? wallet.key
            .replace(/[-_]/g, ' ')
            .replace(/\b\w/g, (char) => char.toUpperCase())
        : undefined;
};
const ActiveWalletAddress = ({ wallet, nameServiceName, menuOption, fullWidth = false, isLoading = false, }) => {
    const { showAlert } = usePasskeyContext();
    const header = nameServiceName || getNameIfNoIcon(wallet);
    const { network } = useWalletConnectorNetwork(wallet === null || wallet === void 0 ? void 0 : wallet.connector);
    const [showMenu, setShowMenu] = useState(false);
    const handleMenuClose = useCallback(() => {
        setShowMenu(false);
    }, [setShowMenu]);
    const handleMenuOpen = useCallback(() => {
        // Use setTimeout here to prevent React 17 from triggering a click event
        // when the dropdown is rendered.
        setTimeout(() => setShowMenu(true), 0);
    }, [setShowMenu]);
    const iconRef = useRef(null);
    const { data: address } = usePromise(() => {
        // COSMOS wallet address changes based on which network is selected
        if ((wallet === null || wallet === void 0 ? void 0 : wallet.connector.connectedChain) === 'COSMOS') {
            return wallet === null || wallet === void 0 ? void 0 : wallet.connector.getAddress();
        }
        return wallet === null || wallet === void 0 ? void 0 : wallet.address;
    }, {
        deps: [wallet, network],
        enabled: Boolean(wallet),
    });
    const menuIcon = useMemo(() => (jsx("div", { ref: iconRef, className: 'active-wallet-information__handle-icon', children: showAlert() ? (jsx(IconWithStatus, { Icon: () => jsx(SvgArrowDown, { width: 24, height: 24 }), variant: 'yellow' })) : (jsx(SvgArrowDown, { width: 24, height: 24 })) })), [showAlert]);
    return (jsxs("div", { className: 'active-wallet-information__address-container', children: [jsxs(BaseButton, { onClick: handleMenuOpen, dataTestId: 'active-wallet-information-button', children: [jsxs("div", { className: 'active-wallet-information__handle-row', children: [jsx(Typography, { className: fullWidth
                                    ? 'active-wallet-information__handle--full-width'
                                    : 'active-wallet-information__handle', color: 'primary', variant: 'body_normal', "data-testid": 'header-text', children: header }), header && menuIcon] }), jsxs("div", { className: 'active-wallet-information__handle-row', children: [jsx(Typography, { className: 'active-wallet-information__address', weight: header ? 'regular' : 'medium', variant: header ? 'body_small' : 'body_normal', color: header ? 'secondary' : 'primary', children: isLoading ? (jsx(Skeleton, { className: 'active-wallet-information__address-skeleton' })) : (shortenWalletAddress(address)) }), !header && menuIcon] })] }), jsx(Dropdown, { isOpen: showMenu, onClickOutside: handleMenuClose, onScroll: handleMenuClose, anchorRef: iconRef, anchorOrigin: 'bottom-right', transformOrigin: 'top-left', children: jsx(DropdownMenu, { options: menuOption, setShowMenu: setShowMenu }) })] }));
};

export { ActiveWalletAddress, getNameIfNoIcon };
