'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _get2 = require('babel-runtime/helpers/get');

var _get3 = _interopRequireDefault(_get2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _reactDom = require('react-dom');

var _reactDom2 = _interopRequireDefault(_reactDom);

var _debounce = require('lodash/debounce');

var _debounce2 = _interopRequireDefault(_debounce);

var _reduxStore = require('../../../../reduxStore');

var _reduxStore2 = _interopRequireDefault(_reduxStore);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// ======================================================
// 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 = function (_ComposedComponent) {
    (0, _inherits3.default)(wrappedWithOutsideClick, _ComposedComponent);

    function wrappedWithOutsideClick(props) {
      (0, _classCallCheck3.default)(this, wrappedWithOutsideClick);

      var _this = (0, _possibleConstructorReturn3.default)(this, (wrappedWithOutsideClick.__proto__ || Object.getPrototypeOf(wrappedWithOutsideClick)).call(this, props));

      _this.checkForOutsideClick = _this.checkForOutsideClick.bind(_this);
      return _this;
    }

    (0, _createClass3.default)(wrappedWithOutsideClick, [{
      key: 'componentDidMount',
      value: function componentDidMount() {
        this.mouseDownEventCb = (0, _debounce2.default)(this.checkForOutsideClick, 300, {
          leading: true,
          trailing: false
        });
        document.addEventListener('click', this.mouseDownEventCb, true);
        (0, _get3.default)(wrappedWithOutsideClick.prototype.__proto__ || Object.getPrototypeOf(wrappedWithOutsideClick.prototype), 'componentDidMount', this) && (0, _get3.default)(wrappedWithOutsideClick.prototype.__proto__ || Object.getPrototypeOf(wrappedWithOutsideClick.prototype), 'componentDidMount', this).call(this);
      }
    }, {
      key: 'componentWillUnmount',
      value: function componentWillUnmount() {
        document.removeEventListener('click', this.mouseDownEventCb, true);
        (0, _get3.default)(wrappedWithOutsideClick.prototype.__proto__ || Object.getPrototypeOf(wrappedWithOutsideClick.prototype), 'componentWillUnmount', this) && (0, _get3.default)(wrappedWithOutsideClick.prototype.__proto__ || Object.getPrototypeOf(wrappedWithOutsideClick.prototype), 'componentWillUnmount', this).call(this);
      }
    }, {
      key: 'checkForOutsideClick',
      value: function checkForOutsideClick(e) {
        var isModalOpen = _reduxStore2.default.getAppState().getIn(['modal', 'isOpen']);
        if ((0, _get3.default)(wrappedWithOutsideClick.prototype.__proto__ || Object.getPrototypeOf(wrappedWithOutsideClick.prototype), 'ignoreClickOutsideIf', this) && (0, _get3.default)(wrappedWithOutsideClick.prototype.__proto__ || Object.getPrototypeOf(wrappedWithOutsideClick.prototype), 'ignoreClickOutsideIf', this).call(this, e) || isModalOpen) {
          return;
        }

        if ((0, _get3.default)(wrappedWithOutsideClick.prototype.__proto__ || Object.getPrototypeOf(wrappedWithOutsideClick.prototype), 'clickedOutside', this)) {
          var domNode = _reactDom2.default.findDOMNode(this);
          if (!(domNode && domNode.contains(e.target))) {
            (0, _get3.default)(wrappedWithOutsideClick.prototype.__proto__ || Object.getPrototypeOf(wrappedWithOutsideClick.prototype), 'clickedOutside', this).call(this, e);
          }
        }
      }
    }]);
    return wrappedWithOutsideClick;
  }(ComposedComponent);

  return wrappedWithOutsideClick;
};

exports.default = wrapWithOutsideClick;