import { RawBundleSource as i, LocaleManager as u } from "@dialpad/i18n";
import f from "./en-US.js";
import d from "./zh-CN.js";
import m from "./nl-NL.js";
import p from "./fr-FR.js";
import g from "./de-DE.js";
import h from "./it-IT.js";
import S from "./ja-JP.js";
import w from "./pt-BR.js";
import N from "./ru-RU.js";
import L from "./es-LA.js";
const r = "dialtone", t = {
  ENGLISH: "en-US",
  CHINESE: "zh-CN",
  DUTCH: "nl-NL",
  FRENCH: "fr-FR",
  GERMAN: "de-DE",
  ITALIAN: "it-IT",
  JAPANESE: "ja-JP",
  PORTUGUESE: "pt-BR",
  RUSSIAN: "ru-RU",
  SPANISH: "es-LA"
}, l = "en-US", c = "user-locale";
class o {
  /**
   * @prop {import('@dialpad/i18n').UseI18N} i18n
   * @private
   */
  constructor(e = null) {
    if (typeof o.instance == "object")
      return o.instance;
    this._locale = e || o.getPreferredLocale();
    const a = new i({
      resources: i.builtResources([
        ["en-US", r, f],
        ["zh-CN", r, d],
        ["nl-NL", r, m],
        ["fr-FR", r, p],
        ["de-DE", r, g],
        ["it-IT", r, h],
        ["ja-JP", r, S],
        ["pt-BR", r, w],
        ["ru-RU", r, N],
        ["es-LA", r, L]
      ])
    }), n = new u({
      bundleSource: a,
      allowedLocales: Object.values(t),
      fallbackLocale: l,
      preferredLocale: this._locale,
      namespaces: [r]
    });
    return o.instance = this, this.i18n = n.useI18N(r), typeof window < "u" && (window.onstorage = (s) => {
      s.key === c && (this.currentLocale = s.newValue);
    }), this;
  }
  /**
   * Gets the preferred locale from user's locale stored in localStorage or the browser language
   * @returns { string }
   */
  static getPreferredLocale() {
    if (typeof window > "u" || !window.localStorage)
      return l;
    const e = window.localStorage.getItem(c), a = Object.values(t).find((n) => n.startsWith(navigator.language.slice(0, 2)));
    return e || a || l;
  }
  static getAllowedLocales() {
    return t;
  }
  /**
   * Passthrough function to the i18n $t function including the dialtone namespace
   * Returns a translated string based on a key and optional variables.
   * It’s used for simple text translations.
   * https://github.com/dialpad/goblin-client-tools/tree/main/packages/i18n#t
   */
  $t(...e) {
    return this.i18n.$t(...e);
  }
  /**
   * Passthrough function to the i18n $ta function including the dialtone namespace
   * Returns an object containing translated attributes to pass directly as props to components, it can contain
   * aria-label, title, etc, rather than just a plain text. It’s useful for handling element attributes in the UI.
   * https://github.com/dialpad/goblin-client-tools/tree/main/packages/i18n#ta
   */
  $ta(...e) {
    return this.i18n.$ta(...e);
  }
  get currentLocale() {
    return this._locale;
  }
  set currentLocale(e) {
    if (e !== this._locale) {
      if (!Object.values(t).includes(e))
        throw new Error(`Locale ${e} is not allowed, please use one of the following: ${Object.values(t).join(", ")}`);
      this._locale = e, this.i18n.setI18N({ preferredLocale: e }, r);
    }
  }
}
export {
  o as DialtoneLocalization
};
//# sourceMappingURL=index.js.map
