import { createContext, useState, useContext, useCallback } from "react";
import Checkbox from "./checkbox.js";
import cs from "../_util/classNames.js";
import { ConfigContext } from "../ConfigProvider/index.js";
import useMergeValue from "../_util/hooks/useMergeValue.js";
import { isArray, isObject } from "../_util/is.js";
import { jsx } from "../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js";
var __read = globalThis && globalThis.__read || function(o, n) {
  var m = typeof Symbol === "function" && o[Symbol.iterator];
  if (!m)
    return o;
  var i = m.call(o), r, ar = [], e;
  try {
    while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
      ar.push(r.value);
  } catch (error) {
    e = {
      error
    };
  } finally {
    try {
      if (r && !r.done && (m = i["return"]))
        m.call(i);
    } finally {
      if (e)
        throw e.error;
    }
  }
  return ar;
};
var __spreadArray = globalThis && globalThis.__spreadArray || function(to, from, pack) {
  if (pack || arguments.length === 2)
    for (var i = 0, l = from.length, ar; i < l; i++) {
      if (ar || !(i in from)) {
        if (!ar)
          ar = Array.prototype.slice.call(from, 0, i);
        ar[i] = from[i];
      }
    }
  return to.concat(ar || Array.prototype.slice.call(from));
};
var CheckboxGroupContext = createContext({
  isCheckboxGroup: false,
  checkboxGroupValue: [],
  onGroupChange: function() {
  },
  registerValue: function() {
  },
  unRegisterValue: function() {
  }
});
function Group(props) {
  var _a;
  var _b = __read(useMergeValue([], {
    defaultValue: "defaultValue" in props ? props.defaultValue || [] : void 0,
    value: "value" in props ? props.value || [] : void 0
  }), 2), value = _b[0], setValue = _b[1];
  var _c = __read(useState([]), 2), allOptionValues = _c[0], setAllOptionValues = _c[1];
  var _d = useContext(ConfigContext), getPrefixCls = _d.getPrefixCls, rtl = _d.rtl;
  var disabled = props.disabled, options = props.options, style = props.style, className = props.className, error = props.error, children = props.children, _e = props.direction, direction = _e === void 0 ? "horizontal" : _e;
  var prefixCls = getPrefixCls("checkbox");
  var classNames = cs(prefixCls + "-group", (_a = {}, _a[prefixCls + "-group-is-error"] = error, _a[prefixCls + "-group-direction-" + direction] = direction, _a[prefixCls + "-group-rtl"] = rtl, _a), className);
  var onChange = useCallback(function(optionValue, checked, e) {
    var newVal = value.slice();
    if (checked) {
      newVal.push(optionValue);
    } else {
      newVal.splice(value.indexOf(optionValue), 1);
    }
    setValue(newVal);
    props.onChange && props.onChange(newVal.filter(function(v) {
      return allOptionValues.indexOf(v) > -1;
    }), e);
  }, [value, props.onChange, allOptionValues]);
  return /* @__PURE__ */ jsx("span", {
    className: classNames,
    style,
    children: /* @__PURE__ */ jsx(CheckboxGroupContext.Provider, {
      value: {
        isCheckboxGroup: true,
        checkboxGroupValue: value,
        onGroupChange: onChange,
        disabled,
        registerValue: function(value2) {
          setAllOptionValues(function(allOptionValues2) {
            return Array.from(new Set(__spreadArray(__spreadArray([], __read(allOptionValues2), false), [value2], false)));
          });
        },
        unRegisterValue: function(value2) {
          setAllOptionValues(function(allOptionValues2) {
            return allOptionValues2.filter(function(x) {
              return x !== value2;
            });
          });
        }
      },
      children: isArray(options) ? options.map(function(option) {
        var label = isObject(option) ? option.label : option;
        var checkValue = isObject(option) ? option.value : option;
        return /* @__PURE__ */ jsx(Checkbox, {
          disabled: disabled || isObject(option) && option.disabled,
          value: checkValue,
          children: label
        }, checkValue);
      }) : children
    })
  });
}
Group.displayName = "CheckboxGroup";
export {
  CheckboxGroupContext,
  Group as default
};
//# sourceMappingURL=group.js.map
