var T = Object.defineProperty;
var z = (l, o, t) => o in l ? T(l, o, { enumerable: !0, configurable: !0, writable: !0, value: t }) : l[o] = t;
var n = (l, o, t) => z(l, typeof o != "symbol" ? o + "" : o, t);
import { getWindow as h, isOverflowElement as X } from "../../../../../@floating-ui_utils@0.2.10/node_modules/@floating-ui/utils/dist/floating-ui.utils.dom.js";
import { isIOS as H, isWebKit as F } from "./detectBrowser.js";
import { ownerDocument as a } from "./owner.js";
import { useIsoLayoutEffect as j } from "./useIsoLayoutEffect.js";
import { Timeout as O } from "./useTimeout.js";
import { AnimationFrame as I } from "./useAnimationFrame.js";
import { NOOP as R } from "./empty.js";
let Y = {}, C = {}, G = "";
function V(l) {
  if (typeof document > "u")
    return !1;
  const o = a(l);
  return h(o).innerWidth - o.documentElement.clientWidth > 0;
}
function q(l) {
  if (!(typeof CSS < "u" && CSS.supports && CSS.supports("scrollbar-gutter", "stable")) || typeof document > "u")
    return !1;
  const e = a(l).documentElement, r = {
    scrollbarGutter: e.style.scrollbarGutter,
    overflowY: e.style.overflowY
  };
  e.style.scrollbarGutter = "stable", e.style.overflowY = "scroll";
  const s = e.offsetWidth;
  e.style.overflowY = "hidden";
  const c = e.offsetWidth;
  return Object.assign(e.style, r), s === c;
}
function A(l) {
  const o = a(l), t = o.documentElement, e = o.body, r = X(t) ? t : e, s = r.style.overflow;
  return r.style.overflow = "hidden", () => {
    r.style.overflow = s;
  };
}
function K(l) {
  var b;
  const o = a(l), t = o.documentElement, e = o.body, r = h(t);
  let s = 0, c = 0, u = !1;
  const d = I.create();
  if (F && (((b = r.visualViewport) == null ? void 0 : b.scale) ?? 1) !== 1)
    return () => {
    };
  function m() {
    const f = r.getComputedStyle(t), i = r.getComputedStyle(e), p = (f.scrollbarGutter || "").includes("both-edges") ? "stable both-edges" : "stable";
    s = t.scrollTop, c = t.scrollLeft, Y = {
      scrollbarGutter: t.style.scrollbarGutter,
      overflowY: t.style.overflowY,
      overflowX: t.style.overflowX
    }, G = t.style.scrollBehavior, C = {
      position: e.style.position,
      height: e.style.height,
      width: e.style.width,
      boxSizing: e.style.boxSizing,
      overflowY: e.style.overflowY,
      overflowX: e.style.overflowX,
      scrollBehavior: e.style.scrollBehavior
    };
    const B = t.scrollHeight > t.clientHeight, W = t.scrollWidth > t.clientWidth, x = f.overflowY === "scroll" || i.overflowY === "scroll", E = f.overflowX === "scroll" || i.overflowX === "scroll", w = Math.max(0, r.innerWidth - e.clientWidth), g = Math.max(0, r.innerHeight - e.clientHeight), S = parseFloat(i.marginTop) + parseFloat(i.marginBottom), k = parseFloat(i.marginLeft) + parseFloat(i.marginRight), L = X(t) ? t : e;
    if (u = q(l), u) {
      t.style.scrollbarGutter = p, L.style.overflowY = "hidden", L.style.overflowX = "hidden";
      return;
    }
    Object.assign(t.style, {
      scrollbarGutter: p,
      overflowY: "hidden",
      overflowX: "hidden"
    }), (B || x) && (t.style.overflowY = "scroll"), (W || E) && (t.style.overflowX = "scroll"), Object.assign(e.style, {
      position: "relative",
      height: S || g ? `calc(100dvh - ${S + g}px)` : "100dvh",
      width: k || w ? `calc(100vw - ${k + w}px)` : "100vw",
      boxSizing: "border-box",
      overflow: "hidden",
      scrollBehavior: "unset"
    }), e.scrollTop = s, e.scrollLeft = c, t.setAttribute("data-base-ui-scroll-locked", ""), t.style.scrollBehavior = "unset";
  }
  function v() {
    Object.assign(t.style, Y), Object.assign(e.style, C), u || (t.scrollTop = s, t.scrollLeft = c, t.removeAttribute("data-base-ui-scroll-locked"), t.style.scrollBehavior = G);
  }
  function y() {
    v(), d.request(m);
  }
  return m(), r.addEventListener("resize", y), () => {
    d.cancel(), v(), typeof r.removeEventListener == "function" && r.removeEventListener("resize", y);
  };
}
class M {
  constructor() {
    n(this, "lockCount", 0);
    n(this, "restore", null);
    n(this, "timeoutLock", O.create());
    n(this, "timeoutUnlock", O.create());
    n(this, "release", () => {
      this.lockCount -= 1, this.lockCount === 0 && this.restore && this.timeoutUnlock.start(0, this.unlock);
    });
    n(this, "unlock", () => {
      var o;
      this.lockCount === 0 && this.restore && ((o = this.restore) == null || o.call(this), this.restore = null);
    });
  }
  acquire(o) {
    return this.lockCount += 1, this.lockCount === 1 && this.restore === null && this.timeoutLock.start(0, () => this.lock(o)), this.release;
  }
  lock(o) {
    if (this.lockCount === 0 || this.restore !== null)
      return;
    const e = a(o).documentElement, r = h(e).getComputedStyle(e).overflowY;
    if (r === "hidden" || r === "clip") {
      this.restore = R;
      return;
    }
    const s = H || !V(o);
    this.restore = s ? A(o) : K(o);
  }
}
const U = new M();
function et(l = !0, o = null) {
  j(() => {
    if (l)
      return U.acquire(o);
  }, [l, o]);
}
export {
  et as useScrollLock
};
