import { defineComponent, inject, computed, watchEffect, onBeforeUnmount, onMounted, ref, createVNode, mergeProps } from 'vue';
import classNames from '../_util/classNames.mjs';
import VcCheckbox from '../vc-checkbox/Checkbox.mjs';
import { flattenChildren } from '../_util/props-util/index.mjs';
import warning from '../_util/warning.mjs';
import { useInjectFormItemContext, FormItemInputContext } from '../form/FormItemContext.mjs';
import '../hooks/index.mjs';
import useStyle from './style/index.mjs';
import { checkboxProps, CheckboxGroupContextKey } from './interface.mjs';
import useConfigInject from '../config-provider/hooks/useConfigInject.mjs';

var Checkbox = /* @__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 [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(() => {
      warning(!!(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);
    };
    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,
        onInput,
        class: className,
        style,
        ...restAttrs
      } = attrs;
      const checkboxProps2 = {
        ...restProps,
        id,
        prefixCls: prefixCls.value,
        ...restAttrs,
        disabled: mergedDisabled.value
      };
      if (checkboxGroup && !skipGroup) {
        checkboxProps2.onChange = (...args) => {
          emit("change", ...args);
          checkboxGroup.toggleOption({
            label: children,
            value: props.value
          });
        };
        checkboxProps2.name = checkboxGroup.name.value;
        checkboxProps2.checked = checkboxGroup.mergedValue.value.includes(props.value);
        checkboxProps2.disabled = mergedDisabled.value || checkboxGroup.disabled.value;
        checkboxProps2.indeterminate = indeterminate;
      } else {
        checkboxProps2.onChange = handleChange;
      }
      const classString = classNames({
        [`${prefixCls.value}-wrapper`]: true,
        [`${prefixCls.value}-rtl`]: direction.value === "rtl",
        [`${prefixCls.value}-wrapper-checked`]: checkboxProps2.checked,
        [`${prefixCls.value}-wrapper-disabled`]: checkboxProps2.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, mergeProps({
        "aria-checked": ariaChecked
      }, checkboxProps2, {
        "class": checkboxClass,
        "ref": checkboxRef,
        "disabled": mergedDisabled.value
      }), null), children.length ? createVNode("span", null, [children]) : null]));
    };
  }
});

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