import { defineComponent, inject, computed, watchEffect, onBeforeUnmount, onMounted, ref, createVNode } from 'vue';
import '../../../utils/index.mjs';
import '../../../vue-components/index.mjs';
import { useInjectFormItemContext, FormItemInputContext } from '../../form/src/FormItemContext.mjs';
import useConfigInject from '../../config-provider/src/hooks/useConfigInject.mjs';
import useStyle from '../style/index.mjs';
import '../../config-provider/index.mjs';
import { CheckboxGroupContextKey } from './interface.mjs';
import { checkboxProps } from './props.mjs';
import { useInjectDisabled } from '../../config-provider/src/context/disabledContext.mjs';
import { devWarning } from '../../../utils/log.mjs';
import { flattenChildren } from '../../../utils/vue/props.mjs';
import { classNames } from '../../../utils/dom/class.mjs';
import VcCheckbox from '../../../vue-components/vc-checkbox/src/Checkbox.mjs';

var ACheckbox = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ACheckbox",
  inheritAttrs: false,
  __ANT_CHECKBOX: true,
  props: checkboxProps(),
  // emits: ['change', 'update:checked'],
  setup(props, {
    emit,
    attrs,
    slots,
    expose
  }) {
    const formItemContext = useInjectFormItemContext();
    const formItemInputContext = FormItemInputContext.useInject();
    const {
      prefixCls,
      direction,
      disabled
    } = useConfigInject("checkbox", props);
    const contextDisabled = useInjectDisabled();
    const [wrapSSR, hashId] = useStyle(prefixCls);
    const checkboxGroup = inject(CheckboxGroupContextKey, void 0);
    const uniId = Symbol("checkboxUniId");
    const mergedDisabled = computed(() => {
      return (checkboxGroup == null ? void 0 : checkboxGroup.disabled.value) || disabled.value;
    });
    watchEffect(() => {
      if (!props.skipGroup && checkboxGroup)
        checkboxGroup.registerValue(uniId, props.value);
    });
    onBeforeUnmount(() => {
      if (checkboxGroup)
        checkboxGroup.cancelValue(uniId);
    });
    onMounted(() => {
      devWarning(!!(props.checked !== void 0 || checkboxGroup || props.value === void 0), "Checkbox", "`value` is not validate prop, do you mean `checked`?");
    });
    const handleChange = (event) => {
      const targetChecked = event.target.checked;
      emit("update:checked", targetChecked);
      emit("change", event);
      formItemContext.onFieldChange();
    };
    const checkboxRef = ref();
    const focus = () => {
      var _a;
      (_a = checkboxRef.value) == null ? void 0 : _a.focus();
    };
    const blur = () => {
      var _a;
      (_a = checkboxRef.value) == null ? void 0 : _a.blur();
    };
    expose({
      focus,
      blur
    });
    return () => {
      var _a;
      const children = flattenChildren((_a = slots.default) == null ? void 0 : _a.call(slots));
      const {
        indeterminate,
        skipGroup,
        id = formItemContext.id.value,
        ...restProps
      } = props;
      const {
        onMouseenter,
        onMouseleave,
        class: className,
        style,
        ...restAttrs
      } = attrs;
      const getCheckboxProps = {
        ...restProps,
        id,
        prefixCls: prefixCls.value,
        ...restAttrs,
        disabled: mergedDisabled.value
      };
      if (checkboxGroup && !skipGroup) {
        getCheckboxProps.onChange = (...args) => {
          emit("change", ...args);
          checkboxGroup.toggleOption({
            label: children,
            value: props.value
          });
        };
        getCheckboxProps.name = checkboxGroup.name.value;
        getCheckboxProps.checked = checkboxGroup.mergedValue.value.includes(props.value);
        getCheckboxProps.disabled = mergedDisabled.value || contextDisabled.value;
        getCheckboxProps.indeterminate = indeterminate;
      } else {
        getCheckboxProps.onChange = handleChange;
      }
      const classString = classNames({
        [`${prefixCls.value}-wrapper`]: true,
        [`${prefixCls.value}-rtl`]: direction.value === "rtl",
        [`${prefixCls.value}-wrapper-checked`]: getCheckboxProps.checked,
        [`${prefixCls.value}-wrapper-disabled`]: getCheckboxProps.disabled,
        [`${prefixCls.value}-wrapper-in-form-item`]: formItemInputContext.isFormItemInput
      }, className, hashId.value);
      const checkboxClass = classNames({
        [`${prefixCls.value}-indeterminate`]: indeterminate
      }, hashId.value);
      const ariaChecked = indeterminate ? "mixed" : void 0;
      return wrapSSR(createVNode("label", {
        "class": classString,
        "style": style,
        "onMouseenter": onMouseenter,
        "onMouseleave": onMouseleave
      }, [createVNode(VcCheckbox, {
        "aria-checked": ariaChecked,
        ...getCheckboxProps,
        "class": checkboxClass,
        "ref": checkboxRef
      }, null), children.length ? createVNode("span", null, [children]) : null]));
    };
  }
});

export { ACheckbox as default };
//# sourceMappingURL=Checkbox.mjs.map
