"use strict";

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

exports.__esModule = true;
exports.withSelectedIds = exports.passedWithSelectedIdsPropsTypes = void 0;

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

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

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

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var passedWithSelectedIdsPropsTypes = {
  selectedIds: _propTypes.default.object.isRequired,
  onSelectId: _propTypes.default.func.isRequired,
  onSelectIds: _propTypes.default.func.isRequired,
  onClearSelectedIds: _propTypes.default.func.isRequired,
  onUpdateSelectedIds: _propTypes.default.func.isRequired
};
exports.passedWithSelectedIdsPropsTypes = passedWithSelectedIdsPropsTypes;

var withSelectedIds = function withSelectedIds(Component) {
  var _class, _temp;

  return _temp = _class =
  /*#__PURE__*/
  function (_React$PureComponent) {
    (0, _inheritsLoose2.default)(_class, _React$PureComponent);

    function _class() {
      var _this;

      for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }

      _this = _React$PureComponent.call.apply(_React$PureComponent, [this].concat(args)) || this;
      (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "state", {
        selectedIds: {}
      });
      (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleClearSelectedIds", function () {
        _this.setState({
          selectedIds: {}
        });
      });
      (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleUpdateSelectedIds", function (selectedIds) {
        _this.setState({
          selectedIds: selectedIds
        });
      });
      (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleSelectId", function (id, isSelected) {
        var selectedIds = Object.assign({}, _this.state.selectedIds);

        if (isSelected) {
          selectedIds[id] = true;
        } else {
          delete selectedIds[id];
        }

        _this.setState({
          selectedIds: selectedIds
        });
      });
      (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleSelectIds", function (ids, isSelected) {
        if (isSelected === void 0) {
          isSelected = true;
        }

        var selectedIds = Object.assign({}, _this.state.selectedIds);
        ids.forEach(function (id) {
          if (isSelected) {
            selectedIds[id] = true;
          } else {
            delete selectedIds[id];
          }
        });

        _this.setState({
          selectedIds: selectedIds
        });
      });
      return _this;
    }

    var _proto = _class.prototype;

    _proto.getSelectedIdsProps = function getSelectedIdsProps() {
      return {
        selectedIds: this.state.selectedIds,
        onSelectId: this.handleSelectId,
        onSelectIds: this.handleSelectIds,
        onClearSelectedIds: this.handleClearSelectedIds,
        onUpdateSelectedIds: this.handleUpdateSelectedIds
      };
    };

    _proto.render = function render() {
      return _react.default.createElement(Component, (0, _extends2.default)({}, this.getSelectedIdsProps(), this.props));
    };

    return _class;
  }(_react.default.PureComponent), (0, _defineProperty2.default)(_class, "displayName", "WithSelectedIds(" + (Component.displayName || Component.name || 'Component') + ")"), _temp;
};

exports.withSelectedIds = withSelectedIds;