"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const event = require("../../../../constants/event.js");
const error = require("../../../../utils/error.js");
const useFormItem = require("../../../form/src/hooks/use-form-item.js");
const constants = require("../constants.js");
const Vue = require("vue");
const useCheckboxEvent = (props, { model, isLimitExceeded, hasOwnLabel, isDisabled, isLabeledByFormItem }) => {
  const checkboxGroup = Vue.inject(constants.checkboxGroupContextKey, void 0);
  const { formItem } = useFormItem.useFormItem();
  const { emit } = Vue.getCurrentInstance();
  function getLabeledValue(value) {
    return [
      true,
      props.trueValue,
      props.trueLabel
    ].includes(value) ? props.trueValue ?? props.trueLabel ?? true : props.falseValue ?? props.falseLabel ?? false;
  }
  function emitChangeEvent(checked, e) {
    emit(event.CHANGE_EVENT, getLabeledValue(checked), e);
  }
  function handleChange(e) {
    if (isLimitExceeded.value) return;
    const target = e.target;
    emit(event.CHANGE_EVENT, getLabeledValue(target.checked), e);
  }
  async function onClickRoot(e) {
    if (isLimitExceeded.value) return;
    if (!hasOwnLabel.value && !isDisabled.value && isLabeledByFormItem.value) {
      if (!e.composedPath().some((item) => item.tagName === "LABEL")) {
        model.value = getLabeledValue([
          false,
          props.falseValue,
          props.falseLabel
        ].includes(model.value));
        await Vue.nextTick();
        emitChangeEvent(model.value, e);
      }
    }
  }
  const validateEvent = Vue.computed(() => (checkboxGroup == null ? void 0 : checkboxGroup.validateEvent) || props.validateEvent);
  Vue.watch(() => props.modelValue, () => {
    if (validateEvent.value) formItem == null ? void 0 : formItem.validate("change").catch((err) => error.debugWarn(err));
  });
  return {
    handleChange,
    onClickRoot
  };
};
exports.useCheckboxEvent = useCheckboxEvent;
