import { storage as l } from "./core/storage.mjs";
class c {
  static instance;
  translationsCache = /* @__PURE__ */ new Map();
  manifestCache = /* @__PURE__ */ new Map();
  static BASE_URL = "https://i18n.oneweb.mercedes-benz.com";
  static BASE_PHRASE_URL = `${this.BASE_URL}/projects/`;
  constructor() {
  }
  static getInstance() {
    return c.instance || (c.instance = new c()), c.instance;
  }
  async fetchCrowdinManifest(t) {
    if (this.manifestCache.has(t)) return this.manifestCache.get(t);
    const a = `${c.BASE_URL}/${t}/manifest.json`;
    try {
      const n = await fetch(a);
      if (!n.ok) throw new Error(`Error fetching Crowdin manifest: ${n.statusText}`);
      const s = await n.json(), e = { files: s.files, languages: s.languages, timestamp: s.timestamp, content: s.content };
      return this.manifestCache.set(t, e), e;
    } catch (n) {
      throw console.error(n), n;
    }
  }
  async fetchCrowdinTranslation(t, a) {
    this.translationsCache.has(a) || this.translationsCache.set(a, {});
    const n = this.translationsCache.get(a);
    if (!n[t]) {
      const { timestamp: s } = await this.fetchCrowdinManifest(a), e = new URL(`${c.BASE_URL}/${a}${t}`);
      e.searchParams.append("timestamp", s);
      const i = await this.fetchTranslation(e);
      n[t] = i;
    }
    return n[t];
  }
  async fetchPhraseTranslation(t, a, n, s) {
    this.translationsCache.has(a) || this.translationsCache.set(a, {});
    const e = this.translationsCache.get(a);
    if (!e[t]) {
      const i = new URL(`${c.BASE_PHRASE_URL}${a}/locales/${t}/download`);
      i.searchParams.append("file_format", n), s && Object.keys(s).forEach(((o) => {
        i.searchParams.append(o, s[o]);
      }));
      const r = await this.fetchTranslation(i);
      e[t] = r;
    }
    return e[t];
  }
  async fetchTranslation(t) {
    try {
      const a = await fetch(t);
      if (!a.ok) throw new Error(`Error fetching translation: ${a.statusText}`);
      return await a.json();
    } catch (a) {
      throw console.error(`Error fetching translation: ${a}`), a;
    }
  }
  async getFileTranslations(t, a, n) {
    const { content: s } = await this.fetchCrowdinManifest(a);
    t = Array.isArray(t) ? t : [t];
    let e = {};
    for (const i of t) {
      if (s && !Object.values(s).some(((o) => o.includes(i)))) throw new Error(`File ${i} not found in the manifest`);
      const r = await this.fetchCrowdinTranslation(i, a);
      n?.merge ? e = { ...e, ...r } : e[i] = r;
    }
    return e;
  }
  async getLocaleTranslations(t, a, n, s) {
    if (n) return this.fetchPhraseTranslation(t, a, n, s);
    const { languages: e, content: i } = await this.fetchCrowdinManifest(a), r = this.findMatchingLocale(t, e), o = i[r];
    if (o.length === 0) throw new Error(`No translation files found for locale ${r}`);
    let f = {};
    return await Promise.all(o.map((async (w) => {
      const u = await this.fetchCrowdinTranslation(w, a);
      f = { ...f, ...u };
    }))), f;
  }
  findMatchingLocale(t, a) {
    if (a.includes(t)) return t;
    const [n] = t.split("-"), s = a.find(((e) => e === n));
    if (!s) throw new Error(`Locale ${t} not found in the manifest`);
    return s;
  }
}
const d = c.getInstance();
function m(h, t, a, n) {
  return d.getLocaleTranslations(h, t, a, n);
}
const g = "b2x.iam.translations", C = 1440 * 60 * 1e3;
async function $(h) {
  const {
    distributionHash: t,
    projectName: a = h.distributionHash,
    country: n = h.country.toUpperCase(),
    language: s = h.language.toLowerCase()
  } = h, e = `${g}.${a}.${s}`, i = l.session.get(e, { data: null, timestamp: Date.now() });
  try {
    if (!i.data || p(i.timestamp)) {
      const r = await m(
        `${s}-${n}`,
        t
      );
      i.data = Object.values(r).reduce(
        (o, f) => (f.identifier && f.translation && (o[f.identifier] = f.translation), o),
        {}
      ), i.timestamp = Date.now(), l.session.set(e, i);
    }
  } catch {
    i.data = {};
  }
  return {
    exists: (r) => !!i.data[r],
    t: (r) => i.data[r] || r
  };
}
function p(h) {
  return Date.now() - h > C;
}
export {
  $ as initTranslations
};
//# sourceMappingURL=i18n.mjs.map
