'use strict';

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

var vue = require('vue');
require('../../../utils/index.cjs');
require('../../../vue-components/index.cjs');
var FormItemContext = require('../../form/src/FormItemContext.cjs');
var useConfigInject = require('../../config-provider/src/hooks/useConfigInject.cjs');
var index = require('../style/index.cjs');
require('../../config-provider/index.cjs');
var _interface = require('./interface.cjs');
var props = require('./props.cjs');
var disabledContext = require('../../config-provider/src/context/disabledContext.cjs');
var log = require('../../../utils/log.cjs');
var props$1 = require('../../../utils/vue/props.cjs');
var _class = require('../../../utils/dom/class.cjs');
var Checkbox = require('../../../vue-components/vc-checkbox/src/Checkbox.cjs');

var ACheckbox = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ACheckbox",
  inheritAttrs: false,
  __ANT_CHECKBOX: true,
  props: props.checkboxProps(),
  // emits: ['change', 'update:checked'],
  setup(props, {
    emit,
    attrs,
    slots,
    expose
  }) {
    const formItemContext = FormItemContext.useInjectFormItemContext();
    const formItemInputContext = FormItemContext.FormItemInputContext.useInject();
    const {
      prefixCls,
      direction,
      disabled
    } = useConfigInject.default("checkbox", props);
    const contextDisabled = disabledContext.useInjectDisabled();
    const [wrapSSR, hashId] = index.default(prefixCls);
    const checkboxGroup = vue.inject(_interface.CheckboxGroupContextKey, void 0);
    const uniId = Symbol("checkboxUniId");
    const mergedDisabled = vue.computed(() => {
      return (checkboxGroup == null ? void 0 : checkboxGroup.disabled.value) || disabled.value;
    });
    vue.watchEffect(() => {
      if (!props.skipGroup && checkboxGroup)
        checkboxGroup.registerValue(uniId, props.value);
    });
    vue.onBeforeUnmount(() => {
      if (checkboxGroup)
        checkboxGroup.cancelValue(uniId);
    });
    vue.onMounted(() => {
      log.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 = vue.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 = props$1.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 = _class.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 = _class.classNames({
        [`${prefixCls.value}-indeterminate`]: indeterminate
      }, hashId.value);
      const ariaChecked = indeterminate ? "mixed" : void 0;
      return wrapSSR(vue.createVNode("label", {
        "class": classString,
        "style": style,
        "onMouseenter": onMouseenter,
        "onMouseleave": onMouseleave
      }, [vue.createVNode(Checkbox.default, {
        "aria-checked": ariaChecked,
        ...getCheckboxProps,
        "class": checkboxClass,
        "ref": checkboxRef
      }, null), children.length ? vue.createVNode("span", null, [children]) : null]));
    };
  }
});

exports.default = ACheckbox;
//# sourceMappingURL=Checkbox.cjs.map
