"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const index = require("../../../../hooks/use-id/index.js");
const constants = require("../constants.js");
const Vue = require("vue");
const useFormItem = () => {
  return {
    form: Vue.inject(constants.formContextKey, void 0),
    formItem: Vue.inject(constants.formItemContextKey, void 0)
  };
};
const useFormItemInputId = (props, { formItemContext, disableIdGeneration, disableIdManagement }) => {
  if (!disableIdGeneration) disableIdGeneration = Vue.ref(false);
  if (!disableIdManagement) disableIdManagement = Vue.ref(false);
  const instance = Vue.getCurrentInstance();
  const inLabel = () => {
    let parent = instance == null ? void 0 : instance.parent;
    while (parent) {
      if (parent.type.name === "ElFormItem") return false;
      if (parent.type.name === "ElLabelWrap") return true;
      parent = parent.parent;
    }
    return false;
  };
  const inputId = Vue.ref();
  let idUnwatch = void 0;
  const isLabeledByFormItem = Vue.computed(() => {
    var _a;
    return !!(!(props.label || props.ariaLabel) && formItemContext && formItemContext.inputIds && ((_a = formItemContext.inputIds) == null ? void 0 : _a.length) <= 1);
  });
  Vue.onMounted(() => {
    idUnwatch = Vue.watch([Vue.toRef(props, "id"), disableIdGeneration], ([id, disableIdGeneration2]) => {
      const newId = id ?? (!disableIdGeneration2 ? index.useId().value : void 0);
      if (newId !== inputId.value) {
        if ((formItemContext == null ? void 0 : formItemContext.removeInputId) && !inLabel()) {
          inputId.value && formItemContext.removeInputId(inputId.value);
          if (!(disableIdManagement == null ? void 0 : disableIdManagement.value) && !disableIdGeneration2 && newId) formItemContext.addInputId(newId);
        }
        inputId.value = newId;
      }
    }, { immediate: true });
  });
  Vue.onUnmounted(() => {
    idUnwatch && idUnwatch();
    if (formItemContext == null ? void 0 : formItemContext.removeInputId) inputId.value && formItemContext.removeInputId(inputId.value);
  });
  return {
    isLabeledByFormItem,
    inputId
  };
};
exports.useFormItem = useFormItem;
exports.useFormItemInputId = useFormItemInputId;
