import P from "../../../@babel/runtime/helpers/esm/extends.js";
import { forwardRef as _, useState as k, useRef as T, useEffect as h, useCallback as K, createElement as M } from "react";
import { useComposedRefs as R } from "../../react-compose-refs/dist/index.js";
import { Primitive as w } from "../../react-primitive/dist/index.js";
import { useCallbackRef as y } from "../../react-use-callback-ref/dist/index.js";
const E = "focusScope.autoFocusOnMount", b = "focusScope.autoFocusOnUnmount", N = {
  bubbles: !1,
  cancelable: !0
}, X = /* @__PURE__ */ _((e, n) => {
  const { loop: t = !1, trapped: c = !1, onMountAutoFocus: m, onUnmountAutoFocus: L, ...g } = e, [s, U] = k(null), p = y(m), v = y(L), $ = T(null), x = R(
    n,
    (o) => U(o)
  ), i = T({
    paused: !1,
    pause() {
      this.paused = !0;
    },
    resume() {
      this.paused = !1;
    }
  }).current;
  h(() => {
    if (c) {
      let o = function(u) {
        if (i.paused || !s)
          return;
        const r = u.target;
        s.contains(r) ? $.current = r : d($.current, {
          select: !0
        });
      }, f = function(u) {
        if (i.paused || !s)
          return;
        const r = u.relatedTarget;
        r !== null && (s.contains(r) || d($.current, {
          select: !0
        }));
      }, a = function(u) {
        if (document.activeElement === document.body)
          for (const F of u)
            F.removedNodes.length > 0 && d(s);
      };
      document.addEventListener("focusin", o), document.addEventListener("focusout", f);
      const l = new MutationObserver(a);
      return s && l.observe(s, {
        childList: !0,
        subtree: !0
      }), () => {
        document.removeEventListener("focusin", o), document.removeEventListener("focusout", f), l.disconnect();
      };
    }
  }, [
    c,
    s,
    i.paused
  ]), h(() => {
    if (s) {
      O.add(i);
      const o = document.activeElement;
      if (!s.contains(o)) {
        const a = new CustomEvent(E, N);
        s.addEventListener(E, p), s.dispatchEvent(a), a.defaultPrevented || (D(q(I(s)), {
          select: !0
        }), document.activeElement === o && d(s));
      }
      return () => {
        s.removeEventListener(E, p), setTimeout(() => {
          const a = new CustomEvent(b, N);
          s.addEventListener(b, v), s.dispatchEvent(a), a.defaultPrevented || d(o ?? document.body, {
            select: !0
          }), s.removeEventListener(b, v), O.remove(i);
        }, 0);
      };
    }
  }, [
    s,
    p,
    v,
    i
  ]);
  const A = K((o) => {
    if (!t && !c || i.paused)
      return;
    const f = o.key === "Tab" && !o.altKey && !o.ctrlKey && !o.metaKey, a = document.activeElement;
    if (f && a) {
      const l = o.currentTarget, [u, r] = H(l);
      u && r ? !o.shiftKey && a === r ? (o.preventDefault(), t && d(u, {
        select: !0
      })) : o.shiftKey && a === u && (o.preventDefault(), t && d(r, {
        select: !0
      })) : a === l && o.preventDefault();
    }
  }, [
    t,
    c,
    i.paused
  ]);
  return /* @__PURE__ */ M(w.div, P({
    tabIndex: -1
  }, g, {
    ref: x,
    onKeyDown: A
  }));
});
function D(e, { select: n = !1 } = {}) {
  const t = document.activeElement;
  for (const c of e)
    if (d(c, {
      select: n
    }), document.activeElement !== t)
      return;
}
function H(e) {
  const n = I(e), t = S(n, e), c = S(n.reverse(), e);
  return [
    t,
    c
  ];
}
function I(e) {
  const n = [], t = document.createTreeWalker(e, NodeFilter.SHOW_ELEMENT, {
    acceptNode: (c) => {
      const m = c.tagName === "INPUT" && c.type === "hidden";
      return c.disabled || c.hidden || m ? NodeFilter.FILTER_SKIP : c.tabIndex >= 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
    }
  });
  for (; t.nextNode(); )
    n.push(t.currentNode);
  return n;
}
function S(e, n) {
  for (const t of e)
    if (!V(t, {
      upTo: n
    }))
      return t;
}
function V(e, { upTo: n }) {
  if (getComputedStyle(e).visibility === "hidden")
    return !0;
  for (; e; ) {
    if (n !== void 0 && e === n)
      return !1;
    if (getComputedStyle(e).display === "none")
      return !0;
    e = e.parentElement;
  }
  return !1;
}
function W(e) {
  return e instanceof HTMLInputElement && "select" in e;
}
function d(e, { select: n = !1 } = {}) {
  if (e && e.focus) {
    const t = document.activeElement;
    e.focus({
      preventScroll: !0
    }), e !== t && W(e) && n && e.select();
  }
}
const O = j();
function j() {
  let e = [];
  return {
    add(n) {
      const t = e[0];
      n !== t && (t == null || t.pause()), e = C(e, n), e.unshift(n);
    },
    remove(n) {
      var t;
      e = C(e, n), (t = e[0]) === null || t === void 0 || t.resume();
    }
  };
}
function C(e, n) {
  const t = [
    ...e
  ], c = t.indexOf(n);
  return c !== -1 && t.splice(c, 1), t;
}
function q(e) {
  return e.filter(
    (n) => n.tagName !== "A"
  );
}
export {
  X as FocusScope
};
