import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import EventListener from 'react-event-listener';

function ownerDocument(node) {
  return node && node.ownerDocument || document;
}
/**
 * Listen for click events that occur somewhere in the document, outside of the element itself.
 * For instance, if you need to hide a menu when people click anywhere else on your page.
 */


var ClickAwayListener =
/*#__PURE__*/
function (_React$Component) {
  _inherits(ClickAwayListener, _React$Component);

  function ClickAwayListener() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, ClickAwayListener);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(ClickAwayListener)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_this), "mounted", false);

    _defineProperty(_assertThisInitialized(_this), "moved", false);

    _defineProperty(_assertThisInitialized(_this), "handleClickAway", function (event) {
      var onClickAway = _this.props.onClickAway; // Ignore events that have been `event.preventDefault()` marked.

      if (event.defaultPrevented) {
        return;
      } // IE 11 support, which trigger the handleClickAway even after the unbind


      if (!_this.mounted) {
        return;
      } // Do not act if user performed touchmove


      if (_this.moved) {
        _this.moved = false;
        return;
      } // The child might render null.


      if (!_this.node) {
        return;
      }

      var doc = ownerDocument(_this.node);

      if (doc.documentElement && doc.documentElement.contains(event.target) && !_this.node.contains(event.target)) {
        onClickAway(event);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleTouchMove", function () {
      _this.moved = true;
    });

    return _this;
  }

  _createClass(ClickAwayListener, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      // Finds the first child when a component returns a fragment.
      // https://github.com/facebook/react/blob/036ae3c6e2f056adffc31dfb78d1b6f0c63272f0/packages/react-dom/src/__tests__/ReactDOMFiber-test.js#L105
      this.node = ReactDOM.findDOMNode(this);
      this.mounted = true;
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      this.mounted = false;
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
          children = _this$props.children,
          mouseEvent = _this$props.mouseEvent,
          touchEvent = _this$props.touchEvent,
          onClickAway = _this$props.onClickAway,
          other = _objectWithoutProperties(_this$props, ["children", "mouseEvent", "touchEvent", "onClickAway"]);

      var listenerProps = {};

      if (mouseEvent !== false) {
        listenerProps[mouseEvent] = this.handleClickAway;
      }

      if (touchEvent !== false) {
        listenerProps[touchEvent] = this.handleClickAway;
        listenerProps.onTouchMove = this.handleTouchMove;
      }

      return React.createElement(React.Fragment, null, children, React.createElement(EventListener, _extends({
        target: "document"
      }, listenerProps, other)));
    }
  }]);

  return ClickAwayListener;
}(React.Component);

ClickAwayListener.propTypes = {
  /**
   * 要监听的鼠标事件，`false`表示禁用事件监听
   */
  mouseEvent: PropTypes.oneOf(['onClick', 'onMouseDown', 'onMouseUp', false]),

  /**
   * 点击页面其他部分的事件回调.
   */
  onClickAway: PropTypes.func.isRequired,

  /**
   * 要监听的触摸事件，`false`表示禁用事件监听
   */
  touchEvent: PropTypes.oneOf(['onTouchStart', 'onTouchEnd', false])
};
ClickAwayListener.defaultProps = {
  mouseEvent: 'onMouseUp',
  touchEvent: 'onTouchEnd'
};
export default ClickAwayListener;