'use strict';

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

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _reactDom2 = _interopRequireDefault(_reactDom);

var _mxa = require('../../../components/mxa');

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /**
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                * Created by baoyinghai on 1/4/17.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                */


var FilterBarContainer = function (_React$Component) {
  _inherits(FilterBarContainer, _React$Component);

  function FilterBarContainer(props) {
    _classCallCheck(this, FilterBarContainer);

    /* eslint-disable */
    var _this = _possibleConstructorReturn(this, (FilterBarContainer.__proto__ || Object.getPrototypeOf(FilterBarContainer)).call(this, props));

    _this._switchHandler = function (e) {
      e.stopPropagation();
      if (_this.tag && _this.tag.compareDocumentPosition(e.target) !== 20 && _this.props.advancedSearch) {
        var dropTags = document.querySelectorAll('.mx-select-dropdown.mx-select-dropdown--single.mx-select-dropdown-placement-bottomLeft');
        if (dropTags && dropTags.length > 0) {
          if (Array.prototype.some.call(dropTags, function (d) {
            return d.compareDocumentPosition(e.target) === 20;
          })) {
            return;
          }
        }
        var switchHandler = _this.props.switchHandler;

        switchHandler && switchHandler();
      }
    };

    var height = Math.ceil(_this.props.filters.length / 2) * 40 + 19;
    _this.height = height + 'px';
    return _this;
  }

  _createClass(FilterBarContainer, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      var defaultSearchBar = this.props.querySelector;
      var tags = document.querySelectorAll('.' + defaultSearchBar);
      if (tags.length > 0) {
        this.tag = tags[tags.length - 1];
      }
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      window.removeEventListener('click', this._switchHandler);
    }
  }, {
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      var _this2 = this;

      if (nextProps.advancedSearch) {
        setTimeout(function () {
          return window.addEventListener('click', _this2._switchHandler);
        });
      } else {
        window.removeEventListener('click', this._switchHandler);
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var prefixCls = this.props.prefixCls;

      var defaultSearchBar = prefixCls + '-search-bar-default';
      return _react2.default.createElement(
        'div',
        { className: defaultSearchBar, style: { height: this.props.advancedSearch ? this.height : '0px' } },
        this.props.children
      );
    }
  }]);

  return FilterBarContainer;
}(_react2.default.Component);

exports.default = FilterBarContainer;