"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

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

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

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

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

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

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

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

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

var _react = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _checkbox = _interopRequireDefault(require("../checkbox"));

var _dropdown = _interopRequireDefault(require("../dropdown"));

var _menu = _interopRequireDefault(require("../menu"));

var _icon = _interopRequireDefault(require("../icon"));

var SelectionCheckboxAll = /*#__PURE__*/function (_Component) {
  (0, _inherits2["default"])(SelectionCheckboxAll, _Component);

  var _super = (0, _createSuper2["default"])(SelectionCheckboxAll);

  function SelectionCheckboxAll(props) {
    var _this;

    (0, _classCallCheck2["default"])(this, SelectionCheckboxAll);
    _this = _super.call(this, props);

    _this.handleSelectAllChagne = function (e) {
      var checked = e.target.checked;
      var onSelect = _this.props.onSelect;
      onSelect(checked ? 'all' : 'removeAll', 0, null);
    };

    _this.defaultSelections = props.hideDefaultSelections ? [] : [{
      key: 'all',
      text: props.locale.selectAll,
      onSelect: function onSelect() {}
    }, {
      key: 'invert',
      text: props.locale.selectInvert,
      onSelect: function onSelect() {}
    }];
    _this.state = {
      checked: _this.getCheckState(props),
      indeterminate: _this.getIndeterminateState(props)
    };
    return _this;
  }

  (0, _createClass2["default"])(SelectionCheckboxAll, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.subscribe();
    }
  }, {
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(nextProps) {
      this.setCheckState(nextProps);
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      if (this.unsubscribe) {
        this.unsubscribe();
      }
    }
  }, {
    key: "subscribe",
    value: function subscribe() {
      var _this2 = this;

      var store = this.props.store;
      this.unsubscribe = store.subscribe(function () {
        _this2.setCheckState(_this2.props);
      });
    }
  }, {
    key: "checkSelection",
    value: function checkSelection(data, type, byDefaultChecked) {
      var _this$props = this.props,
          store = _this$props.store,
          getCheckboxPropsByItem = _this$props.getCheckboxPropsByItem,
          getRecordKey = _this$props.getRecordKey; // type should be 'every' | 'some'

      if (type === 'every' || type === 'some') {
        return byDefaultChecked ? data[type](function (item, i) {
          return getCheckboxPropsByItem(item, i).defaultChecked;
        }) : data[type](function (item, i) {
          return store.getState().selectedRowKeys.indexOf(getRecordKey(item, i)) >= 0;
        });
      }

      return false;
    }
  }, {
    key: "setCheckState",
    value: function setCheckState(props) {
      var checked = this.getCheckState(props);
      var indeterminate = this.getIndeterminateState(props);
      this.setState(function (prevState) {
        var newState = {};

        if (indeterminate !== prevState.indeterminate) {
          newState.indeterminate = indeterminate;
        }

        if (checked !== prevState.checked) {
          newState.checked = checked;
        }

        return newState;
      });
    }
  }, {
    key: "getCheckState",
    value: function getCheckState(props) {
      var store = props.store,
          data = props.data;
      var checked;

      if (!data.length) {
        checked = false;
      } else {
        checked = store.getState().selectionDirty ? this.checkSelection(data, 'every', false) : this.checkSelection(data, 'every', false) || this.checkSelection(data, 'every', true);
      }

      return checked;
    }
  }, {
    key: "getIndeterminateState",
    value: function getIndeterminateState(props) {
      var store = props.store,
          data = props.data;
      var indeterminate;

      if (!data.length) {
        indeterminate = false;
      } else {
        indeterminate = store.getState().selectionDirty ? this.checkSelection(data, 'some', false) && !this.checkSelection(data, 'every', false) : this.checkSelection(data, 'some', false) && !this.checkSelection(data, 'every', false) || this.checkSelection(data, 'some', true) && !this.checkSelection(data, 'every', true);
      }

      return indeterminate;
    }
  }, {
    key: "renderMenus",
    value: function renderMenus(selections) {
      var onSelect = this.props.onSelect;
      return selections.map(function (selection, index) {
        return /*#__PURE__*/_react["default"].createElement(_menu["default"].Item, {
          key: selection.key || index
        }, /*#__PURE__*/_react["default"].createElement("div", {
          onClick: function onClick() {
            return onSelect(selection.key, index, selection.onSelect);
          }
        }, selection.text));
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          disabled = _this$props2.disabled,
          prefixCls = _this$props2.prefixCls,
          checkboxPrefixCls = _this$props2.checkboxPrefixCls,
          selections = _this$props2.selections,
          getPopupContainer = _this$props2.getPopupContainer,
          menuProps = _this$props2.menuProps,
          dropdownProps = _this$props2.dropdownProps;
      var _this$state = this.state,
          checked = _this$state.checked,
          indeterminate = _this$state.indeterminate;
      var selectionPrefixCls = "".concat(prefixCls, "-selection");
      var customSelections = null;

      if (selections) {
        var newSelections = Array.isArray(selections) ? this.defaultSelections.concat(selections) : this.defaultSelections;

        var menu = /*#__PURE__*/_react["default"].createElement(_menu["default"], (0, _extends2["default"])({}, menuProps, {
          className: "".concat(selectionPrefixCls, "-menu"),
          selectedKeys: []
        }), this.renderMenus(newSelections));

        customSelections = newSelections.length > 0 ? /*#__PURE__*/_react["default"].createElement(_dropdown["default"], (0, _extends2["default"])({}, dropdownProps, {
          overlay: menu,
          getPopupContainer: getPopupContainer
        }), /*#__PURE__*/_react["default"].createElement("div", {
          className: "".concat(selectionPrefixCls, "-down")
        }, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
          type: "expand_more"
        }))) : null;
      }

      return /*#__PURE__*/_react["default"].createElement("div", {
        className: selectionPrefixCls
      }, /*#__PURE__*/_react["default"].createElement(_checkbox["default"], {
        prefixCls: checkboxPrefixCls,
        className: (0, _classnames["default"])((0, _defineProperty2["default"])({}, "".concat(selectionPrefixCls, "-select-all-custom"), customSelections)),
        checked: checked,
        indeterminate: indeterminate,
        disabled: disabled,
        onChange: this.handleSelectAllChagne
      }), customSelections);
    }
  }]);
  return SelectionCheckboxAll;
}(_react.Component);

exports["default"] = SelectionCheckboxAll;
//# sourceMappingURL=SelectionCheckboxAll.js.map
