import { getNodeName as V } from "../../../../../../../@floating-ui_utils@0.2.10/node_modules/@floating-ui/utils/dist/floating-ui.utils.dom.js";
import { getDocument as y } from "./element.js";
const u = {
  inert: /* @__PURE__ */ new WeakMap(),
  "aria-hidden": /* @__PURE__ */ new WeakMap(),
  none: /* @__PURE__ */ new WeakMap()
};
function S(r) {
  return r === "inert" ? u.inert : r === "aria-hidden" ? u["aria-hidden"] : u.none;
}
let h = /* @__PURE__ */ new WeakSet(), d = {}, m = 0;
const E = (r) => r && (r.host || E(r.parentNode)), C = (r, o) => o.map((n) => {
  if (r.contains(n))
    return n;
  const i = E(n);
  return r.contains(i) ? i : null;
}).filter((n) => n != null);
function T(r, o, n, i) {
  const c = "data-base-ui-inert", a = i ? "inert" : n ? "aria-hidden" : null, w = C(o, r), l = /* @__PURE__ */ new Set(), N = new Set(w), M = [];
  d[c] || (d[c] = /* @__PURE__ */ new WeakMap());
  const p = d[c];
  w.forEach(b), A(o), l.clear();
  function b(t) {
    !t || l.has(t) || (l.add(t), t.parentNode && b(t.parentNode));
  }
  function A(t) {
    !t || N.has(t) || [].forEach.call(t.children, (e) => {
      if (V(e) !== "script")
        if (l.has(e))
          A(e);
        else {
          const k = a ? e.getAttribute(a) : null, s = k !== null && k !== "false", f = S(a), W = (f.get(e) || 0) + 1, g = (p.get(e) || 0) + 1;
          f.set(e, W), p.set(e, g), M.push(e), W === 1 && s && h.add(e), g === 1 && e.setAttribute(c, ""), !s && a && e.setAttribute(a, a === "inert" ? "" : "true");
        }
    });
  }
  return m += 1, () => {
    M.forEach((t) => {
      const e = S(a), s = (e.get(t) || 0) - 1, f = (p.get(t) || 0) - 1;
      e.set(t, s), p.set(t, f), s || (!h.has(t) && a && t.removeAttribute(a), h.delete(t)), f || t.removeAttribute(c);
    }), m -= 1, m || (u.inert = /* @__PURE__ */ new WeakMap(), u["aria-hidden"] = /* @__PURE__ */ new WeakMap(), u.none = /* @__PURE__ */ new WeakMap(), h = /* @__PURE__ */ new WeakSet(), d = {});
  };
}
function O(r, o = !1, n = !1) {
  const i = y(r[0]).body;
  return T(r.concat(Array.from(i.querySelectorAll("[aria-live]"))), i, o, n);
}
export {
  O as markOthers
};
