import { ref, onMounted, onBeforeUnmount } from "vue";
import { FOCUSOUT_PREVENTED as c$1, FOCUSOUT_PREVENTED_OPTS as E } from "./tokens.mjs";
const a = ref(), u = ref(0), d = ref(0);
let r = 0;
const T = (e) => {
  const n = [], o = document.createTreeWalker(e, NodeFilter.SHOW_ELEMENT, {
    acceptNode: (t) => {
      const s = t.tagName === "INPUT" && t.type === "hidden";
      return t.disabled || t.hidden || s ? NodeFilter.FILTER_SKIP : t.tabIndex >= 0 || t === document.activeElement ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
    }
  });
  for (; o.nextNode(); )
    n.push(o.currentNode);
  return n;
}, f = (e, n) => {
  for (const o of e)
    if (!N(o, n))
      return o;
}, N = (e, n) => {
  if (getComputedStyle(e).visibility === "hidden")
    return true;
  for (; e; ) {
    if (n && e === n)
      return false;
    if (getComputedStyle(e).display === "none")
      return true;
    e = e.parentElement;
  }
  return false;
}, R = (e) => {
  const n = T(e), o = f(n, e), t = f(n.reverse(), e);
  return [o, t];
}, y = (e) => e instanceof HTMLInputElement && "select" in e, S = (e, n) => {
  if (e && e.focus) {
    const o = document.activeElement;
    e.focus({ preventScroll: true }), d.value = window.performance.now(), e !== o && y(e) && n && e.select();
  }
};
function m(e, n) {
  const o = [...e], t = e.indexOf(n);
  return t !== -1 && o.splice(t, 1), o;
}
const b = () => {
  let e = [];
  return {
    push: (t) => {
      const s = e[0];
      s && t !== s && s.pause(), e = m(e, t), e.unshift(t);
    },
    remove: (t) => {
      var s, l;
      e = m(e, t), (l = (s = e[0]) == null ? void 0 : s.resume) == null || l.call(s);
    }
  };
}, _ = (e, n = false) => {
  const o = document.activeElement;
  for (const t of e)
    if (S(t, n), document.activeElement !== o)
      return;
}, k = b(), C = () => u.value > d.value, c = () => {
  a.value = "pointer", u.value = window.performance.now();
}, v = () => {
  a.value = "keyboard", u.value = window.performance.now();
}, I = () => (onMounted(() => {
  r === 0 && (document.addEventListener("mousedown", c), document.addEventListener("touchstart", c), document.addEventListener("keydown", v)), r++;
}), onBeforeUnmount(() => {
  r--, r <= 0 && (document.removeEventListener("mousedown", c), document.removeEventListener("touchstart", c), document.removeEventListener("keydown", v));
}), {
  focusReason: a,
  lastUserFocusTimestamp: u,
  lastAutomatedFocusTimestamp: d
}), O = (e) => new CustomEvent(c$1, {
  ...E,
  detail: e
});
export {
  O as createFocusOutPreventedEvent,
  _ as focusFirstDescendant,
  k as focusableStack,
  R as getEdges,
  f as getVisibleElement,
  C as isFocusCausedByUserEvent,
  N as isHidden,
  T as obtainAllFocusableElements,
  S as tryFocus,
  I as useFocusReason
};
