import { inject, computed, onMounted, onActivated, nextTick } from "vue";
import { u as useAriaInvalid } from "./useAriaInvalid-BTUbGj3Y.mjs";
import { u as useId } from "./useId-9_HUwM96.mjs";
import { c as useFocus } from "./index-nMtnPEP0.mjs";
import { j as formGroupKey } from "./keys-D5Pj1dmf.mjs";
import { u as useDebounceFn } from "./debounce-CAItkwCO.mjs";
import { u as useStateClass } from "./useStateClass-BGbSLWFN.mjs";
import { u as useToNumber } from "./index-DX8Yoi0C.mjs";
const normalizeInput = (v, modelModifiers) => {
  if (v === null) return;
  let update = v;
  if (modelModifiers.number && typeof update === "string" && update !== "") {
    const parsed = Number.parseFloat(update);
    update = Number.isNaN(parsed) ? update : parsed;
  }
  return update;
};
const useFormInput = (props, input, modelValue, modelModifiers) => {
  const computedId = useId(() => props.id, "input");
  const debounceNumber = useToNumber(() => props.debounce ?? 0, { nanToZero: true });
  const debounceMaxWaitNumber = useToNumber(() => props.debounceMaxWait ?? Number.NaN);
  const formGroupData = inject(formGroupKey, null)?.(computedId);
  const computedState = computed(
    () => props.state !== void 0 ? props.state : formGroupData?.state.value ?? null
  );
  const isDisabled = computed(() => props.disabled || (formGroupData?.disabled.value ?? false));
  const computedAriaInvalid = useAriaInvalid(() => props.ariaInvalid, computedState);
  const stateClass = useStateClass(computedState);
  const internalUpdateModelValue = useDebounceFn(
    (value) => {
      modelValue.value = value;
    },
    () => modelModifiers.lazy === true ? 0 : debounceNumber.value,
    { maxWait: () => modelModifiers.lazy === true ? Number.NaN : debounceMaxWaitNumber.value }
  );
  const updateModelValue = (value, force = false, immediate = false) => {
    if (modelModifiers.lazy === true && force === false) return;
    if (immediate) {
      modelValue.value = value;
    } else {
      internalUpdateModelValue(value);
    }
  };
  const { focused } = useFocus(input, {
    initialValue: props.autofocus
  });
  const _formatValue = (value, evt, force = false) => {
    if (props.formatter !== void 0 && (!props.lazyFormatter || force)) {
      return props.formatter(value, evt);
    }
    return value;
  };
  onMounted(() => {
    if (input.value) {
      input.value.value = modelValue.value?.toString() ?? "";
    }
  });
  onActivated(() => {
    nextTick(() => {
      if (props.autofocus) {
        focused.value = true;
      }
    });
  });
  const syncDisplayedValue = (nextValue) => {
    if (input.value && input.value.value !== nextValue) {
      input.value.value = nextValue;
    }
  };
  const onInput = (evt) => {
    const { value } = evt.target;
    const formattedValue = _formatValue(value, evt);
    if (evt.defaultPrevented) {
      evt.preventDefault();
      return;
    }
    const nextModel = formattedValue;
    updateModelValue(nextModel);
    if (formattedValue !== value) {
      syncDisplayedValue(formattedValue);
    }
  };
  const onChange = (evt) => {
    const { value } = evt.target;
    const formattedValue = _formatValue(value, evt);
    if (evt.defaultPrevented) {
      evt.preventDefault();
      return;
    }
    const nextModel = formattedValue;
    if (modelValue.value !== nextModel) {
      updateModelValue(formattedValue, true);
    }
  };
  const onBlur = (evt) => {
    if (!modelModifiers.lazy && !props.lazyFormatter && !modelModifiers.trim && debounceNumber.value <= 0)
      return;
    const { value } = evt.target;
    const formattedValue = _formatValue(value, evt, true);
    const nextModel = modelModifiers.trim ? formattedValue.trim() : formattedValue;
    internalUpdateModelValue.cancel();
    if (modelValue.value !== nextModel) {
      updateModelValue(nextModel, true, true);
    }
    if (nextModel !== value) {
      syncDisplayedValue(nextModel);
    }
  };
  const focus = () => {
    if (!isDisabled.value) {
      focused.value = true;
    }
  };
  const blur = () => {
    if (!isDisabled.value) {
      focused.value = false;
    }
  };
  return {
    input,
    computedId,
    computedAriaInvalid,
    onInput,
    onChange,
    onBlur,
    focus,
    blur,
    stateClass,
    isDisabled
  };
};
export {
  normalizeInput as n,
  useFormInput as u
};
//# sourceMappingURL=useFormInput-CEdB4KGR.mjs.map
