"use strict";

var _typeof = require("@babel/runtime-corejs3/helpers/typeof");
var _WeakMap = require("@babel/runtime-corejs3/core-js-stable/weak-map");
var _Object$getOwnPropertyDescriptor = require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor");
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/extends"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/slicedToArray"));
var _taggedTemplateLiteral2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/taggedTemplateLiteral"));
var _react = _interopRequireWildcard(require("react"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _utils = require("../../../utils/");
var _DefaultFilterIcon = _interopRequireDefault(require("./DefaultFilterIcon"));
var _styles = require("../../../base/styles");
var _keyCode = _interopRequireDefault(require("../../../utils/keyCode"));
var _templateObject;
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; }
var FilterPanelStyle = _styledComponents.default.div(_templateObject || (_templateObject = (0, _taggedTemplateLiteral2.default)(["\n  display: flex;\n  flex-direction: column;\n  max-height: 450px;\n  min-width: 160px;\n  border-radius: 2px;\n  background-color: #fff;\n  box-shadow: 0 0 5px 0 rgba(154,154,154,.5);\n  cursor: default;\n\n  .", " {\n    display: flex;\n    background-color: #ebedf1;\n\n    .", " {\n      display: flex;\n      color:#666;\n      background-color: #fff;\n      padding: 8px 16px 8px 16px;\n      border-right: 1px solid transparent;\n      border-left: 1px solid transparent;\n      border-top: 1px solid transparent;\n      border-top-right-radius: 2px;\n      border-top-left-radius: 2px;\n    }\n  }\n\n  .", " {\n    display: flex;\n  }\n"])), _styles.Classes.popupHeader, _styles.Classes.filterIcon, _styles.Classes.popupBody);
var useWindowEvents = function useWindowEvents(func, evens) {
  _react.default.useEffect(function () {
    evens.forEach(function (event) {
      return window.addEventListener(event, func, true);
    });
    return function () {
      return evens.forEach(function (event) {
        return window.removeEventListener(event, func, true);
      });
    };
  }, [evens, func]);
};
function FilterPanel(_ref) {
  var style = _ref.style,
    children = _ref.children,
    position = _ref.position,
    filterIcon = _ref.filterIcon,
    onClose = _ref.onClose,
    hideFilterPopupHeader = _ref.hideFilterPopupHeader;
  var _useState = (0, _react.useState)(position),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    perfectPosition = _useState2[0],
    setPerfectPosition = _useState2[1];
  var _useState3 = (0, _react.useState)(false),
    _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
    visible = _useState4[0],
    setVisible = _useState4[1];
  var ref = _react.default.useRef(null);
  var isContainPanel = function isContainPanel(e) {
    return (0, _utils.isElementInEventPath)(ref.current, e);
  };
  (0, _react.useEffect)(function () {
    setPerfectPosition((0, _utils.keepWithinBounds)(document.body, ref.current, position.x, position.y, true));
    setVisible(true);
  }, [position]);
  var hasPopupMouseEvent = (0, _react.useRef)(false);
  var handleMouseEvent = function handleMouseEvent() {
    // 当弹出的过滤面板内部发生鼠标按下、抬起事件时，标记当前事件，并在click捕获期清除标记，用来确定鼠标按下、抬起发生在过滤面板内部
    // 利用了React.createPortal冒泡是根据React Tree的特性：
    // https://jwwnz.medium.com/react-portals-and-event-bubbling-8df3e35ca3f1
    hasPopupMouseEvent.current = true;
  };
  useWindowEvents(function (e) {
    !isContainPanel(e) && !hasPopupMouseEvent.current && onClose();
    hasPopupMouseEvent.current = false;
  }, ['click']);
  var handleKeyDown = function handleKeyDown(e) {
    if (e.currentTarget.contains(e.target) && e.keyCode === _keyCode.default.ESC) {
      onClose();
    }
  };
  return /*#__PURE__*/_react.default.createElement(FilterPanelStyle, {
    className: _styles.Classes.popup,
    style: (0, _extends2.default)((0, _extends2.default)({}, style), {
      left: visible ? perfectPosition.x : 0,
      top: visible ? perfectPosition.y : 0,
      opacity: visible ? 1 : 0
    }),
    onMouseDown: handleMouseEvent,
    onMouseUp: handleMouseEvent,
    onKeyDown: handleKeyDown,
    ref: ref,
    tabIndex: -1
  }, !hideFilterPopupHeader ? /*#__PURE__*/_react.default.createElement("div", {
    className: _styles.Classes.popupHeader
  }, /*#__PURE__*/_react.default.createElement("span", {
    className: _styles.Classes.filterIcon
  }, filterIcon || /*#__PURE__*/_react.default.createElement(_DefaultFilterIcon.default, {
    width: 12,
    height: 12
  }))) : null, /*#__PURE__*/_react.default.createElement("div", {
    className: _styles.Classes.popupBody
  }, children));
}
var _default = exports.default = FilterPanel;