"use strict";
var _a, _b;
const common_vendor = require("../../../../common/vendor.js");
const uni_modules_uviewPro_libs_config_config = require("../config/config.js");
const uni_modules_uviewPro_libs_config_themeTokens = require("../config/theme-tokens.js");
const uni_modules_uviewPro_libs_config_color = require("../config/color.js");
const uni_modules_uviewPro_libs_util_systemTheme = require("./system-theme.js");
const uni_modules_uviewPro_locale_index = require("../../locale/index.js");
const THEME_STORAGE_KEY = "uview-pro-theme";
const DARK_MODE_STORAGE_KEY = "uview-pro-dark-mode";
const LOCALE_STORAGE_KEY = "uview-pro-locale";
const DEFAULT_LIGHT_TOKENS = ((_a = uni_modules_uviewPro_libs_config_themeTokens.defaultThemes[0]) == null ? void 0 : _a.color) || {};
const DEFAULT_DARK_TOKENS = ((_b = uni_modules_uviewPro_libs_config_themeTokens.defaultThemes[0]) == null ? void 0 : _b.darkColor) || {};
const STRUCTURAL_TOKENS = /* @__PURE__ */ new Set([
  "bgColor",
  "bgWhite",
  "bgGrayLight",
  "bgGrayDark",
  "bgBlack",
  "borderColor",
  "lightColor",
  "mainColor",
  "contentColor",
  "tipsColor",
  "whiteColor",
  "blackColor",
  "dividerColor",
  "maskColor",
  "shadowColor"
]);
class ConfigProvider {
  constructor() {
    this.themesRef = common_vendor.ref([]);
    this.currentThemeRef = common_vendor.ref(null);
    this.darkModeRef = common_vendor.ref(uni_modules_uviewPro_libs_config_config.config.defaultDarkMode);
    this.cssVarsRef = common_vendor.ref({});
    this.localesRef = common_vendor.ref([]);
    this.currentLocaleRef = common_vendor.ref(null);
    this.baseColorTokens = DEFAULT_LIGHT_TOKENS;
    this.baseDarkColorTokens = DEFAULT_DARK_TOKENS;
    this.debug = false;
    this.systemDarkModeMediaQuery = null;
    this.lastAppliedCssKeys = [];
    this.interval = 0;
    this.initSystemDarkModeListener();
  }
  /**
   * 初始化系统暗黑模式监听器
   * 支持 H5、App、小程序等平台
   */
  initSystemDarkModeListener() {
    try {
      if (typeof window !== "undefined" && window.matchMedia) {
        this.systemDarkModeMediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
        const listener = () => {
          if (this.darkModeRef.value === "auto") {
            this.applyTheme(this.currentThemeRef.value);
          }
        };
        if (this.systemDarkModeMediaQuery.addEventListener) {
          this.systemDarkModeMediaQuery.addEventListener("change", listener);
        } else if (this.systemDarkModeMediaQuery.addListener) {
          this.systemDarkModeMediaQuery.addListener(listener);
        }
      }
    } catch (e) {
      if (this.debug)
        common_vendor.index.__f__("warn", "at uni_modules/uview-pro/libs/util/config-provider.ts:98", "[ConfigProvider] H5 system dark mode listener failed", e);
    }
    try {
      if (typeof common_vendor.index !== "undefined" && typeof common_vendor.index.onThemeChange === "function") {
        common_vendor.index.onThemeChange((res) => {
          common_vendor.index.__f__("log", "at uni_modules/uview-pro/libs/util/config-provider.ts:105", "[ConfigProvider] system theme changed", res);
          if (this.darkModeRef.value === "auto") {
            this.applyTheme(this.currentThemeRef.value);
          }
        });
      }
    } catch (e) {
      if (this.debug)
        common_vendor.index.__f__("warn", "at uni_modules/uview-pro/libs/util/config-provider.ts:113", "[ConfigProvider] uni-app system dark mode listener failed", e);
    }
    this.initAppEvent();
  }
  /**
   * App 平台事件监听
   * 经测试 uni.onThemeChange 在 App 平台目前没生效，暂时只能通过定时检查
   */
  initAppEvent() {
  }
  /**
   * 检测当前是否应该使用暗黑模式
   */
  isSystemDarkMode() {
    try {
      if (this.systemDarkModeMediaQuery) {
        return this.systemDarkModeMediaQuery.matches;
      }
    } catch (e) {
      if (this.debug)
        common_vendor.index.__f__("warn", "at uni_modules/uview-pro/libs/util/config-provider.ts:147", "[ConfigProvider] matchMedia check failed", e);
    }
    try {
      return uni_modules_uviewPro_libs_util_systemTheme.getSystemDarkMode() === "dark";
    } catch (e) {
      if (this.debug)
        common_vendor.index.__f__("warn", "at uni_modules/uview-pro/libs/util/config-provider.ts:152", "[ConfigProvider] native system theme check failed", e);
      return false;
    }
  }
  /**
   * 初始化主题系统
   * @param themes 可用主题数组
   * @param defaultTheme 可选默认主题名
   */
  initTheme(themes, defaultConfig, isForce) {
    const normalizedThemes = this.normalizeThemes(themes);
    if (!normalizedThemes.length) {
      common_vendor.index.__f__("warn", "at uni_modules/uview-pro/libs/util/config-provider.ts:165", "[ConfigProvider] init called with empty themes");
      return;
    }
    if (defaultConfig) {
      if (typeof defaultConfig === "string") {
        uni_modules_uviewPro_libs_config_config.config.defaultTheme = defaultConfig || uni_modules_uviewPro_libs_config_config.config.defaultTheme;
      } else if (typeof defaultConfig === "object") {
        const { defaultTheme, defaultDarkMode } = defaultConfig;
        uni_modules_uviewPro_libs_config_config.config.defaultTheme = defaultTheme || uni_modules_uviewPro_libs_config_config.config.defaultTheme;
        uni_modules_uviewPro_libs_config_config.config.defaultDarkMode = defaultDarkMode || uni_modules_uviewPro_libs_config_config.config.defaultDarkMode;
      }
    }
    this.themesRef.value = normalizedThemes.slice();
    const saved = this.readStorage(THEME_STORAGE_KEY);
    let initialName = saved || uni_modules_uviewPro_libs_config_config.config.defaultTheme || this.themesRef.value[0].name;
    if (isForce && uni_modules_uviewPro_libs_config_config.config.defaultTheme)
      initialName = uni_modules_uviewPro_libs_config_config.config.defaultTheme;
    let found = this.themesRef.value.find((t) => t.name === initialName);
    if (!found)
      found = this.themesRef.value.find((t) => t.name === uni_modules_uviewPro_libs_config_config.config.defaultTheme);
    if (!found)
      found = this.themesRef.value[0];
    this.currentThemeRef.value = found;
    this.initDarkMode(uni_modules_uviewPro_libs_config_config.config.defaultDarkMode, isForce);
    this.applyTheme(found);
    if (this.debug)
      common_vendor.index.__f__("log", "at uni_modules/uview-pro/libs/util/config-provider.ts:201", "[ConfigProvider] initialized, theme=", found.name, "darkMode=", this.darkModeRef.value);
    return this;
  }
  /**
   * 初始化暗黑模式设置
   * @param darkMode
   */
  initDarkMode(darkMode, isForce) {
    const savedDarkMode = this.readStorage(DARK_MODE_STORAGE_KEY);
    let darkModeValue = savedDarkMode || darkMode || uni_modules_uviewPro_libs_config_config.config.defaultDarkMode;
    if (isForce && darkMode)
      darkModeValue = darkMode;
    this.darkModeRef.value = darkModeValue;
  }
  /**
   * 初始化国际化数据
   * @param locales 可选的 locale 列表（对象数组，包含 name 字段）
   * @param defaultLocaleName 可选默认 locale 名称
   */
  initLocales(locales, defaultLocaleName, isForce) {
    const normalized = this.normalizeLocales(locales);
    if (!normalized.length) {
      if (this.debug)
        common_vendor.index.__f__("warn", "at uni_modules/uview-pro/libs/util/config-provider.ts:226", "[ConfigProvider] initLocales called with empty locales");
      return;
    }
    this.localesRef.value = normalized.slice();
    const saved = this.readStorage(LOCALE_STORAGE_KEY);
    let initialName = saved || defaultLocaleName || uni_modules_uviewPro_libs_config_config.config.defaultLocale;
    if (isForce && defaultLocaleName)
      initialName = defaultLocaleName;
    let found = this.localesRef.value.find((l) => l.name === initialName);
    if (!found)
      found = this.localesRef.value.find((l) => l.name === uni_modules_uviewPro_libs_config_config.config.defaultLocale);
    if (!found)
      found = this.localesRef.value[0];
    this.currentLocaleRef.value = found;
    if (this.debug)
      common_vendor.index.__f__("log", "at uni_modules/uview-pro/libs/util/config-provider.ts:244", "[ConfigProvider] locales initialized, locale=", found == null ? void 0 : found.name);
    return this;
  }
  /**
   * 归一化 locale 配置，保证始终至少有一个默认 locale
   */
  normalizeLocales(locales) {
    let builtinList = [];
    try {
      builtinList = Object.values(uni_modules_uviewPro_locale_index.localePack || {}).filter((v) => v && typeof v === "object");
    } catch (e) {
      if (this.debug)
        common_vendor.index.__f__("warn", "at uni_modules/uview-pro/libs/util/config-provider.ts:258", "[ConfigProvider] normalizeLocales read builtin failed", e);
    }
    if (!Array.isArray(locales) || !locales.length) {
      return builtinList.slice();
    }
    const map = /* @__PURE__ */ new Map();
    builtinList.forEach((item) => {
      if (item && item.name) {
        map.set(item.name, { ...item || {} });
      }
    });
    locales.forEach((loc) => {
      if (!loc || !loc.name)
        return;
      const existing = map.get(loc.name);
      if (!existing) {
        map.set(loc.name, { ...loc || {} });
        return;
      }
      const merged = { ...existing };
      Object.keys(loc).forEach((k) => {
        const v = loc[k];
        if (v != null && typeof v === "object" && !Array.isArray(v) && typeof merged[k] === "object") {
          merged[k] = { ...merged[k] || {}, ...v || {} };
        } else {
          merged[k] = v;
        }
      });
      map.set(loc.name, merged);
    });
    return Array.from(map.values());
  }
  /**
   * 获取所有可用 locale
   */
  getLocales() {
    return this.localesRef.value.slice();
  }
  /**
   * 获取当前 locale 对象
   */
  getCurrentLocale() {
    return this.currentLocaleRef.value;
  }
  /**
   * 切换 locale 并持久化
   */
  setLocale(localeName) {
    if (!this.localesRef.value || this.localesRef.value.length === 0) {
      common_vendor.index.__f__("warn", "at uni_modules/uview-pro/libs/util/config-provider.ts:318", "[ConfigProvider] setLocale called but locales list empty");
      return;
    }
    const locale = this.localesRef.value.find((l) => l.name === localeName);
    if (!locale) {
      common_vendor.index.__f__("warn", "at uni_modules/uview-pro/libs/util/config-provider.ts:323", "[ConfigProvider] locale not found:", localeName);
      return;
    }
    this.currentLocaleRef.value = locale;
    this.writeStorage(LOCALE_STORAGE_KEY, localeName);
    if (this.debug)
      common_vendor.index.__f__("log", "at uni_modules/uview-pro/libs/util/config-provider.ts:328", "[ConfigProvider] setLocale ->", localeName);
  }
  /**
   * 翻译函数
   * 支持 key 路径，例如 'calendar.placeholder'
   * replacements 支持数组或对象替换占位符 {0} 或 {name}
   */
  t(key, replacements, localeName) {
    try {
      if (!Array.isArray(this.localesRef.value) || this.localesRef.value.length === 0) {
        this.initLocales();
      }
    } catch (e) {
      if (this.debug)
        common_vendor.index.__f__("warn", "at uni_modules/uview-pro/libs/util/config-provider.ts:343", "[ConfigProvider] lazy initLocales failed", e);
    }
    const localeObj = localeName && this.localesRef.value.find((l) => l.name === localeName) || this.currentLocaleRef.value;
    if (!localeObj)
      return key;
    const parts = key.split(".");
    let cur = localeObj;
    for (let i = 0; i < parts.length; i++) {
      if (cur == null)
        break;
      cur = cur[parts[i]];
    }
    let text = typeof cur === "string" ? cur : key;
    if (replacements != null) {
      if (Array.isArray(replacements)) {
        replacements.forEach((val, idx) => {
          text = text.split(`{${idx}}`).join(String(val));
        });
      } else if (typeof replacements === "object") {
        Object.keys(replacements).forEach((k) => {
          text = text.split(`{${k}}`).join(String(replacements[k]));
        });
      }
    }
    return text;
  }
  /**
   * 获取所有可用主题
   */
  getThemes() {
    return this.themesRef.value.slice();
  }
  /**
   * 获取当前主题
   */
  getCurrentTheme() {
    return this.currentThemeRef.value;
  }
  /**
   * 切换主题并持久化
   */
  setTheme(themeName) {
    if (!this.themesRef.value || this.themesRef.value.length === 0) {
      common_vendor.index.__f__("warn", "at uni_modules/uview-pro/libs/util/config-provider.ts:389", "[ConfigProvider] setTheme called but themes list empty");
      return;
    }
    const theme = this.themesRef.value.find((t) => t.name === themeName);
    if (!theme) {
      common_vendor.index.__f__("warn", "at uni_modules/uview-pro/libs/util/config-provider.ts:395", "[ConfigProvider] theme not found:", themeName);
      return;
    }
    this.currentThemeRef.value = theme;
    this.applyTheme(theme);
    this.writeStorage(THEME_STORAGE_KEY, themeName);
    if (this.debug)
      common_vendor.index.__f__("log", "at uni_modules/uview-pro/libs/util/config-provider.ts:407", "[ConfigProvider] setTheme ->", themeName);
  }
  /**
   * 运行时更新当前主题颜色并应用（不持久化）
   * @param colors 主题颜色键值，支持部分更新
   */
  setThemeColor(colors) {
    if (!colors || Object.keys(colors).length === 0)
      return;
    if (!this.currentThemeRef.value) {
      common_vendor.index.__f__("warn", "at uni_modules/uview-pro/libs/util/config-provider.ts:417", "[ConfigProvider] setThemeColor called but no current theme");
      return;
    }
    const mode = this.getActiveMode();
    if (mode === "dark") {
      const existing = this.currentThemeRef.value.darkColor || {};
      this.currentThemeRef.value.darkColor = {
        ...existing,
        ...colors
      };
    } else {
      const existing = this.currentThemeRef.value.color || {};
      this.currentThemeRef.value.color = {
        ...existing,
        ...colors
      };
    }
    this.applyTheme(this.currentThemeRef.value);
    if (this.debug)
      common_vendor.index.__f__("log", "at uni_modules/uview-pro/libs/util/config-provider.ts:440", "[ConfigProvider] setThemeColor ->", colors);
  }
  /**
   * 获取当前暗黑模式设置
   */
  getDarkMode() {
    return this.darkModeRef.value;
  }
  /**
   * 设置暗黑模式
   * @param mode 'auto' (跟随系统) | 'light' (强制亮色) | 'dark' (强制暗黑)
   */
  setDarkMode(mode) {
    this.darkModeRef.value = mode;
    this.writeStorage(DARK_MODE_STORAGE_KEY, mode);
    this.applyTheme(this.currentThemeRef.value);
    if (this.debug)
      common_vendor.index.__f__("log", "at uni_modules/uview-pro/libs/util/config-provider.ts:462", "[ConfigProvider] setDarkMode ->", mode);
  }
  /**
   * 检查当前是否处于暗黑模式
   */
  isInDarkMode() {
    const mode = this.darkModeRef.value;
    if (mode === "dark")
      return true;
    if (mode === "light")
      return false;
    return this.isSystemDarkMode();
  }
  /**
   * 归一化主题配置，保证始终至少有一个默认主题
   */
  normalizeThemes(themes) {
    if (Array.isArray(themes) && themes.length) {
      return this.mergeThemes(uni_modules_uviewPro_libs_config_themeTokens.defaultThemes, themes);
    }
    return uni_modules_uviewPro_libs_config_themeTokens.defaultThemes.slice();
  }
  mergeThemes(...lists) {
    const map = /* @__PURE__ */ new Map();
    lists.filter((list) => Array.isArray(list) && list.length > 0).forEach((list) => {
      list.forEach((theme) => {
        const normalized = this.ensureDarkVariant({
          ...theme,
          color: this.applyColorFallbacks(theme.color),
          darkColor: theme.darkColor ? { ...theme.darkColor } : void 0,
          css: theme.css ? { ...theme.css } : void 0,
          darkCss: theme.darkCss ? { ...theme.darkCss } : void 0
        });
        map.set(normalized.name, normalized);
      });
    });
    return Array.from(map.values());
  }
  ensureDarkVariant(theme) {
    const finalDark = this.buildDarkPalette(theme);
    return {
      ...theme,
      darkColor: this.applyDarkFallbacks(finalDark)
    };
  }
  buildDarkPalette(theme) {
    const provided = theme.darkColor || {};
    const generated = this.generateDarkFromLight(theme.color || {}, provided);
    return {
      ...generated,
      ...provided
    };
  }
  /**
   * 应用亮色主题
   */
  applyColorFallbacks(color) {
    return {
      ...this.baseColorTokens || {},
      ...color || {}
    };
  }
  /**
   * 应用暗黑主题
   */
  applyDarkFallbacks(color) {
    return {
      ...this.baseDarkColorTokens || {},
      ...color || {}
    };
  }
  filterNonStructuralTokens(palette) {
    const result = {};
    Object.entries(palette || {}).forEach(([key, value]) => {
      if (!this.isStructuralToken(key)) {
        result[key] = value;
      }
    });
    return result;
  }
  generateDarkFromLight(palette, provided) {
    const result = {};
    const nonStructural = this.filterNonStructuralTokens(palette);
    Object.entries(nonStructural).forEach(([key, value]) => {
      var _a2;
      if (typeof value !== "string")
        return;
      if (provided && Object.prototype.hasOwnProperty.call(provided, key)) {
        return;
      }
      const fallback = (_a2 = this.baseDarkColorTokens) == null ? void 0 : _a2[key];
      result[key] = this.createDarkVariantFromLight(value, fallback);
    });
    return result;
  }
  createDarkVariantFromLight(color, fallback) {
    const normalized = this.normalizeHex(color);
    const fallbackHex = fallback ? this.normalizeHex(fallback) : null;
    if (normalized && fallbackHex) {
      return this.mixHex(normalized, fallbackHex, 0.6);
    }
    if (fallbackHex)
      return fallbackHex;
    return normalized || color;
  }
  normalizeHex(color) {
    if (!color)
      return null;
    const hex = color.trim();
    if (/^#([0-9a-fA-F]{6})$/.test(hex))
      return hex.toLowerCase();
    return null;
  }
  mixHex(fromHex, toHex, ratio) {
    const from = this.hexToRgb(fromHex);
    const to = this.hexToRgb(toHex);
    if (!from || !to)
      return toHex;
    const clamp = (val) => Math.min(255, Math.max(0, Math.round(val)));
    const r = clamp(from.r * (1 - ratio) + to.r * ratio);
    const g = clamp(from.g * (1 - ratio) + to.g * ratio);
    const b = clamp(from.b * (1 - ratio) + to.b * ratio);
    return this.rgbToHex(r, g, b);
  }
  hexToRgb(hex) {
    const match = /^#([0-9a-fA-F]{6})$/.exec(hex);
    if (!match)
      return null;
    return {
      r: parseInt(match[1].slice(0, 2), 16),
      g: parseInt(match[1].slice(2, 4), 16),
      b: parseInt(match[1].slice(4, 6), 16)
    };
  }
  rgbToHex(r, g, b) {
    const toHex = (val) => val.toString(16).padStart(2, "0");
    return `#${toHex(r)}${toHex(g)}${toHex(b)}`;
  }
  isStructuralToken(token) {
    return STRUCTURAL_TOKENS.has(token);
  }
  /**
   * 运行时同步主题颜色（$u.color）
   * 更新响应式 color 对象，确保所有使用 $u.color 的地方都能响应式更新
   */
  syncRuntimeTheme(palette) {
    var _a2, _b2;
    try {
      const defaultPalette = this.getActiveMode() === "dark" ? this.baseDarkColorTokens : this.baseColorTokens;
      const mergedPalette = {
        ...defaultPalette,
        ...palette
      };
      Object.keys(mergedPalette).forEach((key) => {
        const value = mergedPalette[key];
        if (value != null) {
          uni_modules_uviewPro_libs_config_color.color[key] = value;
        }
      });
      if (typeof common_vendor.index !== "undefined" && ((_b2 = (_a2 = common_vendor.index) == null ? void 0 : _a2.$u) == null ? void 0 : _b2.color)) {
        common_vendor.index.$u.color = uni_modules_uviewPro_libs_config_color.color;
      }
    } catch (e) {
      if (this.debug)
        common_vendor.index.__f__("warn", "at uni_modules/uview-pro/libs/util/config-provider.ts:640", "[ConfigProvider] sync runtime theme failed", e);
    }
  }
  /**
   * 获取当前激活的模式
   */
  getActiveMode() {
    return this.isInDarkMode() ? "dark" : "light";
  }
  /**
   * 获取当前主题的配色方案
   */
  getPaletteForMode(theme, mode) {
    if (mode === "dark") {
      return theme.darkColor && Object.keys(theme.darkColor).length ? theme.darkColor : theme.color || {};
    }
    return theme.color || {};
  }
  /**
   * 获取当前主题的CSS变量覆盖
   */
  getCssOverrides(theme, mode) {
    if (mode === "dark") {
      return (theme.darkCss && Object.keys(theme.darkCss).length ? theme.darkCss : theme.css) || {};
    }
    return theme.css || {};
  }
  /**
   * 读取Storage key
   */
  readStorage(key) {
    try {
      if (typeof common_vendor.index === "undefined" || typeof common_vendor.index.getStorageSync !== "function")
        return null;
      const value = common_vendor.index.getStorageSync(key);
      return value ?? null;
    } catch (e) {
      if (this.debug)
        common_vendor.index.__f__("warn", "at uni_modules/uview-pro/libs/util/config-provider.ts:680", "[ConfigProvider] failed to read storage", e);
      return null;
    }
  }
  /**
   * 写入Storage key value
   */
  writeStorage(key, value) {
    try {
      if (typeof common_vendor.index === "undefined" || typeof common_vendor.index.setStorageSync !== "function")
        return;
      common_vendor.index.setStorageSync(key, value);
    } catch (e) {
      if (this.debug)
        common_vendor.index.__f__("warn", "at uni_modules/uview-pro/libs/util/config-provider.ts:693", "[ConfigProvider] failed to write storage", e);
    }
  }
  /**
   * 更新文档主题模式 H5
   */
  updateDocumentMode(mode) {
    if (typeof document === "undefined" || !document.documentElement)
      return;
    const root = document.documentElement;
    root.dataset.uThemeMode = mode;
    root.classList.remove("u-theme-light", "u-theme-dark");
    root.classList.add(`u-theme-${mode}`);
  }
  /**
   * 转换为 CSS 变量名称
   */
  toCssVarName(key, prefix = "--u") {
    const types = uni_modules_uviewPro_libs_config_config.config.type;
    if (types.some((type) => key.startsWith(type))) {
      prefix += "-type";
    }
    const kebab = key.replace(/([a-z0-9])([A-Z])/g, "$1-$2").replace(/[\s_]+/g, "-").toLowerCase();
    return `${prefix}-${kebab}`;
  }
  /**
   * 添加 RGB 值
   */
  attachRgbVar(target, varName, value) {
    if (typeof value !== "string")
      return;
    const hex = value.startsWith("#") ? value.slice(1) : "";
    if (!/^[0-9a-fA-F]{6}$/.test(hex))
      return;
    const r = parseInt(hex.slice(0, 2), 16);
    const g = parseInt(hex.slice(2, 4), 16);
    const b = parseInt(hex.slice(4, 6), 16);
    target[`${varName}-rgb`] = `${r}, ${g}, ${b}`;
  }
  /**
   * 构建 CSS 变量映射表
   * 生成格式：
   */
  buildCssVarMap(theme, mode) {
    const map = {
      "--u-theme-mode": mode
    };
    const palette = this.getPaletteForMode(theme, mode);
    const cssOverrides = this.getCssOverrides(theme, mode);
    const applyEntry = (key, value) => {
      if (value == null)
        return;
      const strValue = String(value);
      if (key.startsWith("--")) {
        map[key] = strValue;
        this.attachRgbVar(map, key, strValue);
        return;
      }
      const cssVarName = this.toCssVarName(key);
      map[cssVarName] = strValue;
      this.attachRgbVar(map, cssVarName, strValue);
    };
    Object.entries(palette || {}).forEach(([key, value]) => applyEntry(key, value));
    Object.entries(cssOverrides || {}).forEach(([key, value]) => applyEntry(key, value));
    return map;
  }
  /**
   * 刷新 CSS 变量 H5
   */
  flushCssVars(vars) {
    if (typeof document === "undefined" || !document.documentElement)
      return;
    const root = document.documentElement;
    this.lastAppliedCssKeys.forEach((key) => {
      root.style.removeProperty(key);
    });
    Object.entries(vars).forEach(([key, value]) => {
      root.style.setProperty(key, value);
    });
    this.lastAppliedCssKeys = Object.keys(vars);
  }
  /**
   * 将主题应用到运行时：
   * - 1) 调用 uni.$u.setColor(theme.color) 如果存在
   * - 2) 在 H5 环境中，将 css map 注入到 document.documentElement 的 CSS 变量中
   * - 3) 支持暗黑模式：根据 darkColor 或 color 应用相应的颜色
   */
  applyTheme(theme) {
    if (!theme)
      return;
    const mode = this.getActiveMode();
    const palette = this.getPaletteForMode(theme, mode);
    this.syncRuntimeTheme(palette);
    const cssVarMap = this.buildCssVarMap(theme, mode);
    this.cssVarsRef.value = cssVarMap;
    this.flushCssVars(cssVarMap);
    this.updateDocumentMode(mode);
  }
}
const configProvider = new ConfigProvider();
exports.configProvider = configProvider;
//# sourceMappingURL=../../../../../.sourcemap/mp-weixin/uni_modules/uview-pro/libs/util/config-provider.js.map
