import { useRef as f, useEffect as i } from "react";
import { getFocusableElements as a } from "../utils/index.js";
function p(r) {
  const t = f(null), c = f(null), u = (e) => {
    var s, o;
    const n = document.activeElement === c.current, l = document.activeElement === t.current;
    e.key === "Tab" && (n && !e.shiftKey && (e.preventDefault(), (s = t.current) == null || s.focus()), l && e.shiftKey && (e.preventDefault(), (o = c.current) == null || o.focus()));
  };
  i(() => {
    const e = a(r.current);
    t.current = e[0], c.current = e[e.length - 1], t.current && t.current.focus();
  }, [r.current]), i(() => {
    var e;
    return (e = r.current) == null || e.addEventListener("keydown", u), () => {
      var n;
      (n = r.current) == null || n.removeEventListener("keydown", u);
    };
  }, [u]);
}
export {
  p as useTrappedFocus
};
