"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get tooltipConfig () {
        return tooltipConfig;
    },
    get tooltipRoot () {
        return tooltipRoot;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _styled = /*#__PURE__*/ _interop_require_default(require("@emotion/styled"));
const _engines = require("../../engines");
const _utils = require("../../utils");
const _Popover = require("../Popover");
const _hooks = require("./hooks");
const _Tooltipstyles = require("./Tooltip.styles");
const _base = require("./variations/_view/base");
const _base1 = require("./variations/_size/base");
const _Tooltiptokens = require("./Tooltip.tokens");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const ESCAPE_KEYCODE = 27;
const getStringValue = (value)=>{
    return typeof value === 'number' ? `${value}rem` : value;
};
const Popover = (0, _engines.component)(_Popover.popoverConfig);
const StyledPopover = /*#__PURE__*/ (0, _styled.default)(Popover, {
    target: "e1mna4990",
    label: "StyledPopover"
})(_Popover.popoverTokens.arrowMaskWidth, ":var(", _Tooltiptokens.tokens.arrowMaskWidth, ");", _Popover.popoverTokens.arrowMaskHeight, ":var(", _Tooltiptokens.tokens.arrowMaskHeight, ");", _Popover.popoverTokens.arrowMaskImage, ":var(", _Tooltiptokens.tokens.arrowMaskImage, ");", _Popover.popoverTokens.arrowBackground, ":var(", _Tooltiptokens.tokens.arrowBackground, ");", _Popover.popoverTokens.arrowHeight, ":var(", _Tooltiptokens.tokens.arrowHeight, ");", _Popover.popoverTokens.arrowEdgeMargin, ":var(", _Tooltiptokens.tokens.arrowEdgeMargin, ");");
const tooltipRoot = (Root)=>/*#__PURE__*/ (0, _react.forwardRef)(({ id, text, opened, isVisible, isOpen, hasArrow = true, arrow, animated, offset = [
        3,
        8
    ], minWidth, maxWidth, placement = 'bottom', usePortal = true, target, children, onDismiss, view, size, contentLeft, zIndex = '9200', className, style, hoverTimeout, mouseEnterDelay = 0, mouseLeaveDelay, trigger, ...rest }, outerRef)=>{
        const [ref, setRef] = (0, _react.useState)(null);
        mouseLeaveDelay = mouseLeaveDelay ?? hoverTimeout ?? 300;
        const { opened: openedState, showTooltip, hideTooltip, setOpened } = (0, _hooks.useDelayedTooltip)(mouseEnterDelay, mouseLeaveDelay);
        const isTooltipOpened = Boolean(text) && (isVisible || isOpen || opened || openedState);
        (0, _react.useEffect)(()=>{
            const onKeyDown = (event)=>{
                if (event.keyCode === ESCAPE_KEYCODE) {
                    onDismiss?.();
                }
            };
            window.addEventListener('keydown', onKeyDown);
            return ()=>{
                window.removeEventListener('keydown', onKeyDown);
            };
        }, []);
        const onToggle = (isOpen)=>{
            if (trigger === 'hover') {
                if (isOpen) {
                    showTooltip();
                } else {
                    hideTooltip();
                }
            } else {
                setOpened(isOpen);
            }
        };
        return /*#__PURE__*/ _react.default.createElement(StyledPopover, {
            opened: isTooltipOpened,
            placement: placement,
            offset: offset,
            zIndex: zIndex,
            target: target || children,
            usePortal: usePortal,
            hasArrow: arrow || hasArrow,
            "aria-hidden": !isTooltipOpened,
            "aria-live": "polite",
            role: "tooltip",
            animated: animated,
            className: (0, _utils.cx)(ref?.classList.toString()),
            ...(trigger === 'hover' || trigger === 'click') && {
                trigger,
                onToggle
            },
            ...rest
        }, /*#__PURE__*/ _react.default.createElement(Root, {
            view: view,
            size: size,
            ref: setRef,
            className: className,
            style: style
        }, /*#__PURE__*/ _react.default.createElement(_Tooltipstyles.TooltipRoot, {
            ref: outerRef,
            id: id,
            maxWidth: getStringValue(maxWidth),
            minWidth: getStringValue(minWidth)
        }, contentLeft && /*#__PURE__*/ _react.default.createElement(_Tooltipstyles.StyledContentLeft, null, contentLeft), text)));
    });
const tooltipConfig = {
    name: 'Tooltip',
    tag: 'div',
    layout: tooltipRoot,
    base: '',
    variations: {
        view: {
            css: _base.base
        },
        size: {
            css: _base1.base
        }
    },
    defaults: {
        view: 'default',
        size: 'm'
    }
};
