"use strict";

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

require("core-js/modules/es6.function.name");

require("core-js/modules/es7.array.includes");

require("core-js/modules/es6.string.includes");

require("core-js/modules/web.dom.iterable");

require("core-js/modules/es6.array.iterator");

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

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

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }

/**
 * @category controls
 * @component checkbox
 * @variations collab-ui-react
 */
var CheckboxGroup =
/*#__PURE__*/
function (_React$Component) {
  _inheritsLoose(CheckboxGroup, _React$Component);

  function CheckboxGroup() {
    var _this;

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

    _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
    _this.state = {
      values: []
    };

    _this.handleToggle = function (value) {
      var newValues;
      var onChange = _this.props.onChange;
      var values = _this.state.values;
      var isActive = values.includes(value);

      if (isActive) {
        newValues = values.filter(function (v) {
          return v !== value;
        });
        onChange(_this.props.values.filter(function (n) {
          return n !== value;
        }));
      } else {
        newValues = values.concat(value);
        onChange(_this.props.values.concat([value]));
      }

      _this.setState({
        values: newValues
      });
    };

    return _this;
  }

  var _proto = CheckboxGroup.prototype;

  _proto.componentWillMount = function componentWillMount() {
    if (this.props.values) {
      this.setState({
        values: this.props.values
      });
    }
  };

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

    var _this$props = this.props,
        children = _this$props.children,
        name = _this$props.name;
    var values = this.state.values;

    var addHandlersToChildren = function addHandlersToChildren() {
      return _react.default.Children.map(children, function (child) {
        var value = child.props.value;
        return _react.default.cloneElement(child, {
          name: name,
          checked: values.includes(value),
          'aria-checked': values.includes(value),
          onChange: function onChange() {
            return _this2.handleToggle(value);
          }
        });
      });
    };

    return _react.default.createElement("div", {
      className: "cui-checkbox-group"
    }, addHandlersToChildren());
  };

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

CheckboxGroup.displayName = 'CheckboxGroup';
CheckboxGroup.propTypes = {
  /**
   * optional children prop type
   */
  children: _propTypes.default.node,

  /**
   * Callback fired when a button is pressed, `onChange` will be called with the value or
   * array of active values
   *
   * @controllable values
   */
  onChange: _propTypes.default.func,

  /**
   * An array of values, of the active (pressed) buttons
   *
   * @controllable onChange
   */
  values: _propTypes.default.array,

  /**
   * An HTML `<input>` name for each child button.
   *
   */
  name: _propTypes.default.string
};
CheckboxGroup.defaultProps = {
  values: [],
  onChange: function onChange() {},
  name: ''
};
var _default = CheckboxGroup;
exports.default = _default;