import {
  __privateAdd,
  __privateGet,
  __privateMethod,
  __privateSet
} from "../../chunks/chunk.QU3DSPNU.js";
var _useLegacyStylesheets, _initialized, _initialStyleSheets, _initialStyleSheetsCopy, _styleSheets, _legacyStyleSheets, _initialLegacyStyleSheets, _initialLegacyStyleSheetsCopy, _DynamicStylesController_instances, addStyles_fn, commit_fn, init_fn;
class DynamicStylesController {
  constructor(host) {
    __privateAdd(this, _DynamicStylesController_instances);
    __privateAdd(this, _useLegacyStylesheets, !supportsConstructedStylesheets());
    __privateAdd(this, _initialized, false);
    __privateAdd(this, _initialStyleSheets, []);
    __privateAdd(this, _initialStyleSheetsCopy, []);
    __privateAdd(this, _styleSheets, /* @__PURE__ */ new Map());
    __privateAdd(this, _legacyStyleSheets, /* @__PURE__ */ new Map());
    __privateAdd(this, _initialLegacyStyleSheets, []);
    __privateAdd(this, _initialLegacyStyleSheetsCopy, []);
    this.host = host;
    this.host.addController(this);
  }
  hostConnected() {
    __privateMethod(this, _DynamicStylesController_instances, init_fn).call(this);
  }
  /**
   * Checks if the controller has a particular key.
   *
   * @param key - The style key to check.
   */
  has(key) {
    return __privateGet(this, _useLegacyStylesheets) ? __privateGet(this, _legacyStyleSheets).has(key) : __privateGet(this, _styleSheets).has(key);
  }
  /**
   * Injects CSS into the host element's shadow root. The key is used to
   * identify the styles and can be used to update the styles later. If
   * you need to clear style for a particular key, you can call `inject`
   * with an empty string as the `styles` parameter.
   *
   * @param key - A unique key to identify the styles.
   * @param cssResult - The CSSResult to inject.
   */
  inject(key, cssResult) {
    __privateMethod(this, _DynamicStylesController_instances, addStyles_fn).call(this, key, cssResult);
    __privateMethod(this, _DynamicStylesController_instances, commit_fn).call(this);
  }
  /**
   * Clears the styles for a particular key.
   *
   * @param key - The key of the styles to clear.
   */
  clear(key) {
    if (__privateGet(this, _useLegacyStylesheets)) {
      this.host.updateComplete.then(() => {
        const styleEl = __privateGet(this, _legacyStyleSheets).get(key);
        styleEl?.remove();
        __privateGet(this, _legacyStyleSheets).delete(key);
      });
    } else {
      __privateGet(this, _styleSheets).delete(key);
    }
    __privateMethod(this, _DynamicStylesController_instances, commit_fn).call(this);
  }
  /**
   * Clears all styles of the component, including any initial styles.
   */
  clearAll() {
    if (__privateGet(this, _useLegacyStylesheets)) {
      this.host.updateComplete.then(() => {
        __privateGet(this, _legacyStyleSheets).forEach((styleEl) => styleEl.remove());
        __privateGet(this, _initialLegacyStyleSheets).forEach((styleEl) => styleEl.remove());
        __privateGet(this, _legacyStyleSheets).clear();
        __privateSet(this, _initialLegacyStyleSheets, []);
      });
    } else {
      if (this.host.shadowRoot) {
        this.host.shadowRoot.adoptedStyleSheets = [];
        __privateGet(this, _styleSheets).clear();
        __privateSet(this, _initialStyleSheets, []);
      }
    }
  }
  /**
   * Clears the initial styles of the component, but keeps any injected styles.
   */
  clearInitial() {
    if (__privateGet(this, _useLegacyStylesheets)) {
      this.host.updateComplete.then(() => {
        __privateGet(this, _initialLegacyStyleSheets).forEach((styleEl) => styleEl.remove());
        __privateSet(this, _initialLegacyStyleSheets, []);
      });
    } else {
      __privateSet(this, _initialStyleSheets, []);
    }
    __privateMethod(this, _DynamicStylesController_instances, commit_fn).call(this);
  }
  /**
   * Restores the initial styles of the component, and keeps any injected styles.
   */
  restoreInitial() {
    if (__privateGet(this, _useLegacyStylesheets)) {
      __privateGet(this, _initialLegacyStyleSheetsCopy).forEach((styleEl) => {
        __privateGet(this, _initialLegacyStyleSheets).push(
          styleEl.cloneNode(true)
        );
      });
    } else {
      __privateSet(this, _initialStyleSheets, [...__privateGet(this, _initialStyleSheetsCopy)]);
    }
    __privateMethod(this, _DynamicStylesController_instances, commit_fn).call(this);
  }
}
_useLegacyStylesheets = new WeakMap();
_initialized = new WeakMap();
_initialStyleSheets = new WeakMap();
_initialStyleSheetsCopy = new WeakMap();
_styleSheets = new WeakMap();
_legacyStyleSheets = new WeakMap();
_initialLegacyStyleSheets = new WeakMap();
_initialLegacyStyleSheetsCopy = new WeakMap();
_DynamicStylesController_instances = new WeakSet();
addStyles_fn = function(key, cssResult) {
  if (__privateGet(this, _useLegacyStylesheets)) {
    let styleEl = __privateGet(this, _legacyStyleSheets).get(key);
    if (!styleEl) {
      styleEl = document.createElement("style");
      __privateGet(this, _legacyStyleSheets).set(key, styleEl);
    }
    styleEl.textContent = cssResult.cssText;
  } else {
    if (!this.host.shadowRoot || !cssResult.styleSheet) return;
    __privateGet(this, _styleSheets).set(key, cssResult.styleSheet);
  }
};
commit_fn = function() {
  if (__privateGet(this, _useLegacyStylesheets)) {
    this.host.updateComplete.then(() => {
      if (!this.host.shadowRoot) return;
      this.host.shadowRoot.querySelectorAll("style").forEach((style) => {
        style.remove();
      });
      __privateGet(this, _initialLegacyStyleSheets).forEach((styleEl) => {
        this.host.shadowRoot?.appendChild(styleEl);
      });
      __privateGet(this, _legacyStyleSheets).forEach((styleEl) => {
        this.host.shadowRoot?.appendChild(styleEl);
      });
    });
  } else {
    if (!this.host.shadowRoot) return;
    this.host.shadowRoot.adoptedStyleSheets = [
      ...__privateGet(this, _initialStyleSheets),
      ...Array.from(__privateGet(this, _styleSheets).values())
    ];
  }
};
init_fn = function() {
  if (__privateGet(this, _initialized)) return;
  if (__privateGet(this, _useLegacyStylesheets)) {
    this.host.updateComplete.then(() => {
      if (!this.host.shadowRoot) return;
      this.host.shadowRoot.querySelectorAll("style").forEach((style) => {
        __privateGet(this, _initialLegacyStyleSheets).push(style);
        __privateGet(this, _initialLegacyStyleSheetsCopy).push(
          style.cloneNode(true)
        );
      });
    });
  } else {
    if (this.host.shadowRoot && __privateGet(this, _initialStyleSheets).length === 0) {
      __privateSet(this, _initialStyleSheets, [
        ...this.host.shadowRoot.adoptedStyleSheets || []
      ]);
      __privateSet(this, _initialStyleSheetsCopy, [...__privateGet(this, _initialStyleSheets)]);
    }
  }
  __privateSet(this, _initialized, true);
};
function supportsConstructedStylesheets() {
  try {
    new CSSStyleSheet();
    return true;
  } catch {
    return false;
  }
}
export {
  DynamicStylesController,
  supportsConstructedStylesheets
};
