function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

import * as React from 'react';
import classNames from 'classnames';
import { polyfill } from 'react-lifecycles-compat';
import Checkbox from '../checkbox';
import Dropdown from '../dropdown';
import Menu from '../menu';
import Icon from '../icon';

function checkSelection(_ref) {
  var store = _ref.store,
      getCheckboxPropsByItem = _ref.getCheckboxPropsByItem,
      getRecordKey = _ref.getRecordKey,
      data = _ref.data,
      type = _ref.type,
      byDefaultChecked = _ref.byDefaultChecked;
  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;
  });
}

function getIndeterminateState(props) {
  var store = props.store,
      data = props.data;

  if (!data.length) {
    return false;
  }

  var someCheckedNotByDefaultChecked = checkSelection(_extends(_extends({}, props), {
    data: data,
    type: 'some',
    byDefaultChecked: false
  })) && !checkSelection(_extends(_extends({}, props), {
    data: data,
    type: 'every',
    byDefaultChecked: false
  }));
  var someCheckedByDefaultChecked = checkSelection(_extends(_extends({}, props), {
    data: data,
    type: 'some',
    byDefaultChecked: true
  })) && !checkSelection(_extends(_extends({}, props), {
    data: data,
    type: 'every',
    byDefaultChecked: true
  }));

  if (store.getState().selectionDirty) {
    return someCheckedNotByDefaultChecked;
  }

  return someCheckedNotByDefaultChecked || someCheckedByDefaultChecked;
}

function getCheckState(props) {
  var store = props.store,
      data = props.data;

  if (!data.length) {
    return false;
  }

  if (store.getState().selectionDirty) {
    return checkSelection(_extends(_extends({}, props), {
      data: data,
      type: 'every',
      byDefaultChecked: false
    }));
  }

  return checkSelection(_extends(_extends({}, props), {
    data: data,
    type: 'every',
    byDefaultChecked: false
  })) || checkSelection(_extends(_extends({}, props), {
    data: data,
    type: 'every',
    byDefaultChecked: true
  }));
}

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

  var _super = _createSuper(SelectionCheckboxAll);

  function SelectionCheckboxAll(props) {
    var _this;

    _classCallCheck(this, SelectionCheckboxAll);

    _this = _super.call(this, props);
    _this.state = {
      checked: false,
      indeterminate: false
    };

    _this.handleSelectAllChange = function (e) {
      var checked = e.target.checked;

      _this.props.onSelect(checked ? 'all' : 'removeAll', 0, null);
    };

    _this.defaultSelections = props.hideDefaultSelections ? [] : [{
      key: 'all',
      text: props.locale.selectAll
    }, {
      key: 'invert',
      text: props.locale.selectInvert
    }];
    return _this;
  }

  _createClass(SelectionCheckboxAll, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.subscribe();
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      if (this.unsubscribe) {
        this.unsubscribe();
      }
    }
  }, {
    key: "setCheckState",
    value: function setCheckState(props) {
      var checked = getCheckState(props);
      var indeterminate = 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: "subscribe",
    value: function subscribe() {
      var _this2 = this;

      var store = this.props.store;
      this.unsubscribe = store.subscribe(function () {
        _this2.setCheckState(_this2.props);
      });
    }
  }, {
    key: "renderMenus",
    value: function renderMenus(selections) {
      var _this3 = this;

      return selections.map(function (selection, index) {
        return /*#__PURE__*/React.createElement(Menu.Item, {
          key: selection.key || index
        }, /*#__PURE__*/React.createElement("div", {
          onClick: function onClick() {
            _this3.props.onSelect(selection.key, index, selection.onSelect);
          }
        }, selection.text));
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
          disabled = _this$props.disabled,
          prefixCls = _this$props.prefixCls,
          selections = _this$props.selections,
          getPopupContainer = _this$props.getPopupContainer;
      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.createElement(Menu, {
          className: "".concat(selectionPrefixCls, "-menu"),
          selectedKeys: []
        }, this.renderMenus(newSelections));
        customSelections = newSelections.length > 0 ? /*#__PURE__*/React.createElement(Dropdown, {
          overlay: menu,
          getPopupContainer: getPopupContainer
        }, /*#__PURE__*/React.createElement("div", {
          className: "".concat(selectionPrefixCls, "-down")
        }, /*#__PURE__*/React.createElement(Icon, {
          type: "down"
        }))) : null;
      }

      return /*#__PURE__*/React.createElement("div", {
        className: selectionPrefixCls
      }, /*#__PURE__*/React.createElement(Checkbox, {
        className: classNames(_defineProperty({}, "".concat(selectionPrefixCls, "-select-all-custom"), customSelections)),
        checked: checked,
        indeterminate: indeterminate,
        disabled: disabled,
        onChange: this.handleSelectAllChange
      }), customSelections);
    }
  }], [{
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(props, state) {
      var checked = getCheckState(props);
      var indeterminate = getIndeterminateState(props);
      var newState = {};

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

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

      return newState;
    }
  }]);

  return SelectionCheckboxAll;
}(React.Component);

polyfill(SelectionCheckboxAll);
export default SelectionCheckboxAll;