import "../../chunks/chunk.QU3DSPNU.js";
import { css } from "lit";
import { GlobalStylesRegistry } from "../../utils/global-styles.js";
const locks = /* @__PURE__ */ new Set();
function registerGlobalScrollLockStyles() {
  GlobalStylesRegistry.instance.injectGlobalStyles(
    "dialog-scroll-lock",
    css`
      .gds-scroll-lock body {
        padding-right: var(--gds-scroll-lock-size) !important;
        overflow: hidden !important;
      }
    `
  );
}
function lockBodyScrolling(lockingEl) {
  locks.add(lockingEl);
  if (!document.documentElement.classList.contains("gds-scroll-lock")) {
    const scrollbarWidth = getScrollbarWidth() + getExistingBodyPadding();
    let scrollbarGutterProperty = getComputedStyle(
      document.documentElement
    ).scrollbarGutter;
    if (!scrollbarGutterProperty || scrollbarGutterProperty === "auto") {
      scrollbarGutterProperty = "stable";
    }
    if (scrollbarWidth < 2) {
      scrollbarGutterProperty = "";
    }
    document.documentElement.style.setProperty(
      "--gds-scroll-lock-gutter",
      scrollbarGutterProperty
    );
    document.documentElement.classList.add("gds-scroll-lock");
    document.documentElement.style.setProperty(
      "--gds-scroll-lock-size",
      `${scrollbarWidth}px`
    );
  }
}
function unlockBodyScrolling(lockingEl) {
  locks.delete(lockingEl);
  if (locks.size === 0) {
    document.documentElement.classList.remove("gds-scroll-lock");
    document.documentElement.style.removeProperty("--gds-scroll-lock-size");
  }
}
function getScrollbarWidth() {
  const documentWidth = document.documentElement.clientWidth;
  return Math.abs(window.innerWidth - documentWidth);
}
function getExistingBodyPadding() {
  const padding = Number(
    getComputedStyle(document.body).paddingRight.replace(/px/, "")
  );
  if (isNaN(padding) || !padding) {
    return 0;
  }
  return padding;
}
export {
  lockBodyScrolling,
  registerGlobalScrollLockStyles,
  unlockBodyScrolling
};
