import { useRef as u } from "react";
import { useEvent as o } from "./use-event.js";
import { useEventListener as r } from "./use-event-listener.js";
function a(e) {
  let n = u({ value: "", selectionStart: null, selectionEnd: null });
  return r(e, "blur", (l) => {
    let t = l.target;
    t instanceof HTMLInputElement && (n.current = { value: t.value, selectionStart: t.selectionStart, selectionEnd: t.selectionEnd });
  }), o(() => {
    if (document.activeElement !== e && e instanceof HTMLInputElement && e.isConnected) {
      if (e.focus({ preventScroll: !0 }), e.value !== n.current.value) e.setSelectionRange(e.value.length, e.value.length);
      else {
        let { selectionStart: l, selectionEnd: t } = n.current;
        l !== null && t !== null && e.setSelectionRange(l, t);
      }
      n.current = { value: "", selectionStart: null, selectionEnd: null };
    }
  });
}
export {
  a as useRefocusableInput
};
