import { ref, getCurrentInstance, watch, provide, computed, inject, onBeforeUnmount, defineComponent } from 'vue';
import '../../../utils/index.mjs';
import '../../../hooks/index.mjs';
import { devWarning } from '../../../utils/log.mjs';
import { createContext } from '../../../hooks/create-context/index.mjs';

const ContextKey = Symbol("ContextProps");
const InternalContextKey = Symbol("InternalContextProps");
function useProvideFormItemContext(props, useValidation = computed(() => true)) {
  const formItemFields = ref(/* @__PURE__ */ new Map());
  const addFormItemField = (key, type) => {
    formItemFields.value.set(key, type);
    formItemFields.value = new Map(formItemFields.value);
  };
  const removeFormItemField = (key) => {
    formItemFields.value.delete(key);
    formItemFields.value = new Map(formItemFields.value);
  };
  const instance = getCurrentInstance();
  watch([useValidation, formItemFields], () => {
    if (process.env.NODE_ENV !== "production") {
      if (useValidation.value && formItemFields.value.size > 1) {
        devWarning(
          false,
          "Form.Item",
          `FormItem can only collect one field item, you haved set ${[
            ...formItemFields.value.values()
          ].map((v) => `\`${v.name}\``).join(", ")} ${formItemFields.value.size} field items.
        You can set not need to be collected fields into \`a-form-item-rest\``
        );
        let cur = instance;
        while (cur.parent) {
          console.warn("at", cur.type);
          cur = cur.parent;
        }
      }
    }
  });
  provide(ContextKey, props);
  provide(InternalContextKey, {
    addFormItemField,
    removeFormItemField
  });
}
const defaultContext = {
  id: computed(() => void 0),
  onFieldBlur: () => {
  },
  onFieldChange: () => {
  },
  clearValidate: () => {
  }
};
const defaultInternalContext = {
  addFormItemField: () => {
  },
  removeFormItemField: () => {
  }
};
function useInjectFormItemContext() {
  const internalContext = inject(InternalContextKey, defaultInternalContext);
  const formItemFieldKey = Symbol("FormItemFieldKey");
  const instance = getCurrentInstance();
  internalContext.addFormItemField(formItemFieldKey, instance.type);
  onBeforeUnmount(() => {
    internalContext.removeFormItemField(formItemFieldKey);
  });
  provide(InternalContextKey, defaultInternalContext);
  provide(ContextKey, defaultContext);
  return inject(ContextKey, defaultContext);
}
var AFormItemRest = defineComponent({
  compatConfig: { MODE: 3 },
  name: "AFormItemRest",
  setup(_, { slots }) {
    provide(InternalContextKey, defaultInternalContext);
    provide(ContextKey, defaultContext);
    return () => {
      var _a;
      return (_a = slots.default) == null ? void 0 : _a.call(slots);
    };
  }
});
const FormItemInputContext = createContext({});
const NoFormStatus = defineComponent({
  name: "NoFormStatus",
  setup(_, { slots }) {
    FormItemInputContext.useProvide({});
    return () => {
      var _a;
      return (_a = slots.default) == null ? void 0 : _a.call(slots);
    };
  }
});

export { FormItemInputContext, NoFormStatus, AFormItemRest as default, useInjectFormItemContext, useProvideFormItemContext };
//# sourceMappingURL=FormItemContext.mjs.map
