import { useRef as c, useCallback as E, useEffect as m } from "react";
import { TABBABLE_ELEMENTS as a } from "../utilities/elements.es.js";
const k = (o, r = !1, t = null) => {
  const f = c(null), u = E(
    (e) => {
      var i;
      const s = [...((i = f.current) == null ? void 0 : i.querySelectorAll(a)) ?? []], l = s[0], n = s[s.length - 1], d = e.key === "Tab";
      s.length === 0 || !d || (t === e.target && (l.focus(), e.preventDefault()), !e.shiftKey && e.target === n && (l.focus(), e.preventDefault()), e.shiftKey && e.target === l && (n.focus(), e.preventDefault()));
    },
    [t]
  );
  return m(() => {
    if (r)
      return;
    const e = f.current, s = document.activeElement ?? document.body;
    return o && (t == null || t.addEventListener("keydown", u), e == null || e.addEventListener("keydown", u)), () => {
      t == null || t.removeEventListener("keydown", u), e == null || e.removeEventListener("keydown", u), s.focus();
    };
  }, [u, r, o, t]), { elementRef: f };
};
export {
  k as useFocusTrap
};
//# sourceMappingURL=useFocusTrap.es.js.map
