import { bD as b, aJ as v, bE as _, h as i, C as m, _ as h, bF as y } from "./index-DRgrWhNH.js";
function k(e, l, u = l) {
  var f = /* @__PURE__ */ new WeakSet();
  b(e, "input", async (r) => {
    var a = r ? e.defaultValue : e.value;
    if (a = t(e) ? o(a) : a, u(a), v !== null && f.add(v), await _(), a !== (a = l())) {
      var d = e.selectionStart, s = e.selectionEnd, n = e.value.length;
      if (e.value = a ?? "", s !== null) {
        var c = e.value.length;
        d === s && s === n && c > n ? (e.selectionStart = c, e.selectionEnd = c) : (e.selectionStart = d, e.selectionEnd = Math.min(s, c));
      }
    }
  }), // If we are hydrating and the value has since changed,
  // then use the updated value from the input instead.
  (i && e.defaultValue !== e.value || // If defaultValue is set, then value == defaultValue
  // TODO Svelte 6: remove input.value check and set to empty string?
  m(l) == null && e.value) && (u(t(e) ? o(e.value) : e.value), v !== null && f.add(v)), h(() => {
    var r = l();
    if (e === document.activeElement) {
      var a = (
        /** @type {Batch} */
        y ?? v
      );
      if (f.has(a))
        return;
    }
    t(e) && r === o(e.value) || e.type === "date" && !r && !e.value || r !== e.value && (e.value = r ?? "");
  });
}
function t(e) {
  var l = e.type;
  return l === "number" || l === "range";
}
function o(e) {
  return e === "" ? null : +e;
}
export {
  k as b
};
//# sourceMappingURL=input-BDcUSgHd.js.map
