"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const Vue = require("vue");
const useGlobalZIndex = require("../composables/use-global-z-index.js");
const basic = require("../utils/basic.js");
const format = require("../utils/format.js");
const create = require("../utils/create.js");
const props = require("../utils/props.js");
const [name, bem] = create.createNamespace("config-provider");
const CONFIG_PROVIDER_KEY = Symbol(name);
const configProviderProps = {
  tag: props.makeStringProp("div"),
  theme: props.makeStringProp("light"),
  zIndex: Number,
  themeVars: Object,
  themeVarsDark: Object,
  themeVarsLight: Object,
  themeVarsScope: props.makeStringProp("local"),
  iconPrefix: String
};
function insertDash(str) {
  return str.replace(/([a-zA-Z])(\d)/g, "$1-$2");
}
function mapThemeVarsToCSSVars(themeVars) {
  const cssVars = {};
  Object.keys(themeVars).forEach((key) => {
    const formattedKey = insertDash(format.kebabCase(key));
    cssVars[`--van-${formattedKey}`] = themeVars[key];
  });
  return cssVars;
}
function syncThemeVarsOnRoot(newStyle = {}, oldStyle = {}) {
  Object.keys(newStyle).forEach((key) => {
    if (newStyle[key] !== oldStyle[key]) {
      document.documentElement.style.setProperty(key, newStyle[key]);
    }
  });
  Object.keys(oldStyle).forEach((key) => {
    if (!newStyle[key]) {
      document.documentElement.style.removeProperty(key);
    }
  });
}
Vue.defineComponent({
  name,
  props: configProviderProps,
  setup(props2, {
    slots
  }) {
    const style = Vue.computed(() => mapThemeVarsToCSSVars(basic.extend({}, props2.themeVars, props2.theme === "dark" ? props2.themeVarsDark : props2.themeVarsLight)));
    if (basic.inBrowser) {
      const addTheme = () => {
        document.documentElement.classList.add(`van-theme-${props2.theme}`);
      };
      const removeTheme = (theme = props2.theme) => {
        document.documentElement.classList.remove(`van-theme-${theme}`);
      };
      Vue.watch(() => props2.theme, (newVal, oldVal) => {
        if (oldVal) {
          removeTheme(oldVal);
        }
        addTheme();
      }, {
        immediate: true
      });
      Vue.onActivated(addTheme);
      Vue.onDeactivated(removeTheme);
      Vue.onBeforeUnmount(removeTheme);
      Vue.watch(style, (newStyle, oldStyle) => {
        if (props2.themeVarsScope === "global") {
          syncThemeVarsOnRoot(newStyle, oldStyle);
        }
      });
      Vue.watch(() => props2.themeVarsScope, (newScope, oldScope) => {
        if (oldScope === "global") {
          syncThemeVarsOnRoot({}, style.value);
        }
        if (newScope === "global") {
          syncThemeVarsOnRoot(style.value, {});
        }
      });
      if (props2.themeVarsScope === "global") {
        syncThemeVarsOnRoot(style.value, {});
      }
    }
    Vue.provide(CONFIG_PROVIDER_KEY, props2);
    Vue.watchEffect(() => {
      if (props2.zIndex !== void 0) {
        useGlobalZIndex.setGlobalZIndex(props2.zIndex);
      }
    });
    return () => Vue.createVNode(props2.tag, {
      "class": bem(),
      "style": props2.themeVarsScope === "local" ? style.value : void 0
    }, {
      default: () => {
        var _a;
        return [(_a = slots.default) == null ? void 0 : _a.call(slots)];
      }
    });
  }
});
exports.CONFIG_PROVIDER_KEY = CONFIG_PROVIDER_KEY;
exports.configProviderProps = configProviderProps;
