import { disposables as v } from "../utils/disposables.js";
import { getOwnerDocument as g } from "../utils/owner.js";
import { useIsTopLayer as w } from "./use-is-top-layer.js";
import { useIsoMorphicEffect as A } from "./use-iso-morphic-effect.js";
let d = /* @__PURE__ */ new Map(), l = /* @__PURE__ */ new Map();
function h(e) {
  var t;
  let r = (t = l.get(e)) != null ? t : 0;
  return l.set(e, r + 1), r !== 0 ? () => c(e) : (d.set(e, { "aria-hidden": e.getAttribute("aria-hidden"), inert: e.inert }), e.setAttribute("aria-hidden", "true"), e.inert = !0, () => c(e));
}
function c(e) {
  var t;
  let r = (t = l.get(e)) != null ? t : 1;
  if (r === 1 ? l.delete(e) : l.set(e, r - 1), r !== 1) return;
  let n = d.get(e);
  n && (n["aria-hidden"] === null ? e.removeAttribute("aria-hidden") : e.setAttribute("aria-hidden", n["aria-hidden"]), e.inert = n.inert, d.delete(e));
}
function x(e, { allowed: t, disallowed: r } = {}) {
  let n = w(e, "inert-others");
  A(() => {
    var u, s;
    if (!n) return;
    let a = v();
    for (let i of (u = r == null ? void 0 : r()) != null ? u : []) i && a.add(h(i));
    let f = (s = t == null ? void 0 : t()) != null ? s : [];
    for (let i of f) {
      if (!i) continue;
      let m = g(i);
      if (!m) continue;
      let o = i.parentElement;
      for (; o && o !== m.body; ) {
        for (let p of o.children) f.some((b) => p.contains(b)) || a.add(h(p));
        o = o.parentElement;
      }
    }
    return a.dispose;
  }, [n, t, r]);
}
export {
  x as useInertOthers
};
