// @ts-nocheck

/* eslint-disable */
// Extensions
import { Service } from "../service/index.mjs"; // Utilities

import { getObjectValueByPath } from "../../util/helpers.mjs";
import { consoleError, consoleWarn } from "../../util/console.mjs"; // Types

const LANG_PREFIX = '$vuetify.';
const fallback = Symbol('Lang fallback');

function getTranslation(locale, key, usingDefault = false, defaultLocale) {
  const shortKey = key.replace(LANG_PREFIX, '');
  let translation = getObjectValueByPath(locale, shortKey, fallback);

  if (translation === fallback) {
    if (usingDefault) {
      consoleError("Translation key \"".concat(shortKey, "\" not found in fallback"));
      translation = key;
    } else {
      consoleWarn("Translation key \"".concat(shortKey, "\" not found, falling back to default"));
      translation = getTranslation(defaultLocale, key, true, defaultLocale);
    }
  }

  return translation;
}

export class Lang extends Service {
  static property = 'lang';
  defaultLocale = 'en';

  constructor(preset) {
    super();
    const {
      current,
      locales,
      t
    } = preset[Lang.property];
    this.current = current;
    this.locales = locales;
    this.translator = t || this.defaultTranslator;
  }

  currentLocale(key) {
    const translation = this.locales[this.current];
    const defaultLocale = this.locales[this.defaultLocale];
    return getTranslation(translation, key, false, defaultLocale);
  }

  t(key, ...params) {
    if (!key.startsWith(LANG_PREFIX)) return this.replace(key, params);
    return this.translator(key, ...params);
  }

  defaultTranslator(key, ...params) {
    return this.replace(this.currentLocale(key), params);
  }

  replace(str, params) {
    return str.replace(/\{(\d+)\}/g, (match, index) => {
      /* istanbul ignore next */
      return String(params[+index]);
    });
  }

}
//# sourceMappingURL=index.mjs.map