var _excluded = ["plain", "open", "align", "dropProps", "content", "animation", "disabled", "zIndex", "delay", "children", "allowHoverOnTooltip", "ref"];
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
import React, { useRef, Fragment, useLayoutEffect } from "react";
import Drop from "../drop";
import useForwardRef from "../../../hooks/useForwardRef";
import useToggle from "../../../hooks/useToggle";
import useClonedChildren from "../mixins/useClonedChildren";
import useDescribedId from "../mixins/useDescribedId";
import dropAlignMap from "../mixins/dropAlignMap";
import Container from "../container";
var getContent = function getContent(content) {
  return typeof content === "function" ? content() : content;
};
var Tooltip = function Tooltip(_ref) {
  var plain = _ref.plain,
    _ref$open = _ref.open,
    initialOpen = _ref$open === void 0 ? false : _ref$open,
    _ref$align = _ref.align,
    align = _ref$align === void 0 ? "top" : _ref$align,
    dropProps = _ref.dropProps,
    content = _ref.content,
    animation = _ref.animation,
    disabled = _ref.disabled,
    _ref$zIndex = _ref.zIndex,
    zIndex = _ref$zIndex === void 0 ? 80 : _ref$zIndex,
    _ref$delay = _ref.delay,
    delay = _ref$delay === void 0 ? 0 : _ref$delay,
    children = _ref.children,
    allowHoverOnTooltip = _ref.allowHoverOnTooltip,
    parentRef = _ref.ref,
    rest = _objectWithoutPropertiesLoose(_ref, _excluded);
  var id = useDescribedId(rest["aria-describedby"]);
  var _useToggle = useToggle(false),
    isOpen = _useToggle[0],
    open = _useToggle[2],
    close = _useToggle[3];
  var _useForwardRef = useForwardRef(parentRef),
    ref = _useForwardRef[0],
    setRef = _useForwardRef[1];
  var onAccessorRef = useRef(false);
  var onTooltipRef = useRef(false);
  var targetElement = useClonedChildren(children, setRef, _objectSpread(_objectSpread({
    onMouseEnter: function onMouseEnter() {
      onAccessorRef.current = true;
      if (delay) {
        setTimeout(function () {
          if (onAccessorRef.current) {
            open();
          }
        }, delay);
      } else {
        open();
      }
    },
    onMouseLeave: !allowHoverOnTooltip ? function () {
      close();
      onAccessorRef.current = false;
    } : function () {
      return setTimeout(function () {
        if (onTooltipRef.current) return;
        close();
        onAccessorRef.current = false;
      }, 300);
    },
    onFocus: open,
    onBlur: close
  }, isOpen && {
    "aria-describedby": id
  }), rest));
  useLayoutEffect(function () {
    if (ref.current && initialOpen) open();
  }, []);
  if (!content) {
    return children;
  }
  return /*#__PURE__*/React.createElement(Fragment, null, targetElement, isOpen && ref.current && !disabled && /*#__PURE__*/React.createElement(Drop, _extends({
    noEvents: !allowHoverOnTooltip,
    align: (dropProps == null ? void 0 : dropProps.align) || dropAlignMap[align],
    hideShadow: true,
    id: id,
    onClickOutside: close,
    onMouseEnter: function onMouseEnter() {
      return onTooltipRef.current = true;
    },
    onMouseLeave: function onMouseLeave() {
      onTooltipRef.current = false;
      close();
    },
    target: ref.current
  }, dropProps, {
    animation: animation,
    onEsc: close,
    zIndex: zIndex
  }), plain ? getContent(content) : /*#__PURE__*/React.createElement(Container, {
    align: align
  }, getContent(content))));
};
export default Tooltip;