import _extends from "@babel/runtime-corejs3/helpers/extends";
import _slicedToArray from "@babel/runtime-corejs3/helpers/slicedToArray";
import _taggedTemplateLiteral from "@babel/runtime-corejs3/helpers/taggedTemplateLiteral";
var _templateObject;
import React, { useEffect, useState, useRef } from 'react';
import styled from 'styled-components';
import { isElementInEventPath, keepWithinBounds } from '../../../utils/';
import DefaultFilterIcon from './DefaultFilterIcon';
import { Classes } from '../../../base/styles';
import KeyCode from '../../../utils/keyCode';
var FilterPanelStyle = styled.div(_templateObject || (_templateObject = _taggedTemplateLiteral(["\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"])), Classes.popupHeader, Classes.filterIcon, Classes.popupBody);
var useWindowEvents = function useWindowEvents(func, evens) {
  React.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 = useState(position),
    _useState2 = _slicedToArray(_useState, 2),
    perfectPosition = _useState2[0],
    setPerfectPosition = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    visible = _useState4[0],
    setVisible = _useState4[1];
  var ref = React.useRef(null);
  var isContainPanel = function isContainPanel(e) {
    return isElementInEventPath(ref.current, e);
  };
  useEffect(function () {
    setPerfectPosition(keepWithinBounds(document.body, ref.current, position.x, position.y, true));
    setVisible(true);
  }, [position]);
  var hasPopupMouseEvent = 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.ESC) {
      onClose();
    }
  };
  return /*#__PURE__*/React.createElement(FilterPanelStyle, {
    className: Classes.popup,
    style: _extends(_extends({}, 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.createElement("div", {
    className: Classes.popupHeader
  }, /*#__PURE__*/React.createElement("span", {
    className: Classes.filterIcon
  }, filterIcon || /*#__PURE__*/React.createElement(DefaultFilterIcon, {
    width: 12,
    height: 12
  }))) : null, /*#__PURE__*/React.createElement("div", {
    className: Classes.popupBody
  }, children));
}
export default FilterPanel;