import {
  __privateAdd,
  __privateGet,
  __privateMethod
} from "../chunks/chunk.QU3DSPNU.js";
var _useLegacyStylesheets, _styleSheets, _legacyStyleSheets, _initialStyleSheets, _GlobalStylesRegistry_instances, injectStylesLegacy_fn, injectStyles_fn, clearStyles_fn;
import { VER_SUFFIX } from "../scoping.js";
import { supportsConstructedStylesheets } from "../utils/controllers/dynamic-styles-controller.js";
import { isServer } from "./helpers/platform.js";
function getDocumentAdoptedStylesheet() {
  if (isServer) return [];
  return [...document.adoptedStyleSheets];
}
const _GlobalStylesRegistry = class _GlobalStylesRegistry {
  constructor() {
    __privateAdd(this, _GlobalStylesRegistry_instances);
    __privateAdd(this, _useLegacyStylesheets, !supportsConstructedStylesheets());
    __privateAdd(this, _styleSheets, /* @__PURE__ */ new Map());
    __privateAdd(this, _legacyStyleSheets, /* @__PURE__ */ new Map());
    __privateAdd(this, _initialStyleSheets, getDocumentAdoptedStylesheet());
  }
  /**
   * Get the global singleton instance of the GlobalStylesRegistry.
   */
  static get instance() {
    if (!globalThis.__gdsGlobalStylesRegistryScoped?.[VER_SUFFIX])
      globalThis.__gdsGlobalStylesRegistryScoped = {
        ...globalThis.__gdsGlobalStylesRegistryScoped,
        [VER_SUFFIX]: new _GlobalStylesRegistry()
      };
    return globalThis.__gdsGlobalStylesRegistryScoped[VER_SUFFIX];
  }
  /**
   * Injects global styles into the document. This function is intended to be used for global styles
   * that cannot be scoped to a specific component.
   *
   * @param key unique identifier for this stylesheet.
   * @param styles CSSResult produced by the lit css template literal tag
   */
  injectGlobalStyles(key, styles) {
    if (__privateGet(this, _useLegacyStylesheets)) {
      const cssText = Array.isArray(styles) ? styles.map((style) => style.toString()).join("") : styles.toString();
      __privateMethod(this, _GlobalStylesRegistry_instances, injectStylesLegacy_fn).call(this, key, cssText);
    } else {
      styles.styleSheet && __privateMethod(this, _GlobalStylesRegistry_instances, injectStyles_fn).call(this, key, styles.styleSheet);
    }
  }
  /**
   * Clears global styles for the given key.
   *
   * @param key unique identifier for the stylesheet to be removed.
   */
  clearGlobalStyles(key) {
    if (__privateGet(this, _useLegacyStylesheets)) {
      const styleEl = __privateGet(this, _legacyStyleSheets).get(key);
      if (styleEl && styleEl.parentNode) {
        styleEl.parentNode.removeChild(styleEl);
        __privateGet(this, _legacyStyleSheets).delete(key);
      }
    } else {
      __privateMethod(this, _GlobalStylesRegistry_instances, clearStyles_fn).call(this, key);
    }
  }
};
_useLegacyStylesheets = new WeakMap();
_styleSheets = new WeakMap();
_legacyStyleSheets = new WeakMap();
_initialStyleSheets = new WeakMap();
_GlobalStylesRegistry_instances = new WeakSet();
injectStylesLegacy_fn = function(key, cssText) {
  if (isServer) return;
  let styleEl = __privateGet(this, _legacyStyleSheets).get(key);
  if (!styleEl) {
    styleEl = document.createElement("style");
    __privateGet(this, _legacyStyleSheets).set(key, styleEl);
  }
  styleEl.textContent = cssText;
  document.head.appendChild(styleEl);
};
injectStyles_fn = function(key, styleSheet) {
  if (isServer) return;
  __privateGet(this, _styleSheets).set(key, styleSheet);
  document.adoptedStyleSheets = [
    ...__privateGet(this, _initialStyleSheets),
    ...Array.from(__privateGet(this, _styleSheets).values())
  ];
};
clearStyles_fn = function(key) {
  if (__privateGet(this, _styleSheets).has(key)) {
    __privateGet(this, _styleSheets).delete(key);
    document.adoptedStyleSheets = [
      ...__privateGet(this, _initialStyleSheets),
      ...Array.from(__privateGet(this, _styleSheets).values())
    ];
  }
};
let GlobalStylesRegistry = _GlobalStylesRegistry;
export {
  GlobalStylesRegistry
};
