function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _iterable_to_array(iter) {
    if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function _object_without_properties(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys, key, i;
    if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
        sourceKeys = Reflect.ownKeys(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
        return target;
    }
    target = _object_without_properties_loose(source, excluded);
    if (Object.getOwnPropertySymbols) {
        sourceKeys = Object.getOwnPropertySymbols(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
    }
    return target;
}
function _object_without_properties_loose(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
    for(i = 0; i < sourceKeys.length; i++){
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) continue;
        if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
        target[key] = source[key];
    }
    return target;
}
function _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _to_array(arr) {
    return _array_with_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_rest();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
import React, { useEffect, forwardRef, useState } from "react";
import styled from "@emotion/styled";
import { component } from "../../engines";
import { cx } from "../../utils";
import { popoverConfig, popoverTokens } from "../Popover";
import { useDelayedTooltip } from "./hooks";
import { StyledContentLeft, TooltipRoot } from "./Tooltip.styles";
import { base as viewCSS } from "./variations/_view/base";
import { base as sizeCSS } from "./variations/_size/base";
import { tokens } from "./Tooltip.tokens";
var ESCAPE_KEYCODE = 27;
var getStringValue = function getStringValue(value) {
    return typeof value === 'number' ? "".concat(value, "rem") : value;
};
var Popover = component(popoverConfig);
var StyledPopover = /*#__PURE__*/ styled(Popover, {
    target: "ehyx5h60",
    label: "StyledPopover"
})(popoverTokens.arrowMaskWidth, ":var(", tokens.arrowMaskWidth, ");", popoverTokens.arrowMaskHeight, ":var(", tokens.arrowMaskHeight, ");", popoverTokens.arrowMaskImage, ":var(", tokens.arrowMaskImage, ");", popoverTokens.arrowBackground, ":var(", tokens.arrowBackground, ");", popoverTokens.arrowHeight, ":var(", tokens.arrowHeight, ");", popoverTokens.arrowEdgeMargin, ":var(", tokens.arrowEdgeMargin, ");");
/**
 * Компонент для текстовых подсказок. Основное предназначение — подписи к блокам.
 */ export var tooltipRoot = function tooltipRoot(Root) {
    return /*#__PURE__*/ forwardRef(function(_0, _1) {
        var _ref = [
            _0,
            _1
        ], _ref1 = _to_array(_ref), _ref2 = _ref1[0], _rest = _ref1.slice(1), id = _ref2.id, text = _ref2.text, opened = _ref2.opened, isVisible = _ref2.isVisible, isOpen = _ref2.isOpen, _ref_hasArrow = _ref2.hasArrow, hasArrow = _ref_hasArrow === void 0 ? true : _ref_hasArrow, arrow = _ref2.arrow, animated = _ref2.animated, _ref_offset = _ref2.offset, offset = _ref_offset === void 0 ? [
            3,
            8
        ] : _ref_offset, minWidth = _ref2.minWidth, maxWidth = _ref2.maxWidth, _ref_placement = _ref2.placement, placement = _ref_placement === void 0 ? 'bottom' : _ref_placement, _ref_usePortal = _ref2.usePortal, usePortal = _ref_usePortal === void 0 ? true : _ref_usePortal, target = _ref2.target, children = _ref2.children, onDismiss = _ref2.onDismiss, view = _ref2.view, size = _ref2.size, contentLeft = _ref2.contentLeft, _ref_zIndex = _ref2.zIndex, zIndex = _ref_zIndex === void 0 ? '9200' : _ref_zIndex, className = _ref2.className, style = _ref2.style, hoverTimeout = _ref2.hoverTimeout, _ref_mouseEnterDelay = _ref2.mouseEnterDelay, mouseEnterDelay = _ref_mouseEnterDelay === void 0 ? 0 : _ref_mouseEnterDelay, mouseLeaveDelay = _ref2.mouseLeaveDelay, trigger = _ref2.trigger, rest = _object_without_properties(_ref2, [
            "id",
            "text",
            "opened",
            "isVisible",
            "isOpen",
            "hasArrow",
            "arrow",
            "animated",
            "offset",
            "minWidth",
            "maxWidth",
            "placement",
            "usePortal",
            "target",
            "children",
            "onDismiss",
            "view",
            "size",
            "contentLeft",
            "zIndex",
            "className",
            "style",
            "hoverTimeout",
            "mouseEnterDelay",
            "mouseLeaveDelay",
            "trigger"
        ]), _rest1 = _sliced_to_array(_rest, 1), outerRef = _rest1[0];
        var _ref3;
        var _useState = _sliced_to_array(useState(null), 2), ref = _useState[0], setRef = _useState[1];
        mouseLeaveDelay = (_ref3 = mouseLeaveDelay !== null && mouseLeaveDelay !== void 0 ? mouseLeaveDelay : hoverTimeout) !== null && _ref3 !== void 0 ? _ref3 : 300;
        var _useDelayedTooltip = useDelayedTooltip(mouseEnterDelay, mouseLeaveDelay), openedState = _useDelayedTooltip.opened, showTooltip = _useDelayedTooltip.showTooltip, hideTooltip = _useDelayedTooltip.hideTooltip, setOpened = _useDelayedTooltip.setOpened;
        var isTooltipOpened = Boolean(text) && (isVisible || isOpen || opened || openedState);
        useEffect(function() {
            var onKeyDown = function onKeyDown(event) {
                if (event.keyCode === ESCAPE_KEYCODE) {
                    onDismiss === null || onDismiss === void 0 ? void 0 : onDismiss();
                }
            };
            window.addEventListener('keydown', onKeyDown);
            return function() {
                window.removeEventListener('keydown', onKeyDown);
            };
        }, []);
        var onToggle = function onToggle(isOpen) {
            if (trigger === 'hover') {
                if (isOpen) {
                    showTooltip();
                } else {
                    hideTooltip();
                }
            } else {
                setOpened(isOpen);
            }
        };
        return /*#__PURE__*/ React.createElement(StyledPopover, _object_spread({
            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: cx(ref === null || ref === void 0 ? void 0 : ref.classList.toString())
        }, (trigger === 'hover' || trigger === 'click') && {
            trigger: trigger,
            onToggle: onToggle
        }, rest), /*#__PURE__*/ React.createElement(Root, {
            view: view,
            size: size,
            ref: setRef,
            className: className,
            style: style
        }, /*#__PURE__*/ React.createElement(TooltipRoot, {
            ref: outerRef,
            id: id,
            maxWidth: getStringValue(maxWidth),
            minWidth: getStringValue(minWidth)
        }, contentLeft && /*#__PURE__*/ React.createElement(StyledContentLeft, null, contentLeft), text)));
    });
};
export var tooltipConfig = {
    name: 'Tooltip',
    tag: 'div',
    layout: tooltipRoot,
    base: '',
    variations: {
        view: {
            css: viewCSS
        },
        size: {
            css: sizeCSS
        }
    },
    defaults: {
        view: 'default',
        size: 'm'
    }
};
