"use strict";

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

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

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

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

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

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 _checkbox = _interopRequireDefault(require("../checkbox"));

var _radio = _interopRequireDefault(require("../radio"));

var _excluded = ["type", "rowIndex", "radioPrefixCls", "checkboxPrefixCls"];

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

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

  function SelectionBox(props) {
    var _this;

    (0, _classCallCheck2["default"])(this, SelectionBox);
    _this = _super.call(this, props);
    _this.state = {
      checked: _this.getCheckState(props)
    };
    return _this;
  }

  (0, _createClass2["default"])(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 = (0, _objectWithoutProperties2["default"])(_this$props, _excluded);
      var checked = this.state.checked;

      if (type === 'radio') {
        return /*#__PURE__*/_react["default"].createElement(_radio["default"], (0, _extends2["default"])({
          prefixCls: radioPrefixCls,
          checked: checked,
          value: rowIndex
        }, rest));
      }

      return /*#__PURE__*/_react["default"].createElement(_checkbox["default"], (0, _extends2["default"])({
        prefixCls: checkboxPrefixCls,
        checked: checked
      }, rest));
    }
  }]);
  return SelectionBox;
}(_react.Component);

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