"use strict";
const vue = require("vue");
const useAriaInvalid = require("./useAriaInvalid-Caxf1WW4.js");
const useId = require("./useId-Dl8Hi5_U.js");
const index = require("./index-C_bsiScj.js");
const keys = require("./keys-D70E9HED.js");
const debounce = require("./debounce-EDygIUUU.js");
const useStateClass = require("./useStateClass-Bk6-veG2.js");
const index$1 = require("./index-JJEBhaXq.js");
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.useId(() => props.id, "input");
  const debounceNumber = index$1.useToNumber(() => props.debounce ?? 0, { nanToZero: true });
  const debounceMaxWaitNumber = index$1.useToNumber(() => props.debounceMaxWait ?? Number.NaN);
  const formGroupData = vue.inject(keys.formGroupKey, null)?.(computedId);
  const computedState = vue.computed(
    () => props.state !== void 0 ? props.state : formGroupData?.state.value ?? null
  );
  const isDisabled = vue.computed(() => props.disabled || (formGroupData?.disabled.value ?? false));
  const computedAriaInvalid = useAriaInvalid.useAriaInvalid(() => props.ariaInvalid, computedState);
  const stateClass = useStateClass.useStateClass(computedState);
  const internalUpdateModelValue = debounce.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 } = index.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;
  };
  vue.onMounted(() => {
    if (input.value) {
      input.value.value = modelValue.value?.toString() ?? "";
    }
  });
  vue.onActivated(() => {
    vue.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
  };
};
exports.normalizeInput = normalizeInput;
exports.useFormInput = useFormInput;
//# sourceMappingURL=useFormInput-CSIV1X5N.js.map
