'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
var zhHans = require('../../locale/lang/zh-hans.js');

const useLocaleProps = {
  locale: {
    type: Object
  }
};
const LocaleInjectionKey = "VcLocaleInjection";
let localeObjCache;
function translate(path, option, current) {
  const paths = path.split(".");
  let value;
  for (let i = 0, j = paths.length; i < j; i++) {
    const property = paths[i];
    value = current[property];
    if (i === j - 1)
      return template(value, option);
    if (!value)
      return "";
    current = value;
  }
}
const useLocale = () => {
  const vm = vue.getCurrentInstance();
  const props = vm == null ? void 0 : vm.props;
  const locale = vue.computed(() => props.locale || zhHans["default"]);
  const lang = vue.computed(() => locale.value.name);
  const _translator = (...args) => {
    const [path, option] = args;
    return translate(path, option, locale.value) || "";
  };
  const t2 = (...args) => {
    return _translator(...args);
  };
  const provides = {
    locale,
    lang,
    t: t2
  };
  localeObjCache = provides;
  vue.provide(LocaleInjectionKey, provides);
};
function template(str, option) {
  if (!str || !option)
    return str;
  return str.replace(/\{(\w+)\}/g, (_, key) => {
    return option[key];
  });
}
const localeProviderMaker = (locale = zhHans["default"]) => {
  const lang = vue.ref(locale.name);
  const localeRef = vue.ref(locale);
  return {
    lang,
    locale: localeRef,
    t: (...args) => {
      const [path, option] = args;
      return translate(path, option, localeRef.value) || "";
    }
  };
};
const useLocaleInject = () => {
  return vue.inject(LocaleInjectionKey, localeObjCache || {
    lang: vue.ref(zhHans["default"].name),
    locale: vue.ref(zhHans["default"]),
    t: (...args) => {
      const [path, option] = args;
      return translate(path, option, zhHans["default"]);
    }
  });
};
const t = (...args) => {
  const [path, option] = args;
  return localeObjCache == null ? void 0 : localeObjCache.t(path, option);
};

exports.LocaleInjectionKey = LocaleInjectionKey;
exports.localeProviderMaker = localeProviderMaker;
exports.t = t;
exports.useLocale = useLocale;
exports.useLocaleInject = useLocaleInject;
exports.useLocaleProps = useLocaleProps;
//# sourceMappingURL=index.js.map
