'use strict';

exports.__esModule = true;

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

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

/**
 * @category controls
 * @component checkbox
 * @variations collab-ui-react
 */

var CheckboxGroup = function (_React$Component) {
  (0, _inherits3.default)(CheckboxGroup, _React$Component);

  function CheckboxGroup() {
    var _temp, _this, _ret;

    (0, _classCallCheck3.default)(this, CheckboxGroup);

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

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.state = {
      values: []
    }, _this.handleToggle = function (value) {
      var newValues = void 0;
      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([].concat(_this.props.values, [value]));
      }

      _this.setState({
        values: newValues
      });
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

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

  CheckboxGroup.prototype.render = function render() {
    var _this2 = this;

    var _props = this.props,
        children = _props.children,
        name = _props.name;
    var values = this.state.values;


    var addHandlersToChildren = function addHandlersToChildren() {
      return _react2.default.Children.map(children, function (child) {
        var value = child.props.value;

        return _react2.default.cloneElement(child, {
          name: name,
          checked: values.includes(value),
          'aria-checked': values.includes(value),
          onChange: function onChange() {
            return _this2.handleToggle(value);
          }
        });
      });
    };

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

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

CheckboxGroup.displayName = 'CheckboxGroup';
exports.default = CheckboxGroup;


CheckboxGroup.defaultProps = {
  values: [],
  onChange: function onChange() {},
  name: ''
};
module.exports = exports['default'];