"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const log = require("../_utils/log.js");
const is = require("../_utils/is.js");
require("../config-provider/index.js");
const vue = require("vue");
const zhCn = require("./lang/zh-cn.js");
const provide = require("../config-provider/provide.js");
const currentLocal = vue.ref("zh-CN");
const i18nLanguage = vue.ref({
  "zh-CN": zhCn
});
function addLocale(locale, opts) {
  const locales = is.isArray(locale) ? locale : [locale];
  locales.forEach((lc) => {
    const currLocal = lc.locale;
    if (!currLocal) {
      return;
    }
    if (!i18nLanguage.value[currLocal]) {
      i18nLanguage.value[currLocal] = {
        locale: lc.locale
      };
    }
    Object.keys(lc).forEach((key) => {
      const k = key;
      if (!i18nLanguage.value[currLocal][k] || (opts == null ? void 0 : opts.overwrite)) {
        i18nLanguage.value[currLocal][k] = lc[key];
      }
    });
  });
}
function useLocale(localeKey) {
  if (!i18nLanguage.value[localeKey]) {
    log.log.warn(`no '${localeKey}' languages configed`);
    return;
  }
  currentLocal.value = localeKey;
}
function useI18n() {
  const instance = vue.getCurrentInstance();
  const configProvider = instance ? vue.inject(provide.configProviderInjectKey, {}) : null;
  const languages = vue.computed(() => {
    return (configProvider == null ? void 0 : configProvider.locale) ?? i18nLanguage.value[currentLocal.value];
  });
  const locale = vue.computed(() => languages.value.locale);
  const transform = (key, ...args) => {
    if (!languages.value) {
      log.log.warn("no languages configed");
      return "";
    }
    const value = languages.value[key];
    if (args.length > 0 && is.isString(value)) {
      return value.replace(/{(\d+)}/g, (match, index) => {
        return args[index] ?? match;
      });
    }
    if (is.isUndefined(value)) {
      log.log.warn(`Cannot translate the value of keypath '${key}'`);
    }
    return value;
  };
  return {
    locale,
    t: transform
  };
}
exports.addLocale = addLocale;
exports.useI18n = useI18n;
exports.useLocale = useLocale;
