var _excluded = ["icon", "iconPosition", "allowClear", "multiple", "value", "list", "readOnly", "disabled", "children", "onChange"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { forwardRef, useRef, useImperativeHandle } from 'react';
import Checkbox from "../Checkbox";

// Checkbox-Group
var CheckboxGroup = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var icon = _ref.icon,
    _ref$iconPosition = _ref.iconPosition,
    iconPosition = _ref$iconPosition === void 0 ? 'left' : _ref$iconPosition,
    allowClear = _ref.allowClear,
    multiple = _ref.multiple,
    value = _ref.value,
    list = _ref.list,
    readOnly = _ref.readOnly,
    disabled = _ref.disabled,
    children = _ref.children,
    _onChange = _ref.onChange,
    props = _objectWithoutProperties(_ref, _excluded);
  // 节点
  var rootRef = useRef(null);
  useImperativeHandle(ref, function () {
    return {
      rootDOM: rootRef.current,
      getRootDOM: function getRootDOM() {
        return rootRef.current;
      }
    };
  });
  return /*#__PURE__*/React.createElement("div", _extends({}, props, {
    disabled: disabled,
    readOnly: readOnly,
    className: "checkbox-group".concat(props.className ? ' ' + props.className : ''),
    ref: rootRef
  }), Array.isArray(list) && list.length ? list.map(function (item) {
    var _value$findIndex;
    if (!(item !== null && item !== void 0 && item.id)) return null;
    return /*#__PURE__*/React.createElement(Checkbox, {
      key: item.id,
      icon: icon,
      iconPosition: iconPosition,
      checked: (value === null || value === void 0 || (_value$findIndex = value.findIndex) === null || _value$findIndex === void 0 ? void 0 : _value$findIndex.call(value, function (valueItem) {
        return (valueItem === null || valueItem === void 0 ? void 0 : valueItem.id) === item.id;
      })) >= 0,
      onChange: function onChange(checked) {
        var newValue = null;
        // 多选
        if (multiple) {
          if (!checked) {
            newValue = value.filter(function (valueItem) {
              return (valueItem === null || valueItem === void 0 ? void 0 : valueItem.id) !== item.id;
            });
          } else {
            newValue = [].concat(_toConsumableArray(value || []), [item]);
          }
        }
        // 单选
        else {
          if (!checked) {
            newValue = allowClear ? null : [item];
          } else {
            newValue = [item];
          }
        }
        _onChange && _onChange(newValue);
      }
    }, item.name || '');
  }) : null);
});
export default CheckboxGroup;