"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", "children", "zIndex"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
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 (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; }
var getContent = function getContent(content) {
  return typeof content === "function" ? content() : content;
};
var Popover = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, parentRef) {
  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,
    children = _ref.children,
    _ref$zIndex = _ref.zIndex,
    zIndex = _ref$zIndex === void 0 ? 70 : _ref$zIndex,
    rest = _objectWithoutPropertiesLoose(_ref, _excluded);
  var id = (0, _useDescribedId["default"])(rest["aria-describedby"]);
  var _useToggle = (0, _useToggle2["default"])(initialOpen),
    isOpen = _useToggle[0],
    open = _useToggle[2],
    close = _useToggle[3];
  var dropHoverRef = (0, _react.useRef)(false);
  var boxHoverRef = (0, _react.useRef)(false);
  var closeDrop = (0, _react.useCallback)(function () {
    return requestAnimationFrame(function () {
      return !dropHoverRef.current && !boxHoverRef.current && close();
    });
  }, []);
  var _useForwardRef = (0, _useForwardRef2["default"])(parentRef),
    ref = _useForwardRef[0],
    setRef = _useForwardRef[1];
  var targetElement = (0, _useClonedChildren["default"])(children, setRef, _objectSpread(_objectSpread({
    isOpen: isOpen,
    onMouseOver: open,
    onMouseLeave: closeDrop,
    onFocus: open,
    onBlur: closeDrop
  }, isOpen && {
    "aria-describedby": id
  }), rest));
  var onMouseEnter = (0, _react.useCallback)(function () {
    dropHoverRef.current = true;
  }, []);
  var onMouseLeave = (0, _react.useCallback)(function () {
    dropHoverRef.current = false;
    closeDrop();
  }, []);
  return /*#__PURE__*/_react["default"].createElement(_react.Fragment, null, targetElement, isOpen && ref.current && /*#__PURE__*/_react["default"].createElement(_drop["default"], _extends({
    id: id,
    hideShadow: true
  }, dropProps, {
    align: (dropProps == null ? void 0 : dropProps.align) || _dropAlignMap["default"][align],
    animation: animation,
    onEsc: close,
    onMouseEnter: onMouseEnter,
    onMouseLeave: onMouseLeave,
    target: ref.current,
    zIndex: zIndex
  }), plain ? getContent(content) : /*#__PURE__*/_react["default"].createElement(_container["default"], {
    align: align,
    background: "tooltip",
    backgroundOpacity: 0.9,
    padding: [2, 4]
  }, getContent(content))));
});
var _default = exports["default"] = Popover;