'use client'
'use strict';

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

var jsxRuntime = require('react/jsx-runtime');
var React = require('react');
var reactDom = require('react-dom');
require('@dynamic-labs/iconic');
require('@dynamic-labs/wallet-connector-core');
var tooltipArrow = require('../../shared/assets/tooltip-arrow.cjs');
require('../../context/ViewContext/ViewContext.cjs');
require('../../shared/logger.cjs');
require('@dynamic-labs/wallet-book');
var debounce = require('../../shared/utils/functions/debounce/debounce.cjs');
require('@dynamic-labs/utils');
require('../../utils/constants/colors.cjs');
require('../../utils/constants/values.cjs');
require('@dynamic-labs/sdk-api-core');
var index = require('../../shared/consts/index.cjs');
var classNames = require('../../utils/functions/classNames/classNames.cjs');
require('../../events/dynamicEvents.cjs');
require('../../../../_virtual/_tslib.cjs');
require('../../context/DynamicContext/DynamicContext.cjs');
require('../../store/state/loadingAndLifecycle/loadingAndLifecycle.cjs');
require('../../store/state/authMode/authMode.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');
require('../../context/VerificationContext/VerificationContext.cjs');
var useElementById = require('../../utils/hooks/useElementById/useElementById.cjs');
require('../../utils/functions/compareChains/compareChains.cjs');
require('../../views/Passkey/utils/findPrimaryEmbeddedChain/findPrimaryEmbeddedChain.cjs');
require('../../context/ThemeContext/ThemeContext.cjs');
require('../../utils/hooks/useUserUpdateRequest/useUpdateUser/userFieldsSchema.cjs');
require('bs58');
require('@dynamic-labs/types');
require('../../context/SocialRedirectContext/SocialRedirectContext.cjs');
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('../../views/CollectUserDataView/useFields.cjs');
require('../../context/FieldsStateContext/FieldsStateContext.cjs');
require('../../context/UserFieldEditorContext/UserFieldEditorContext.cjs');
require('@dynamic-labs/rpc-providers');
require('../../store/state/walletOptions/walletOptions.cjs');
require('react-i18next');
require('../Accordion/components/AccordionItem/AccordionItem.cjs');
require('../Alert/Alert.cjs');
var useHover = require('../../utils/hooks/useHover/useHover.cjs');
var ShadowDOM = require('../ShadowDOM/ShadowDOM.cjs');
require('../IconButton/IconButton.cjs');
require('../InlineWidget/InlineWidget.cjs');
require('../Input/Input.cjs');
require('../IsBrowser/IsBrowser.cjs');
require('../MenuList/Dropdown/Dropdown.cjs');
require('../OverlayCard/OverlayCard.cjs');
require('../Transition/ZoomTransition/ZoomTransition.cjs');
require('../Transition/SlideInUpTransition/SlideInUpTransition.cjs');
require('../Transition/OpacityTransition/OpacityTransition.cjs');
require('../PasskeyCreatedSuccessBanner/PasskeyCreatedSuccessBanner.cjs');
require('../Popper/Popper/Popper.cjs');
require('../Popper/PopperContext/PopperContext.cjs');
require('react-focus-lock');
require('qrcode');
require('formik');
require('../../utils/hooks/useSubdomainCheck/useSubdomainCheck.cjs');
require('../../context/WalletGroupContext/WalletGroupContext.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('../../widgets/DynamicWidget/context/DynamicWidgetContext.cjs');
require('../../widgets/DynamicWidget/helpers/convertExchangeKeyAndProviderEnum.cjs');
require('../../views/ExchangeWhitelistWarning/ExchangeWhitelistWarning.cjs');
require('../../context/ErrorContext/hooks/useErrorText/useErrorText.cjs');
require('../../context/FooterAnimationContext/index.cjs');
require('../../views/MfaChooseDeviceView/useGetMfaOptions/useGetMfaOptions.cjs');
require('../../context/PasskeyContext/PasskeyContext.cjs');
require('../../context/OnrampContext/OnrampContext.cjs');
require('../../store/state/sendBalances.cjs');
require('../../store/state/connectorsInitializing/connectorsInitializing.cjs');
require('../OverlayCardBase/OverlayCardTarget/OverlayCardTarget.cjs');
require('../../widgets/DynamicWidget/components/DynamicWidgetHeader/DynamicWidgetHeader.cjs');
require('../../views/TransactionConfirmationView/TransactionConfirmationView.cjs');
require('../../widgets/DynamicWidget/components/PasskeyCard/PasskeyCard.cjs');
require('../../widgets/DynamicWidget/views/CryptoComOnramp/CryptoComOnramp.cjs');
require('../../../index.cjs');
require('../../widgets/DynamicWidget/views/ReceiveWalletFunds/ReceiveWalletFunds.cjs');
require('../../store/state/tokenBalances.cjs');
require('../../store/state/multichainBalances.cjs');
require('../../shared/utils/functions/getInitialUrl/getInitialUrl.cjs');

/** Map from useHover React handlers to JS ones */
const handlerMapping = {
    onMouseDown: 'mousedown',
    onMouseEnter: 'mouseenter',
    onMouseLeave: 'mouseleave',
    onMouseUp: 'mouseup',
};
/**
 * Sets up a Tooltip that appears on hover
 *
 * Note: Prefer to use targetRef instead of nesting children!
 * Children of Tooltip will eventually replace content.
 */
const Tooltip = ({ children, content, className, as: Tag = 'span', copykey, targetRef, }) => {
    const { createRootElement } = useElementById.useElementById();
    const portalRoot = createRootElement('dynamic-tooltips');
    const [vector, setVector] = React.useState([0, 0]);
    const [visible, setVisible] = React.useState(false);
    /** If targetRef is not set, fall back to this ref */
    const [childrenWrapperRef, setChildrenWrapperRef] = React.useState(null);
    const effectiveRef = targetRef !== null && targetRef !== void 0 ? targetRef : childrenWrapperRef;
    /** Centers the tooltip to top-center of the target */
    const calcPosition = React.useCallback(() => {
        if (!effectiveRef)
            return;
        const childrenBounding = effectiveRef.getBoundingClientRect();
        setVector([
            childrenBounding.left + childrenBounding.width / 2,
            childrenBounding.top - 6, //add 6px margin over the children
        ]);
    }, [effectiveRef]);
    const [, childrenHoverHandlers] = useHover.useHover({
        falseOnClick: true,
        initialValue: false,
        onHover: () => {
            calcPosition();
            setVisible(true);
        },
        onHoverOff: () => {
            setVisible(false);
        },
    });
    // Updates top/left on initial render, and when window resizes
    React.useEffect(() => {
        calcPosition();
        const resizeListener = debounce.debounce(() => {
            calcPosition();
        }, 3);
        window.addEventListener('resize', resizeListener);
        return () => {
            window.removeEventListener('resize', resizeListener);
        };
    }, [calcPosition]);
    // Set handlers manually
    React.useEffect(() => {
        const element = effectiveRef;
        if (element) {
            for (const handler in childrenHoverHandlers) {
                element.addEventListener(handlerMapping[handler], childrenHoverHandlers[handler]);
            }
            return () => {
                for (const handler in childrenHoverHandlers) {
                    element.removeEventListener(handlerMapping[handler], childrenHoverHandlers[handler]);
                }
            };
        }
        return undefined;
    }, [effectiveRef, childrenHoverHandlers]);
    return (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [reactDom.createPortal(jsxRuntime.jsx(ShadowDOM.ShadowDOM, { zIndex: index.tooltipZIndex, children: jsxRuntime.jsxs("div", { "data-testid": 'dynamicTooltip', className: classNames.classNames('dynamic-tooltip', {
                        'dynamic-tooltip--visible': visible,
                    }, className), style: { left: vector[0], top: vector[1] }, copykey: copykey, children: [content, jsxRuntime.jsx(tooltipArrow.ReactComponent, { className: 'dynamic-tooltip__arrow' })] }) }), portalRoot.current), targetRef === undefined && (jsxRuntime.jsx(Tag, { ref: setChildrenWrapperRef, children: children }))] }));
};

exports.Tooltip = Tooltip;
