var __assign = this && this.__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

var __rest = this && this.__rest || function (s, e) {
  var t = {};

  for (var p in s) {
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  }

  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};

var __spreadArray = this && this.__spreadArray || function (to, from) {
  for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) {
    to[j] = from[i];
  }

  return to;
};

import { jsx as _jsx } from "react/jsx-runtime";
import * as React from 'react';
import classNames from 'classnames';
import useStaticCallback from "../../hooks/useStaticCallback";
import formateOptions from "../_utils/formateOptions";
import { CheckboxMemo as Checkbox } from "./Checkbox";
var prefixCls = 'dmc-checkbox-group';
var defaultProps = {
  options: [],
  valueKey: 'value',
  labelKey: 'label'
};
var CheckboxGroup = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var _a = props,
      type = _a.type,
      size = _a.size,
      options = _a.options,
      valueKey = _a.valueKey,
      labelKey = _a.labelKey,
      onChange = _a.onChange,
      defaultValue = _a.defaultValue,
      value = _a.value,
      disabled = _a.disabled,
      className = _a.className,
      itemClassName = _a.itemClassName,
      renderItem = _a.renderItem,
      restProps = __rest(_a, ["type", "size", "options", "valueKey", "labelKey", "onChange", "defaultValue", "value", "disabled", "className", "itemClassName", "renderItem"]);

  var _b = React.useState(defaultValue || []),
      unControlVal = _b[0],
      setUnControlVal = _b[1];

  var opts = React.useMemo(function () {
    return formateOptions(options, labelKey, valueKey);
  }, [options, labelKey, valueKey]);
  var isControl = ('value' in props);
  var val = isControl ? value !== null && value !== void 0 ? value : [] : unControlVal;
  var onCheckboxChange = useStaticCallback(function (_checked, value) {
    var curr = __spreadArray([], val);

    var index = curr.indexOf(value);
    index === -1 ? curr.push(value) : curr.splice(index, 1);
    !isControl && setUnControlVal(curr);
    onChange && onChange(curr);
  });
  return _jsx("div", __assign({}, restProps, {
    className: classNames(prefixCls, className),
    ref: ref
  }, {
    children: opts.map(function (it) {
      var vk = it[valueKey];
      return _jsx(Checkbox, __assign({
        value: vk,
        type: type,
        size: size,
        disabled: it.disabled || disabled,
        checked: val.includes(vk),
        onChange: onCheckboxChange,
        className: itemClassName,
        title: it[labelKey]
      }, {
        children: renderItem && renderItem(it)
      }), vk);
    })
  }), void 0);
});
CheckboxGroup.defaultProps = defaultProps;
CheckboxGroup.displayName = 'CheckboxGroup';
export default CheckboxGroup;
var CheckboxGroupMemo = /*#__PURE__*/React.memo(CheckboxGroup);
CheckboxGroupMemo.displayName = 'CheckboxGroupMemo';
export { CheckboxGroupMemo };

