import {
  __privateAdd,
  __privateGet
} from "../../chunks/chunk.QU3DSPNU.js";
var _styles, _elements, _useLegacyStylesheets;
import { unsafeCSS } from "lit";
import * as Button from "../../components/button/button.trans.styles.js";
import * as ContextMenu from "../../components/context-menu/context-menu.trans.styles.js";
import * as Datepicker from "../../components/datepicker/datepicker.trans.styles.js";
import * as Dropdown from "../../components/dropdown/dropdown.trans.styles.js";
import * as FilterChips from "../../components/filter-chips/filter-chips.trans.styles.js";
import * as GroupedList from "../../components/grouped-list/grouped-list.trans.styles.js";
import * as Input from "../../components/input/input.trans.styles.js";
import * as SegmentedControl from "../../components/segmented-control/segmented-control.trans.styles.js";
import * as Theme from "../../components/theme/theme.trans.styles.js";
import { supportsConstructedStylesheets } from "../../utils/controllers/dynamic-styles-controller.js";
import { VER_SUFFIX } from "../helpers/custom-element-scoping.js";
const registerTransitionalStyles = () => {
  Theme.register();
  Dropdown.register();
  ContextMenu.register();
  Datepicker.register();
  GroupedList.register();
  SegmentedControl.register();
  FilterChips.register();
  Button.register();
  Input.register();
};
const _TransitionalStyles = class _TransitionalStyles {
  constructor() {
    __privateAdd(this, _styles, /* @__PURE__ */ new Map());
    __privateAdd(this, _elements, /* @__PURE__ */ new Map());
    __privateAdd(this, _useLegacyStylesheets, !supportsConstructedStylesheets());
  }
  static get instance() {
    if (!globalThis.__gdsTransitionalStyles?.[VER_SUFFIX])
      globalThis.__gdsTransitionalStyles = {
        ...globalThis.__gdsTransitionalStyles,
        [VER_SUFFIX]: new _TransitionalStyles()
      };
    return globalThis.__gdsTransitionalStyles[VER_SUFFIX];
  }
  apply(element, styleKey) {
    if (!element.shadowRoot) return;
    const style = __privateGet(this, _styles).get(styleKey);
    if (!style) return;
    __privateGet(this, _elements).set(styleKey, element);
    this.applyToElement(styleKey, style);
  }
  applyToElement(styleKey, sheet) {
    const element = __privateGet(this, _elements).get(styleKey);
    if (!element || !element.shadowRoot) return;
    const applyTransitional = () => {
      element._dynamicStylesController.clearInitial();
      element._dynamicStylesController.inject("t-styles", unsafeCSS(sheet));
      element._isUsingTransitionalStyles = true;
    };
    const clearTransitional = () => {
      element._isUsingTransitionalStyles = false;
      element._dynamicStylesController.clear("t-styles");
      element._dynamicStylesController.restoreInitial();
    };
    const gdsThemeSelector = "[gds-element=gds-theme]";
    let currentRoot = element.getRootNode();
    let closestGdsTheme = element.closest(gdsThemeSelector);
    while (closestGdsTheme === null && currentRoot !== document) {
      closestGdsTheme = currentRoot.host?.closest(
        gdsThemeSelector
      );
      currentRoot = currentRoot.host?.getRootNode();
    }
    if (closestGdsTheme) {
      const theme = closestGdsTheme;
      const updateStyles = () => {
        if (theme.designVersion === "2023") {
          clearTransitional();
        } else {
          applyTransitional();
        }
      };
      theme.addEventListener("gds-design-version-changed", updateStyles);
      element.addEventListener(
        "gds-element-disconnected",
        () => theme.removeEventListener("gds-design-version-changed", updateStyles)
      );
      if (theme.designVersion === "2023") {
        clearTransitional();
        return;
      }
    }
    applyTransitional();
  }
  register(name, styles) {
    let preparedStyle = styles;
    if (__privateGet(this, _useLegacyStylesheets)) {
      preparedStyle = `@layer reset {
        *:not(style, [gds-element]) {
          all: revert;
        }
      }
      ${styles}`;
    }
    __privateGet(this, _styles).set(name, preparedStyle);
    this.applyToElement(name, preparedStyle);
  }
};
_styles = new WeakMap();
_elements = new WeakMap();
_useLegacyStylesheets = new WeakMap();
let TransitionalStyles = _TransitionalStyles;
export {
  TransitionalStyles,
  registerTransitionalStyles
};
