var _excluded = ["plain", "open", "align", "dropProps", "content", "animation", "children", "zIndex", "ref", "onHoverChange"];
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; }
import React, { useCallback, Fragment, useRef, useEffect } 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 noop = function noop() {};
var getContent = function getContent(content) {
  return typeof content === "function" ? content() : content;
};
var Popover = function Popover(_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,
    children = _ref.children,
    _ref$zIndex = _ref.zIndex,
    zIndex = _ref$zIndex === void 0 ? 70 : _ref$zIndex,
    parentRef = _ref.ref,
    _ref$onHoverChange = _ref.onHoverChange,
    onHoverChange = _ref$onHoverChange === void 0 ? noop : _ref$onHoverChange,
    rest = _objectWithoutPropertiesLoose(_ref, _excluded);
  var id = useDescribedId(rest["aria-describedby"]);
  var _useToggle = useToggle(initialOpen),
    isOpen = _useToggle[0],
    open = _useToggle[2],
    close = _useToggle[3];
  var dropHoverRef = useRef(false);
  var boxHoverRef = useRef(false);
  var closeDrop = useCallback(function () {
    return requestAnimationFrame(function () {
      return !dropHoverRef.current && !boxHoverRef.current && close();
    });
  }, []);
  var _useForwardRef = useForwardRef(parentRef),
    ref = _useForwardRef[0],
    setRef = _useForwardRef[1];
  var onTargetElementMouseOver = useCallback(function () {
    open();
    onHoverChange(true);
  }, [open, onHoverChange]);
  var onTargetElementMouseLeave = useCallback(function () {
    closeDrop();
    onHoverChange(false);
  }, [closeDrop, onHoverChange]);
  var targetElement = useClonedChildren(children, setRef, _extends({
    isOpen: isOpen,
    onMouseOver: onTargetElementMouseOver,
    onMouseLeave: onTargetElementMouseLeave,
    onFocus: open,
    onBlur: closeDrop
  }, isOpen && {
    "aria-describedby": id
  }, rest));
  useEffect(function () {
    if (initialOpen) {
      open();
    } else {
      close();
    }
  }, [initialOpen, open, close]);
  var onMouseEnter = useCallback(function () {
    dropHoverRef.current = true;
  }, []);
  var onMouseLeave = useCallback(function () {
    dropHoverRef.current = false;
    closeDrop();
  }, []);
  return /*#__PURE__*/React.createElement(Fragment, null, targetElement, isOpen && ref.current && /*#__PURE__*/React.createElement(Drop, _extends({
    id: id,
    hideShadow: true
  }, dropProps, {
    align: (dropProps == null ? void 0 : dropProps.align) || dropAlignMap[align],
    animation: animation,
    onEsc: close,
    onMouseEnter: onMouseEnter,
    onMouseLeave: onMouseLeave,
    target: ref.current,
    zIndex: zIndex
  }), plain ? getContent(content) : /*#__PURE__*/React.createElement(Container, {
    align: align,
    background: "tooltip",
    backgroundOpacity: 0.9,
    padding: [2, 4]
  }, getContent(content))));
};
export default Popover;