'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
var FormItemContext = require('../../form/src/FormItemContext.cjs');
var useConfigInject = require('../../config-provider/src/hooks/useConfigInject.cjs');
var index = require('../style/index.cjs');
var Checkbox = require('./Checkbox.cjs');
var _interface = require('./interface.cjs');
var props = require('./props.cjs');

var ACheckboxGroup = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ACheckboxGroup",
  inheritAttrs: false,
  props: props.checkboxGroupProps(),
  // emits: ['change', 'update:value'],
  setup(props, {
    slots,
    attrs,
    emit,
    expose
  }) {
    const formItemContext = FormItemContext.useInjectFormItemContext();
    const {
      prefixCls,
      direction
    } = useConfigInject.default("checkbox", props);
    const groupPrefixCls = vue.computed(() => `${prefixCls.value}-group`);
    const [wrapSSR, hashId] = index.default(groupPrefixCls);
    const mergedValue = vue.ref((props.value === void 0 ? props.defaultValue : props.value) || []);
    vue.watch(() => props.value, () => {
      mergedValue.value = props.value || [];
    });
    const options = vue.computed(() => {
      return props.options.map((option) => {
        if (typeof option === "string" || typeof option === "number") {
          return {
            label: option,
            value: option
          };
        }
        return option;
      });
    });
    const triggerUpdate = vue.ref(Symbol("TriggerUpdateKey"));
    const registeredValuesMap = vue.ref(/* @__PURE__ */ new Map());
    const cancelValue = (id) => {
      registeredValuesMap.value.delete(id);
      triggerUpdate.value = Symbol("TriggerUpdateCancelKey");
    };
    const registerValue = (id, value) => {
      registeredValuesMap.value.set(id, value);
      triggerUpdate.value = Symbol("TriggerUpdateRegisterKey");
    };
    const registeredValues = vue.ref(/* @__PURE__ */ new Map());
    vue.watch(triggerUpdate, () => {
      const valuseMap = /* @__PURE__ */ new Map();
      for (const value of registeredValuesMap.value.values())
        valuseMap.set(value, true);
      registeredValues.value = valuseMap;
    });
    const toggleOption = (option) => {
      const optionIndex = mergedValue.value.indexOf(option.value);
      const value = [...mergedValue.value];
      if (optionIndex === -1)
        value.push(option.value);
      else
        value.splice(optionIndex, 1);
      if (props.value === void 0)
        mergedValue.value = value;
      const val = value.filter((v) => registeredValues.value.has(v)).sort((a, b) => {
        const indexA = options.value.findIndex((opt) => opt.value === a);
        const indexB = options.value.findIndex((opt) => opt.value === b);
        return indexA - indexB;
      });
      emit("update:value", val);
      emit("change", val);
      formItemContext.onFieldChange();
    };
    vue.provide(_interface.CheckboxGroupContextKey, {
      cancelValue,
      registerValue,
      toggleOption,
      mergedValue,
      name: vue.computed(() => props.name),
      disabled: vue.computed(() => props.disabled)
    });
    expose({
      mergedValue
    });
    return () => {
      var _a;
      const {
        id = formItemContext.id.value
      } = props;
      let children = null;
      if (options.value && options.value.length > 0) {
        children = options.value.map((option) => vue.createVNode(Checkbox.default, {
          "prefixCls": prefixCls.value,
          "key": option.value.toString(),
          "disabled": "disabled" in option ? option.disabled : props.disabled,
          "indeterminate": option.indeterminate,
          "value": option.value,
          "checked": mergedValue.value.includes(option.value),
          "onChange": option.onChange,
          "class": `${groupPrefixCls.value}-item`
        }, {
          default: () => {
            var _a2;
            return [slots.label !== void 0 ? (_a2 = slots.label) == null ? void 0 : _a2.call(slots, option) : option.label];
          }
        }));
      }
      return wrapSSR(vue.createVNode("div", {
        ...attrs,
        "class": [groupPrefixCls.value, {
          [`${groupPrefixCls.value}-rtl`]: direction.value === "rtl"
        }, attrs.class, hashId.value],
        "id": id
      }, [children || ((_a = slots.default) == null ? void 0 : _a.call(slots))]));
    };
  }
});

exports.default = ACheckboxGroup;
//# sourceMappingURL=Group.cjs.map
