"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _drop = _interopRequireDefault(require("../drop"));
var _useForwardRef2 = _interopRequireDefault(require("../../../hooks/useForwardRef"));
var _useToggle2 = _interopRequireDefault(require("../../../hooks/useToggle"));
var _useClonedChildren = _interopRequireDefault(require("../mixins/useClonedChildren"));
var _useDescribedId = _interopRequireDefault(require("../mixins/useDescribedId"));
var _dropAlignMap = _interopRequireDefault(require("../mixins/dropAlignMap"));
var _container = _interopRequireDefault(require("../container"));
var _excluded = ["plain", "open", "align", "dropProps", "content", "animation", "disabled", "zIndex", "delay", "children", "allowHoverOnTooltip", "ref"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
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 _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; }
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 = (0, _useDescribedId["default"])(rest["aria-describedby"]);
  var _useToggle = (0, _useToggle2["default"])(false),
    isOpen = _useToggle[0],
    open = _useToggle[2],
    close = _useToggle[3];
  var _useForwardRef = (0, _useForwardRef2["default"])(parentRef),
    ref = _useForwardRef[0],
    setRef = _useForwardRef[1];
  var onAccessorRef = (0, _react.useRef)(false);
  var onTooltipRef = (0, _react.useRef)(false);
  var targetElement = (0, _useClonedChildren["default"])(children, setRef, _extends({
    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));
  (0, _react.useLayoutEffect)(function () {
    if (ref.current && initialOpen) open();
  }, []);
  if (!content) {
    return children;
  }
  return /*#__PURE__*/_react["default"].createElement(_react.Fragment, null, targetElement, isOpen && ref.current && !disabled && /*#__PURE__*/_react["default"].createElement(_drop["default"], _extends({
    noEvents: !allowHoverOnTooltip,
    align: (dropProps == null ? void 0 : dropProps.align) || _dropAlignMap["default"][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["default"].createElement(_container["default"], {
    align: align
  }, getContent(content))));
};
var _default = exports["default"] = Tooltip;