"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _get2 = _interopRequireDefault(require("@babel/runtime/helpers/get"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _reactDom = _interopRequireDefault(require("react-dom"));

var _debounce = _interopRequireDefault(require("lodash/debounce"));

var _reduxStore = _interopRequireDefault(require("../../../../reduxStore"));

// ======================================================
// this component extends another component and will run
// the 'clickedOutside' function defined on that component
// if users click outside of it
// clickedOutside: called when the user clicks outside node
// ignoreClickOutsideIf: if returns true then do not run
// the clickedOutside callback
// ======================================================
var wrapWithOutsideClick = function wrapWithOutsideClick(ComposedComponent) {
  var wrappedWithOutsideClick =
  /*#__PURE__*/
  function (_ComposedComponent) {
    (0, _inherits2.default)(wrappedWithOutsideClick, _ComposedComponent);

    function wrappedWithOutsideClick(props) {
      var _this;

      (0, _classCallCheck2.default)(this, wrappedWithOutsideClick);
      _this = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(wrappedWithOutsideClick).call(this, props));
      _this.checkForOutsideClick = _this.checkForOutsideClick.bind((0, _assertThisInitialized2.default)(_this));
      return _this;
    }

    (0, _createClass2.default)(wrappedWithOutsideClick, [{
      key: "componentDidMount",
      value: function componentDidMount() {
        this.mouseDownEventCb = (0, _debounce.default)(this.checkForOutsideClick, 300, {
          leading: true,
          trailing: false
        });
        document.addEventListener('click', this.mouseDownEventCb, true);
        (0, _get2.default)((0, _getPrototypeOf2.default)(wrappedWithOutsideClick.prototype), "componentDidMount", this) && (0, _get2.default)((0, _getPrototypeOf2.default)(wrappedWithOutsideClick.prototype), "componentDidMount", this).call(this);
      }
    }, {
      key: "componentWillUnmount",
      value: function componentWillUnmount() {
        document.removeEventListener('click', this.mouseDownEventCb, true);
        (0, _get2.default)((0, _getPrototypeOf2.default)(wrappedWithOutsideClick.prototype), "componentWillUnmount", this) && (0, _get2.default)((0, _getPrototypeOf2.default)(wrappedWithOutsideClick.prototype), "componentWillUnmount", this).call(this);
      }
    }, {
      key: "checkForOutsideClick",
      value: function checkForOutsideClick(e) {
        var isModalOpen = _reduxStore.default.getAppState().getIn(['modal', 'isOpen']);

        if ((0, _get2.default)((0, _getPrototypeOf2.default)(wrappedWithOutsideClick.prototype), "ignoreClickOutsideIf", this) && (0, _get2.default)((0, _getPrototypeOf2.default)(wrappedWithOutsideClick.prototype), "ignoreClickOutsideIf", this).call(this, e) || isModalOpen) {
          return;
        }

        if ((0, _get2.default)((0, _getPrototypeOf2.default)(wrappedWithOutsideClick.prototype), "clickedOutside", this)) {
          var domNode = _reactDom.default.findDOMNode(this);

          if (!(domNode && domNode.contains(e.target))) {
            (0, _get2.default)((0, _getPrototypeOf2.default)(wrappedWithOutsideClick.prototype), "clickedOutside", this).call(this, e);
          }
        }
      }
    }]);
    return wrappedWithOutsideClick;
  }(ComposedComponent);

  return wrappedWithOutsideClick;
};

var _default = wrapWithOutsideClick;
exports.default = _default;