import { onUnmounted as m, unref as y, nextTick as p } from "vue";
const f = 'button:not([tabindex="-1"]), [href], input, select, textarea, li, a, [tabindex]:not([tabindex="-1"])';
function x() {
  let n = [], u = null, t = null;
  async function a(e) {
    e = y(e), e && (await p(), i(e), t && t.focus(), document.addEventListener("keydown", r), u = new MutationObserver(() => i(e)), e.$el ? u.observe(e.$el, { childList: !0, subtree: !0 }) : u.observe(e, { childList: !0, subtree: !0 }));
  }
  function i(e) {
    if (e === null)
      return;
    let l;
    e.$el ? l = e == null ? void 0 : e.$el.querySelectorAll(f) : e.querySelectorAll(f), n = Array.from(l || []), t = n[0] || null, n[n.length - 1];
  }
  const r = (e) => {
    var l;
    if (e.key === "Tab") {
      const b = e.shiftKey, c = document.activeElement;
      if (!c) {
        e.preventDefault(), (l = n[0]) == null || l.focus();
        return;
      }
      const o = n[0], s = n[n.length - 1];
      !b && c === s ? (e.preventDefault(), o == null || o.focus()) : b && c === o && (e.preventDefault(), s == null || s.focus());
    }
  }, d = () => {
    document.removeEventListener("keydown", r), u == null || u.disconnect();
  };
  return m(() => {
    d();
  }), {
    initFocusTrap: a,
    clearFocusTrap: d
  };
}
export {
  x as useFocusTrap
};
