'use strict';

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

var vue = require('vue');
var classNames = require('../_util/classNames.js');
var Checkbox$1 = require('../vc-checkbox/Checkbox.js');
var index$1 = require('../_util/props-util/index.js');
var warning = require('../_util/warning.js');
var FormItemContext = require('../form/FormItemContext.js');
require('../hooks/index.js');
var index = require('./style/index.js');
var _interface = require('./interface.js');
var useConfigInject = require('../config-provider/hooks/useConfigInject.js');

var Checkbox = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ACheckbox",
  inheritAttrs: false,
  __ANT_CHECKBOX: true,
  props: _interface.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 [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(() => {
      warning.default(!!(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 = 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 = index$1.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.default({
        [`${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.default({
        [`${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$1.default, vue.mergeProps({
        "aria-checked": ariaChecked
      }, checkboxProps2, {
        "class": checkboxClass,
        "ref": checkboxRef,
        "disabled": mergedDisabled.value
      }), null), children.length ? vue.createVNode("span", null, [children]) : null]));
    };
  }
});

exports.default = Checkbox;
//# sourceMappingURL=Checkbox.js.map
