import { r as m, c as a, h as o, H as d } from "./index.61732cef.js";
import "react";
import "react-dom";
const l = () => ".sc-swirl-theme-provider-h{display:unset}", i = "swirl-preferred-theme", c = class {
  constructor(e) {
    m(this, e), this.appThemeUpdated = a(this, "appThemeUpdated", 7), this.themeChange = a(this, "themeChange", 7), this.darkThemeMediaQuery = window.matchMedia("(prefers-color-scheme: dark)"), this.setDesignTokens = [], this.osThemeChangeHandler = (t) => {
      this.osTheme = t.matches ? "dark" : "light", this.updateAppTheme();
    };
  }
  componentWillLoad() {
    this.resolveConfig(), this.determineOSTheme(), this.updateAppTheme();
  }
  disconnectedCallback() {
    this.darkThemeMediaQuery.removeEventListener("change", this.osThemeChangeHandler);
  }
  watchConfig() {
    this.resolveConfig(), this.determineOSTheme(), this.updateAppTheme();
  }
  async getActiveOSTheme() {
    return this.appOSTheme;
  }
  async getPreferredOSTheme() {
    if (!!Boolean(this.resolvedConfig.storage))
      return this.resolvedConfig.storage.getItem(i);
  }
  async setPreferredOSTheme(e) {
    !Boolean(this.resolvedConfig.storage) || !this.resolvedConfig.enabledThemes.includes(e) || (this.resolvedConfig.storage.setItem(i, e), this.updateAppTheme());
  }
  async resetPreferredOSTheme() {
    !Boolean(this.resolvedConfig.storage) || (this.resolvedConfig.storage.removeItem(i), this.updateAppTheme());
  }
  resolveConfig() {
    var e, t, s;
    if (this.resolvedConfig = {
      ...this.config || {},
      enabledThemes: ((e = this.config) == null ? void 0 : e.enabledThemes) || ["light", "dark"],
      rootElement: ((t = this.config) == null ? void 0 : t.rootElement) || document.documentElement,
      storage: ((s = this.config) == null ? void 0 : s.storage) || (window == null ? void 0 : window.localStorage)
    }, !this.resolvedConfig.enabledThemes.includes("light"))
      throw new Error("[Swirl Theme Provider] Light theme must be enabled.");
  }
  determineOSTheme() {
    if (!Boolean(window.matchMedia)) {
      this.osTheme = "light";
      return;
    }
    this.osTheme = this.darkThemeMediaQuery.matches ? "dark" : "light", this.darkThemeMediaQuery.removeEventListener("change", this.osThemeChangeHandler), this.darkThemeMediaQuery.addEventListener("change", this.osThemeChangeHandler);
  }
  async updateAppTheme() {
    const e = await this.getPreferredOSTheme() || this.osTheme;
    this.resolvedConfig.enabledThemes.includes(e) ? this.appOSTheme = e : this.appOSTheme = "light", this.appOSTheme === "dark" ? (document.documentElement.classList.remove("theme-light"), document.documentElement.classList.add("theme-dark")) : (document.documentElement.classList.remove("theme-dark"), document.documentElement.classList.add("theme-light")), this.updateTenantVariables(), this.updateTenantAssets();
    const t = {
      activeTheme: await this.getActiveOSTheme(),
      preferredTheme: await this.getPreferredOSTheme()
    };
    (!Boolean(this.recentOSThemeChangeEventData) || t.activeTheme !== this.recentOSThemeChangeEventData.activeTheme || t.preferredTheme !== this.recentOSThemeChangeEventData.preferredTheme) && (this.recentOSThemeChangeEventData = t, this.themeChange.emit(this.recentOSThemeChangeEventData)), this.appThemeUpdated.emit();
  }
  updateTenantAssets() {
    var t, s, h;
    const e = (s = (t = this.resolvedConfig) == null ? void 0 : t.themes) == null ? void 0 : s[this.appOSTheme];
    !Boolean(e) || (h = document.head.querySelector('link[rel="icon"]')) == null || h.setAttribute("href", e.favicon.link);
  }
  updateTenantVariables() {
    var s, h;
    const e = (h = (s = this.resolvedConfig) == null ? void 0 : s.themes) == null ? void 0 : h[this.appOSTheme];
    if (!Boolean(e)) {
      this.resetTenantVariables();
      return;
    }
    const t = this.resolvedConfig.rootElement;
    this.setDesignTokens = [], e.design_tokens.forEach((r) => {
      const n = `--s-${r.id}`;
      t.style.setProperty(n, `rgba(${r.color.r}, ${r.color.g}, ${r.color.b}, ${r.color.a})`), this.setDesignTokens.push(n);
    });
  }
  resetTenantVariables() {
    const e = this.resolvedConfig.rootElement;
    this.setDesignTokens.forEach((t) => {
      e.style.removeProperty(t);
    }), this.setDesignTokens = [];
  }
  render() {
    return o(d, { key: "da311ad9f17c98b82431154d9f2673b260fa14e7" }, o("slot", { key: "407258de8bac5e17a4a0c4b52f05482455fbc8cf" }));
  }
  static get watchers() {
    return {
      config: [{
        watchConfig: 0
      }]
    };
  }
};
c.style = l();
export {
  c as swirl_theme_provider
};
