import _extends from "@babel/runtime/helpers/esm/extends";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
var _excluded = ["prefixCls", "className", "value", "defaultValue", "disabled", "name", "onChange", "children", "error", "inputProps", "size", "matchingCondition", "status", "errorIcon", "loadingIcon", "successIcon", "showStatusIcon"];

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
/**
 * CheckboxGroup
 */

var CheckboxGroup = /*#__PURE__*/function (_Component) {
  _inherits(CheckboxGroup, _Component);

  var _super = _createSuper(CheckboxGroup);

  function CheckboxGroup(props) {
    var _this;

    _classCallCheck(this, CheckboxGroup);

    _this = _super.call(this, props);

    _defineProperty(_assertThisInitialized(_this), "onItemChange", function (itemValue) {
      return function (e) {
        var _this$props = _this.props,
            checkMatching = _this$props.checkMatching,
            onChange = _this$props.onChange;
        var value = _this.isControlled ? _this.props.value : _this.state.value;
        value = Array.isArray(value) ? value : [value];
        var isContained = checkMatching ? checkMatching(itemValue, value) : value.find(function (v) {
          return itemValue === v;
        });

        if (!isContained) {
          value.push(itemValue);
        } else {
          value.splice(value.indexOf(isContained), 1);
        }

        if (!_this.isControlled) {
          _this.setState({
            value: value
          });
        }

        if (onChange) {
          onChange(e, value);
        }
      };
    });

    _this.isControlled = props.value != null;

    if (!_this.isControlled) {
      _this.state = {
        value: props.defaultValue
      };
    } else {
      _this.state = {};
    }

    return _this;
  }

  _createClass(CheckboxGroup, [{
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(newProps) {
      if (!this.isControlled && newProps.value != null) {
        this.isControlled = true;
        this.setState({
          value: null
        });
      }

      if (!this.isControlled && newProps.defaultValue !== this.props.defaultValue) {
        this.setState({
          value: newProps.defaultValue
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props2 = this.props,
          prefixCls = _this$props2.prefixCls,
          className = _this$props2.className,
          propsValue = _this$props2.value,
          defaultValue = _this$props2.defaultValue,
          disabled = _this$props2.disabled,
          name = _this$props2.name,
          onChange = _this$props2.onChange,
          children = _this$props2.children,
          error = _this$props2.error,
          inputProps = _this$props2.inputProps,
          size = _this$props2.size,
          matchingCondition = _this$props2.matchingCondition,
          status = _this$props2.status,
          errorIcon = _this$props2.errorIcon,
          loadingIcon = _this$props2.loadingIcon,
          successIcon = _this$props2.successIcon,
          showStatusIcon = _this$props2.showStatusIcon,
          other = _objectWithoutProperties(_this$props2, _excluded);

      var value = this.isControlled ? propsValue : this.state.value;
      value = Array.isArray(value) ? _toConsumableArray(value) : [value];
      var items = React.Children.map(children, function (child, index) {
        if (! /*#__PURE__*/React.isValidElement(child)) {
          return null;
        }

        var checked = matchingCondition ? !!matchingCondition(child.props.value, value) : value.indexOf(child.props.value) !== -1;
        return /*#__PURE__*/React.cloneElement(child, {
          key: child.props.value || index,
          checked: checked,
          size: size,
          name: name || child.props.name,
          disabled: disabled || child.props.disabled,
          onChange: _this2.onItemChange(child.props.value)
        });
      });
      var classes = classNames(prefixCls, _defineProperty({}, "".concat(prefixCls, "-disabled"), disabled), className);
      return /*#__PURE__*/React.createElement("div", _extends({
        className: classes
      }, other), items);
    }
  }]);

  return CheckboxGroup;
}(Component);

_defineProperty(CheckboxGroup, "propTypes", {
  prefixCls: PropTypes.string,

  /** 子组件name */
  name: PropTypes.string,

  /** 是否禁用 */
  disabled: PropTypes.bool,

  /** 当前值（适用于受控组件） */
  value: PropTypes.array,

  /** 默认值 */
  defaultValue: PropTypes.oneOfType([PropTypes.array, PropTypes.string]),
  className: PropTypes.string,

  /** 选中状态改变时的回调 function(e, value) */
  onChange: PropTypes.func,

  /** 尺寸 */
  size: PropTypes.oneOf(['small', 'medium', 'large']),

  /** 定制判断值匹配的方法 */
  checkMatching: PropTypes.func
});

_defineProperty(CheckboxGroup, "displayName", 'NeatUI.CheckboxGroup');

_defineProperty(CheckboxGroup, "defaultProps", {
  defaultValue: [],
  prefixCls: 'ne-checkbox-group',
  disabled: false
});

export { CheckboxGroup as default };