import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
var _excluded = ["type", "rowIndex", "radioPrefixCls", "checkboxPrefixCls"];
import React, { Component } from 'react';
import Checkbox from '../checkbox';
import Radio from '../radio';

var SelectionBox = /*#__PURE__*/function (_Component) {
  _inherits(SelectionBox, _Component);

  var _super = _createSuper(SelectionBox);

  function SelectionBox(props) {
    var _this;

    _classCallCheck(this, SelectionBox);

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

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

        _this2.setState({
          checked: checked
        });
      });
    }
  }, {
    key: "getCheckState",
    value: function getCheckState(props) {
      var store = props.store,
          defaultSelection = props.defaultSelection,
          rowIndex = props.rowIndex;
      var checked = false;

      if (store.getState().selectionDirty) {
        checked = store.getState().selectedRowKeys.indexOf(rowIndex) >= 0;
      } else {
        checked = store.getState().selectedRowKeys.indexOf(rowIndex) >= 0 || defaultSelection.indexOf(rowIndex) >= 0;
      }

      return checked;
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
          type = _this$props.type,
          rowIndex = _this$props.rowIndex,
          radioPrefixCls = _this$props.radioPrefixCls,
          checkboxPrefixCls = _this$props.checkboxPrefixCls,
          rest = _objectWithoutProperties(_this$props, _excluded);

      var checked = this.state.checked;

      if (type === 'radio') {
        return /*#__PURE__*/React.createElement(Radio, _extends({
          prefixCls: radioPrefixCls,
          checked: checked,
          value: rowIndex
        }, rest));
      }

      return /*#__PURE__*/React.createElement(Checkbox, _extends({
        prefixCls: checkboxPrefixCls,
        checked: checked
      }, rest));
    }
  }]);

  return SelectionBox;
}(Component);

export { SelectionBox as default };
//# sourceMappingURL=SelectionBox.js.map
