"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const objects = require("../../../../utils/objects.js");
const error = require("../../../../utils/error.js");
const index$1 = require("../../../../hooks/use-locale/index.js");
const index = require("../../../../hooks/use-namespace/index.js");
const index$2 = require("../../../../hooks/use-z-index/index.js");
const index$3 = require("../../../../hooks/use-size/index.js");
const index$4 = require("../../../../hooks/use-empty-values/index.js");
const constants = require("../constants.js");
const Vue = require("vue");
const globalConfig = Vue.ref();
function useGlobalConfig(key, defaultValue = void 0) {
  const config = Vue.getCurrentInstance() ? Vue.inject(constants.configProviderContextKey, globalConfig) : globalConfig;
  if (key) return Vue.computed(() => {
    var _a;
    return ((_a = config.value) == null ? void 0 : _a[key]) ?? defaultValue;
  });
  else return config;
}
function useGlobalComponentSettings(block, sizeFallback) {
  const config = useGlobalConfig();
  const ns = index.useNamespace(block, Vue.computed(() => {
    var _a;
    return ((_a = config.value) == null ? void 0 : _a.namespace) || index.defaultNamespace;
  }));
  const locale = index$1.useLocale(Vue.computed(() => {
    var _a;
    return (_a = config.value) == null ? void 0 : _a.locale;
  }));
  const zIndex = index$2.useZIndex(Vue.computed(() => {
    var _a;
    return ((_a = config.value) == null ? void 0 : _a.zIndex) || index$2.defaultInitialZIndex;
  }));
  const size = Vue.computed(() => {
    var _a;
    return Vue.unref(sizeFallback) || ((_a = config.value) == null ? void 0 : _a.size) || "";
  });
  provideGlobalConfig(Vue.computed(() => Vue.unref(config) || {}));
  return {
    ns,
    locale,
    zIndex,
    size
  };
}
const provideGlobalConfig = (config, app, global = false) => {
  const inSetup = !!Vue.getCurrentInstance();
  const oldConfig = inSetup ? useGlobalConfig() : void 0;
  const provideFn = (app == null ? void 0 : app.provide) ?? (inSetup ? Vue.provide : void 0);
  if (!provideFn) {
    error.debugWarn("provideGlobalConfig", "provideGlobalConfig() can only be used inside setup().");
    return;
  }
  const context = Vue.computed(() => {
    const cfg = Vue.unref(config);
    if (!(oldConfig == null ? void 0 : oldConfig.value)) return cfg;
    return mergeConfig(oldConfig.value, cfg);
  });
  provideFn(constants.configProviderContextKey, context);
  provideFn(index$1.localeContextKey, Vue.computed(() => context.value.locale));
  provideFn(index.namespaceContextKey, Vue.computed(() => context.value.namespace));
  provideFn(index$2.zIndexContextKey, Vue.computed(() => context.value.zIndex));
  provideFn(index$3.SIZE_INJECTION_KEY, { size: Vue.computed(() => context.value.size || "") });
  provideFn(index$4.emptyValuesContextKey, Vue.computed(() => ({
    emptyValues: context.value.emptyValues,
    valueOnClear: context.value.valueOnClear
  })));
  if (global || !globalConfig.value) globalConfig.value = context.value;
  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] !== void 0 ? b[key] : a[key];
  return obj;
};
exports.provideGlobalConfig = provideGlobalConfig;
exports.useGlobalComponentSettings = useGlobalComponentSettings;
exports.useGlobalConfig = useGlobalConfig;
