import p from "./index-getScrollbarSize.js";
import a from "./index-ownerDocument2.js";
import h from "./index-ownerWindow2.js";
function m(i) {
  const e = a(i);
  return e.body === i ? h(i).innerWidth > e.documentElement.clientWidth : i.scrollHeight > i.clientHeight;
}
function c(i, e) {
  e ? i.setAttribute("aria-hidden", "true") : i.removeAttribute("aria-hidden");
}
function u(i) {
  return parseInt(h(i).getComputedStyle(i).paddingRight, 10) || 0;
}
function y(i) {
  const o = ["TEMPLATE", "SCRIPT", "STYLE", "LINK", "MAP", "META", "NOSCRIPT", "PICTURE", "COL", "COLGROUP", "PARAM", "SLOT", "SOURCE", "TRACK"].indexOf(i.tagName) !== -1, t = i.tagName === "INPUT" && i.getAttribute("type") === "hidden";
  return o || t;
}
function g(i, e, o, t, s) {
  const n = [e, o, ...t];
  [].forEach.call(i.children, (r) => {
    const d = n.indexOf(r) === -1, l = !y(r);
    d && l && c(r, s);
  });
}
function f(i, e) {
  let o = -1;
  return i.some((t, s) => e(t) ? (o = s, !0) : !1), o;
}
function x(i, e) {
  const o = [], t = i.container;
  if (!e.disableScrollLock) {
    if (m(t)) {
      const r = p(a(t));
      o.push({
        value: t.style.paddingRight,
        property: "padding-right",
        el: t
      }), t.style.paddingRight = `${u(t) + r}px`;
      const d = a(t).querySelectorAll(".mui-fixed");
      [].forEach.call(d, (l) => {
        o.push({
          value: l.style.paddingRight,
          property: "padding-right",
          el: l
        }), l.style.paddingRight = `${u(l) + r}px`;
      });
    }
    let n;
    if (t.parentNode instanceof DocumentFragment)
      n = a(t).body;
    else {
      const r = t.parentElement, d = h(t);
      n = (r == null ? void 0 : r.nodeName) === "HTML" && d.getComputedStyle(r).overflowY === "scroll" ? r : t;
    }
    o.push({
      value: n.style.overflow,
      property: "overflow",
      el: n
    }, {
      value: n.style.overflowX,
      property: "overflow-x",
      el: n
    }, {
      value: n.style.overflowY,
      property: "overflow-y",
      el: n
    }), n.style.overflow = "hidden";
  }
  return () => {
    o.forEach(({
      value: n,
      el: r,
      property: d
    }) => {
      n ? r.style.setProperty(d, n) : r.style.removeProperty(d);
    });
  };
}
function R(i) {
  const e = [];
  return [].forEach.call(i.children, (o) => {
    o.getAttribute("aria-hidden") === "true" && e.push(o);
  }), e;
}
class E {
  constructor() {
    this.containers = void 0, this.modals = void 0, this.modals = [], this.containers = [];
  }
  add(e, o) {
    let t = this.modals.indexOf(e);
    if (t !== -1)
      return t;
    t = this.modals.length, this.modals.push(e), e.modalRef && c(e.modalRef, !1);
    const s = R(o);
    g(o, e.mount, e.modalRef, s, !0);
    const n = f(this.containers, (r) => r.container === o);
    return n !== -1 ? (this.containers[n].modals.push(e), t) : (this.containers.push({
      modals: [e],
      container: o,
      restore: null,
      hiddenSiblings: s
    }), t);
  }
  mount(e, o) {
    const t = f(this.containers, (n) => n.modals.indexOf(e) !== -1), s = this.containers[t];
    s.restore || (s.restore = x(s, o));
  }
  remove(e, o = !0) {
    const t = this.modals.indexOf(e);
    if (t === -1)
      return t;
    const s = f(this.containers, (r) => r.modals.indexOf(e) !== -1), n = this.containers[s];
    if (n.modals.splice(n.modals.indexOf(e), 1), this.modals.splice(t, 1), n.modals.length === 0)
      n.restore && n.restore(), e.modalRef && c(e.modalRef, o), g(n.container, e.mount, e.modalRef, n.hiddenSiblings, !1), this.containers.splice(s, 1);
    else {
      const r = n.modals[n.modals.length - 1];
      r.modalRef && c(r.modalRef, !1);
    }
    return t;
  }
  isTopModal(e) {
    return this.modals.length > 0 && this.modals[this.modals.length - 1] === e;
  }
}
export {
  E as ModalManager,
  c as ariaHidden
};
