import { ignoreFocusEvent as k } from "./utils.js";
import { getOwnerWindow as a, getOwnerDocument as c } from "../../../../../@react-aria_utils@3.33.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@react-aria/utils/dist/domHelpers.js";
import { openLink as E } from "../../../../../@react-aria_utils@3.33.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@react-aria/utils/dist/openLink.js";
import { isMac as M } from "../../../../../@react-aria_utils@3.33.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@react-aria/utils/dist/platform.js";
import { isVirtualClick as h } from "../../../../../@react-aria_utils@3.33.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@react-aria/utils/dist/isVirtualEvent.js";
import { useEffect as x } from "react";
let f = null;
const l = /* @__PURE__ */ new Set();
let d = /* @__PURE__ */ new Map(), i = !1, b = !1;
const H = {
  Tab: !0,
  Escape: !0
};
function p(t, o) {
  for (let e of l) e(t, o);
}
function O(t) {
  return !(t.metaKey || !M() && t.altKey || t.ctrlKey || t.key === "Control" || t.key === "Shift" || t.key === "Meta");
}
function u(t) {
  i = !0, !E.isOpening && O(t) && (f = "keyboard", p("keyboard", t));
}
function r(t) {
  f = "pointer", "pointerType" in t && t.pointerType, (t.type === "mousedown" || t.type === "pointerdown") && (i = !0, p("pointer", t));
}
function m(t) {
  !E.isOpening && h(t) && (i = !0, f = "virtual");
}
function L(t) {
  t.target === window || t.target === document || k || !t.isTrusted || (!i && !b && (f = "virtual", p("virtual", t)), i = !1, b = !1);
}
function y() {
  i = !1, b = !0;
}
function $(t) {
  if (typeof window > "u" || typeof document > "u" || d.get(a(t))) return;
  const o = a(t), e = c(t);
  let n = o.HTMLElement.prototype.focus;
  o.HTMLElement.prototype.focus = function() {
    i = !0, n.apply(this, arguments);
  }, e.addEventListener("keydown", u, !0), e.addEventListener("keyup", u, !0), e.addEventListener("click", m, !0), o.addEventListener("focus", L, !0), o.addEventListener("blur", y, !1), typeof PointerEvent < "u" ? (e.addEventListener("pointerdown", r, !0), e.addEventListener("pointermove", r, !0), e.addEventListener("pointerup", r, !0)) : process.env.NODE_ENV === "test" && (e.addEventListener("mousedown", r, !0), e.addEventListener("mousemove", r, !0), e.addEventListener("mouseup", r, !0)), o.addEventListener("beforeunload", () => {
    w(t);
  }, {
    once: !0
  }), d.set(o, {
    focus: n
  });
}
const w = (t, o) => {
  const e = a(t), n = c(t);
  o && n.removeEventListener("DOMContentLoaded", o), d.has(e) && (e.HTMLElement.prototype.focus = d.get(e).focus, n.removeEventListener("keydown", u, !0), n.removeEventListener("keyup", u, !0), n.removeEventListener("click", m, !0), e.removeEventListener("focus", L, !0), e.removeEventListener("blur", y, !1), typeof PointerEvent < "u" ? (n.removeEventListener("pointerdown", r, !0), n.removeEventListener("pointermove", r, !0), n.removeEventListener("pointerup", r, !0)) : process.env.NODE_ENV === "test" && (n.removeEventListener("mousedown", r, !0), n.removeEventListener("mousemove", r, !0), n.removeEventListener("mouseup", r, !0)), d.delete(e));
};
function F(t) {
  const o = c(t);
  let e;
  return o.readyState !== "loading" ? $(t) : (e = () => {
    $(t);
  }, o.addEventListener("DOMContentLoaded", e)), () => w(t, e);
}
typeof document < "u" && F();
function I() {
  return f !== "pointer";
}
const K = /* @__PURE__ */ new Set([
  "checkbox",
  "radio",
  "range",
  "color",
  "file",
  "image",
  "button",
  "submit",
  "reset"
]);
function C(t, o, e) {
  let n = c(e == null ? void 0 : e.target);
  const s = typeof window < "u" ? a(e == null ? void 0 : e.target).HTMLInputElement : HTMLInputElement, v = typeof window < "u" ? a(e == null ? void 0 : e.target).HTMLTextAreaElement : HTMLTextAreaElement, g = typeof window < "u" ? a(e == null ? void 0 : e.target).HTMLElement : HTMLElement, T = typeof window < "u" ? a(e == null ? void 0 : e.target).KeyboardEvent : KeyboardEvent;
  return t = t || n.activeElement instanceof s && !K.has(n.activeElement.type) || n.activeElement instanceof v || n.activeElement instanceof g && n.activeElement.isContentEditable, !(t && o === "keyboard" && e instanceof T && !H[e.key]);
}
function _(t, o, e) {
  $(), x(() => {
    if ((e == null ? void 0 : e.enabled) === !1) return;
    let n = (s, v) => {
      C(!!(e != null && e.isTextInput), s, v) && t(I());
    };
    return l.add(n), () => {
      l.delete(n);
    };
  }, o);
}
export {
  F as addWindowFocusTracking,
  l as changeHandlers,
  d as hasSetupGlobalListeners,
  I as isFocusVisible,
  _ as useFocusVisibleListener
};
