import { match as F } from "./match.js";
import { getOwnerDocument as N } from "./owner.js";
let m = ["[contentEditable=true]", "[tabindex]", "a[href]", "area[href]", "button:not([disabled])", "iframe", "input:not([disabled])", "select:not([disabled])", "textarea:not([disabled])"].map((e) => `${e}:not([tabindex='-1'])`).join(","), v = ["[data-autofocus]"].map((e) => `${e}:not([tabindex='-1'])`).join(",");
var h = ((e) => (e[e.First = 1] = "First", e[e.Previous = 2] = "Previous", e[e.Next = 4] = "Next", e[e.Last = 8] = "Last", e[e.WrapAround = 16] = "WrapAround", e[e.NoScroll = 32] = "NoScroll", e[e.AutoFocus = 64] = "AutoFocus", e))(h || {}), A = ((e) => (e[e.Error = 0] = "Error", e[e.Overflow = 1] = "Overflow", e[e.Success = 2] = "Success", e[e.Underflow = 3] = "Underflow", e))(A || {}), x = ((e) => (e[e.Previous = -1] = "Previous", e[e.Next = 1] = "Next", e))(x || {});
function y(e = document.body) {
  return e == null ? [] : Array.from(e.querySelectorAll(m)).sort((t, r) => Math.sign((t.tabIndex || Number.MAX_SAFE_INTEGER) - (r.tabIndex || Number.MAX_SAFE_INTEGER)));
}
function S(e = document.body) {
  return e == null ? [] : Array.from(e.querySelectorAll(v)).sort((t, r) => Math.sign((t.tabIndex || Number.MAX_SAFE_INTEGER) - (r.tabIndex || Number.MAX_SAFE_INTEGER)));
}
var p = ((e) => (e[e.Strict = 0] = "Strict", e[e.Loose = 1] = "Loose", e))(p || {});
function P(e, t = 0) {
  var r;
  return e === ((r = N(e)) == null ? void 0 : r.body) ? !1 : F(t, { 0() {
    return e.matches(m);
  }, 1() {
    let o = e;
    for (; o !== null; ) {
      if (o.matches(m)) return !0;
      o = o.parentElement;
    }
    return !1;
  } });
}
var w = ((e) => (e[e.Keyboard = 0] = "Keyboard", e[e.Mouse = 1] = "Mouse", e))(w || {});
typeof window < "u" && typeof document < "u" && (document.addEventListener("keydown", (e) => {
  e.metaKey || e.altKey || e.ctrlKey || (document.documentElement.dataset.headlessuiFocusVisible = "");
}, !0), document.addEventListener("click", (e) => {
  e.detail === 1 ? delete document.documentElement.dataset.headlessuiFocusVisible : e.detail === 0 && (document.documentElement.dataset.headlessuiFocusVisible = "");
}, !0));
function L(e) {
  e == null || e.focus({ preventScroll: !0 });
}
let I = ["textarea", "input"].join(",");
function M(e) {
  var t, r;
  return (r = (t = e == null ? void 0 : e.matches) == null ? void 0 : t.call(e, I)) != null ? r : !1;
}
function O(e, t = (r) => r) {
  return e.slice().sort((r, o) => {
    let s = t(r), a = t(o);
    if (s === null || a === null) return 0;
    let n = s.compareDocumentPosition(a);
    return n & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : n & Node.DOCUMENT_POSITION_PRECEDING ? 1 : 0;
  });
}
function T(e, t, { sorted: r = !0, relativeTo: o = null, skipElements: s = [] } = {}) {
  let a = Array.isArray(e) ? e.length > 0 ? e[0].ownerDocument : document : e.ownerDocument, n = Array.isArray(e) ? r ? O(e) : e : t & 64 ? S(e) : y(e);
  s.length > 0 && n.length > 1 && (n = n.filter((u) => !s.some((c) => c != null && "current" in c ? (c == null ? void 0 : c.current) === u : c === u))), o = o ?? a.activeElement;
  let f = (() => {
    if (t & 5) return 1;
    if (t & 10) return -1;
    throw new Error("Missing Focus.First, Focus.Previous, Focus.Next or Focus.Last");
  })(), E = (() => {
    if (t & 1) return 0;
    if (t & 2) return Math.max(0, n.indexOf(o)) - 1;
    if (t & 4) return Math.max(0, n.indexOf(o)) + 1;
    if (t & 8) return n.length - 1;
    throw new Error("Missing Focus.First, Focus.Previous, Focus.Next or Focus.Last");
  })(), b = t & 32 ? { preventScroll: !0 } : {}, d = 0, i = n.length, l;
  do {
    if (d >= i || d + i <= 0) return 0;
    let u = E + d;
    if (t & 16) u = (u + i) % i;
    else {
      if (u < 0) return 3;
      if (u >= i) return 1;
    }
    l = n[u], l == null || l.focus(b), d += f;
  } while (l !== a.activeElement);
  return t & 6 && M(l) && l.select(), 2;
}
export {
  h as Focus,
  A as FocusResult,
  p as FocusableMode,
  L as focusElement,
  T as focusIn,
  m as focusableSelector,
  S as getAutoFocusableElements,
  y as getFocusableElements,
  P as isFocusableElement,
  O as sortByDomNode
};
