/**
 * ldesign v0.4.2
 * (c) 2023 ldesign
 * @license MIT
 */

import { ref, getCurrentInstance, inject, computed, h, provide } from 'vue';
import cloneDeep from 'lodash/cloneDeep';
import isFunction from 'lodash/isFunction';
import isString from 'lodash/isString';
import { configProviderInjectKey, defaultGlobalConfig, mergeWith } from './context.js';
import './type.js';
import 'lodash/mergeWith';
import 'lodash/merge';
import 'lodash/isArray';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '@babel/runtime/helpers/typeof';
import '../_chunks/dep-2e5d8948.js';
import 'dayjs';
import '../_chunks/dep-4580aabe.js';

var globalConfigCopy = ref();
function useConfig(componentName) {
  var injectGlobalConfig = getCurrentInstance() ? inject(configProviderInjectKey, null) : globalConfigCopy;
  var mergedGlobalConfig = computed(function () {
    return (injectGlobalConfig === null || injectGlobalConfig === void 0 ? void 0 : injectGlobalConfig.value) || defaultGlobalConfig;
  });
  var globalConfig = computed(function () {
    return mergedGlobalConfig.value[componentName];
  });
  var classPrefix = computed(function () {
    return mergedGlobalConfig.value.classPrefix;
  });
  var t = function t(pattern) {
    for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
      args[_key - 1] = arguments[_key];
    }
    var data = args[0];
    if (isString(pattern)) {
      if (!data) return pattern;
      var regular = /\{\s*([\w-]+)\s*\}/g;
      var translated = pattern.replace(regular, function (match, key) {
        if (data) return String(data[key]);
        return "";
      });
      return translated;
    }
    if (isFunction(pattern)) {
      if (!args.length) return pattern(h);
      return pattern.apply(void 0, args);
    }
    return "";
  };
  return {
    t: t,
    global: globalConfig,
    globalConfig: globalConfig,
    classPrefix: classPrefix
  };
}
var provideConfig = function provideConfig(props) {
  var defaultData = cloneDeep(defaultGlobalConfig);
  var mergedGlobalConfig = computed(function () {
    return mergeWith(defaultData, props.globalConfig);
  });
  provide(configProviderInjectKey, mergedGlobalConfig);
  if (!globalConfigCopy.value) globalConfigCopy.value = mergedGlobalConfig.value;
  return mergedGlobalConfig;
};

export { provideConfig, useConfig };
//# sourceMappingURL=useConfig.js.map
