'use strict';

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

exports.__esModule = true;
exports.ListControlCheckbox = void 0;

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 _lodash = _interopRequireDefault(require("lodash"));

var _Checkbox = require("../../form/Checkbox");

var ListControlCheckbox =
/*#__PURE__*/
function (_React$Component) {
  (0, _inheritsLoose2.default)(ListControlCheckbox, _React$Component);

  function ListControlCheckbox() {
    return _React$Component.apply(this, arguments) || this;
  }

  var _proto = ListControlCheckbox.prototype;

  _proto.getVisibleItems = function getVisibleItems() {
    return this.context.listStore.getPrimaryKeys();
  };

  _proto.onChange = function onChange() {
    var _this = this;

    var formRecords = this.context.form.value;
    var items = this.getVisibleItems();
    var checked = this.refs.input.getValue();
    var newFormValue = {};
    items.forEach(function (id) {
      var _$extend;

      newFormValue[id] = _lodash.default.extend({}, formRecords[id], (_$extend = {}, _$extend[_this.props.dependentFieldName] = checked, _$extend));
    });
    this.context.form.setValue(newFormValue);
  };

  _proto.render = function render() {
    var _this2 = this;

    var formRecords = this.context.form.value;
    var items = this.getVisibleItems();
    var checkboxValue = false;

    if (items.length > 0) {
      var selectedItems = items.filter(function (id) {
        return formRecords[id] && formRecords[id][_this2.props.dependentFieldName];
      });

      if (selectedItems.length === items.length) {
        checkboxValue = true;
      }

      if (selectedItems.length > 0 && selectedItems.length < items.length) {
        checkboxValue = null;
      }
    }

    return _react.default.createElement(_Checkbox.Checkbox, {
      "aria-checked": checkboxValue === null ? 'mixed' : checkboxValue,
      checked: checkboxValue,
      hiddenLabel: this.props.hiddenLabel,
      disabled: items.length === 0,
      onChange: this.onChange.bind(this),
      ref: "input",
      label: this.props.label,
      groupClassName: "rc-list-checkbox",
      "aria-labelledby": this.props.ariaLabelledById,
      removeName: true
    });
  };

  return ListControlCheckbox;
}(_react.default.Component);

exports.ListControlCheckbox = ListControlCheckbox;
(0, _defineProperty2.default)(ListControlCheckbox, "contextTypes", {
  listStore: _propTypes.default.object,
  form: _propTypes.default.object
});
(0, _defineProperty2.default)(ListControlCheckbox, "propTypes", {
  label: _propTypes.default.string,
  dependentFieldName: _propTypes.default.string.isRequired,
  ariaLabelledById: _propTypes.default.string,
  hiddenLabel: _propTypes.default.bool
});
(0, _defineProperty2.default)(ListControlCheckbox, "defaultProps", {
  label: 'TRANSLATE:SELECT_ALL',
  ariaLabelledById: null,
  hiddenLabel: true
});