"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 _classnames = /*#__PURE__*/ _interop_require_default(require("classnames"));
const _react1 = require("@floating-ui/react");
const _react2 = require("@emotion/react");
const _Slot = require("../../_Slot/Slot");
const _Popover = require("../Popover/Popover");
const _utils = require("../Popover/utils");
const _Tooltiptokens = require("./Tooltip.tokens");
const _Tooltipstyles = require("./Tooltip.styles");
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 tooltipRoot = (Root)=>/*#__PURE__*/ (0, _react.forwardRef)(({ children, target, iconSlot, trigger = 'click', placement = 'bottom', hasTail = true, flip: outsideFlip = false, shift: outsideShift = false, offset: outerOffset = 0, delayOpen = 0, delayClose = 0, zIndex = 1000, className, style, size, view, portal, ...rest }, outerRootRef)=>{
        const [opened, setOpened] = (0, _react.useState)(false);
        const arrowRef = (0, _react.useRef)(null);
        const handleToggle = (opened)=>{
            setOpened(opened);
        };
        const { refs, floatingStyles, context } = (0, _react1.useFloating)({
            whileElementsMounted: _react1.autoUpdate,
            placement,
            open: opened,
            onOpenChange: handleToggle,
            middleware: [
                outsideShift && (0, _react1.shift)({
                    limiter: (0, _react1.limitShift)()
                }),
                outsideFlip && (0, _react1.flip)(),
                hasTail && (0, _react1.arrow)({
                    element: arrowRef,
                    padding: _Popover.ARROW_PADDING
                }),
                (0, _react1.offset)((hasTail ? _Popover.ARROW_HEIGHT : 0) + outerOffset)
            ]
        });
        const click = (0, _react1.useClick)(context, {
            enabled: trigger === 'click' || matchMedia('(hover: none)').matches
        });
        const focus = (0, _react1.useFocus)(context, {
            enabled: trigger === 'focus'
        });
        const dismiss = (0, _react1.useDismiss)(context, {
            enabled: true,
            outsidePress: true
        });
        const role = (0, _react1.useRole)(context);
        const hover = (0, _react1.useHover)(context, {
            mouseOnly: true,
            enabled: trigger === 'hover',
            handleClose: (0, _react1.safePolygon)({
                requireIntent: false
            }),
            delay: {
                open: delayOpen,
                close: delayClose
            }
        });
        const { getReferenceProps, getFloatingProps } = (0, _react1.useInteractions)([
            dismiss,
            role,
            click,
            hover,
            focus
        ]);
        return /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/ _react.default.createElement(_Slot.Slot, {
            ref: refs.setReference,
            ...getReferenceProps()
        }, target), opened && /*#__PURE__*/ _react.default.createElement(_react1.FloatingPortal, (0, _utils.getFloatingPortalProps)(portal), /*#__PURE__*/ _react.default.createElement(Root, {
            ref: refs.setFloating,
            size: size,
            view: view,
            style: {
                ...floatingStyles,
                zIndex
            },
            ...getFloatingProps()
        }, /*#__PURE__*/ _react.default.createElement(_Tooltipstyles.Wrapper, {
            ref: outerRootRef,
            className: (0, _classnames.default)(className, _Tooltiptokens.classes.tooltipRoot),
            style: style,
            "data-tooltip-open": opened,
            ...rest
        }, iconSlot && /*#__PURE__*/ _react.default.createElement(_Tooltipstyles.IconWrapper, null, iconSlot), children, hasTail && /*#__PURE__*/ _react.default.createElement(_react1.FloatingArrow, {
            ref: arrowRef,
            context: context,
            width: _Popover.ARROW_WIDTH,
            height: _Popover.ARROW_HEIGHT,
            fill: `var(${_Tooltiptokens.tokens.backgroundColor})`,
            d: _Popover.ARROW_POLYGON
        })))));
    });
const tooltipConfig = {
    name: 'Tooltip',
    tag: 'div',
    layout: tooltipRoot,
    base: _Tooltipstyles.base,
    variations: {
        view: {
            css: (0, /*#__PURE__*/ _react2.css)("css")
        },
        size: {
            css: (0, /*#__PURE__*/ _react2.css)("css")
        }
    },
    defaults: {
        view: 'default',
        size: 'm'
    }
};
