'use strict';

var vue = require('vue');
var key = require('../../../constants/key.js');
var objects = require('../../../utils/src/objects.js');

const globalConfig = vue.ref();
function useGlobalConfig(key$1, defaultValue = void 0) {
  const config = vue.getCurrentInstance() ? vue.inject(key.configProviderContextKey, globalConfig) : globalConfig;
  if (key$1) {
    return vue.computed(() => config.value?.[key$1] ?? defaultValue);
  } else {
    return config;
  }
}
const provideGlobalConfig = (config, app, global = false) => {
  const inSetup = !!vue.getCurrentInstance();
  const oldConfig = inSetup ? useGlobalConfig() : void 0;
  const provideFn = app?.provide ?? (inSetup ? vue.provide : void 0);
  if (!provideFn) {
    return;
  }
  const context = vue.computed(() => {
    const cfg = vue.unref(config) ?? {};
    if (!oldConfig?.value) return cfg;
    return mergeConfig(oldConfig.value, cfg);
  });
  if (app?.provide) {
    app.provide(key.configProviderContextKey, context);
  } else if (inSetup) {
    vue.provide(key.configProviderContextKey, context);
  }
  if (global || !globalConfig.value) {
    globalConfig.value = context.value;
  }
  if (app?.provide) {
    app.provide(key.ImportMetaEnvKey, context.value.env);
  } else if (inSetup) {
    vue.provide(key.ImportMetaEnvKey, context.value.env);
  }
  return context;
};
const mergeConfig = (a, b) => {
  const keys = [.../* @__PURE__ */ new Set([...objects.keysOf(a), ...objects.keysOf(b)])];
  const obj = {};
  for (const key of keys) {
    obj[key] = b[key] ?? a[key];
  }
  return obj;
};

exports.provideGlobalConfig = provideGlobalConfig;
exports.useGlobalConfig = useGlobalConfig;
//# sourceMappingURL=index.js.map
