import { defineComponent, computed, ref, watch, provide, createVNode } from 'vue';
import { useInjectFormItemContext } from '../../form/src/FormItemContext.mjs';
import useConfigInject from '../../config-provider/src/hooks/useConfigInject.mjs';
import useStyle from '../style/index.mjs';
import ACheckbox from './Checkbox.mjs';
import { CheckboxGroupContextKey } from './interface.mjs';
import { checkboxGroupProps } from './props.mjs';

var ACheckboxGroup = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ACheckboxGroup",
  inheritAttrs: false,
  props: checkboxGroupProps(),
  // emits: ['change', 'update:value'],
  setup(props, {
    slots,
    attrs,
    emit,
    expose
  }) {
    const formItemContext = useInjectFormItemContext();
    const {
      prefixCls,
      direction
    } = useConfigInject("checkbox", props);
    const groupPrefixCls = computed(() => `${prefixCls.value}-group`);
    const [wrapSSR, hashId] = useStyle(groupPrefixCls);
    const mergedValue = ref((props.value === void 0 ? props.defaultValue : props.value) || []);
    watch(() => props.value, () => {
      mergedValue.value = props.value || [];
    });
    const options = computed(() => {
      return props.options.map((option) => {
        if (typeof option === "string" || typeof option === "number") {
          return {
            label: option,
            value: option
          };
        }
        return option;
      });
    });
    const triggerUpdate = ref(Symbol("TriggerUpdateKey"));
    const registeredValuesMap = 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 = ref(/* @__PURE__ */ new Map());
    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();
    };
    provide(CheckboxGroupContextKey, {
      cancelValue,
      registerValue,
      toggleOption,
      mergedValue,
      name: computed(() => props.name),
      disabled: 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) => createVNode(ACheckbox, {
          "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(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))]));
    };
  }
});

export { ACheckboxGroup as default };
//# sourceMappingURL=Group.mjs.map
