import "@seamless/logger";
import { a as g, L as w } from "./initialize-logger.mjs";
import { g as p } from "./shared-logger.mjs";
import { g as I } from "./get-environment-data.mjs";
import "@seamless/store";
import { t as S } from "./try-catch.mjs";
import "./with-usage-tracking.mjs";
import { storage as T } from "../storage.mjs";
const _ = class l {
  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/`;
  static isBrowser = typeof window < "u";
  constructor() {
  }
  static getInstance() {
    return l.instance ||= new l(), l.instance;
  }
  async fetchCrowdinManifest(t) {
    if (l.isBrowser && this.manifestCache.has(t)) return this.manifestCache.get(t);
    let e = `${l.BASE_URL}/${t}/manifest.json`;
    try {
      let n = await fetch(e);
      if (!n.ok) throw Error(`Error fetching Crowdin manifest: ${n.statusText}`);
      let a = await n.json(), s = { files: a.files, languages: a.languages, custom_languages: a.custom_languages, timestamp: a.timestamp, content: a.content };
      return l.isBrowser && this.manifestCache.set(t, s), s;
    } catch (n) {
      throw console.error(n), n;
    }
  }
  async buildCrowdinTranslationUrl(t, e, n) {
    let a = new URL(`${l.BASE_URL}/${e}${t}`);
    return a.searchParams.append("timestamp", n), a;
  }
  async fetchCrowdinTranslation(t, e, n) {
    if (!l.isBrowser) {
      let s = await this.buildCrowdinTranslationUrl(t, e, n);
      return await this.fetchTranslation(s);
    }
    this.translationsCache.has(e) || this.translationsCache.set(e, {});
    let a = this.translationsCache.get(e);
    if (!a[t]) {
      let s = await this.buildCrowdinTranslationUrl(t, e, n);
      a[t] = await this.fetchTranslation(s);
    }
    return a[t];
  }
  buildPhraseTranslationUrl(t, e, n, a) {
    let s = new URL(`${l.BASE_PHRASE_URL}${e}/locales/${t}/download`);
    return s.searchParams.append("file_format", n), a && Object.keys(a).forEach((r) => {
      s.searchParams.append(r, a[r]);
    }), s;
  }
  async fetchPhraseTranslation(t, e, n, a) {
    if (!l.isBrowser) {
      let r = this.buildPhraseTranslationUrl(t, e, n, a);
      return await this.fetchTranslation(r);
    }
    this.translationsCache.has(e) || this.translationsCache.set(e, {});
    let s = this.translationsCache.get(e);
    if (!s[t]) {
      let r = this.buildPhraseTranslationUrl(t, e, n, a);
      s[t] = await this.fetchTranslation(r);
    }
    return s[t];
  }
  async fetchTranslation(t) {
    try {
      let e = await fetch(t);
      if (!e.ok) throw Error(`Error fetching translation: ${e.statusText}`);
      return await e.json();
    } catch (e) {
      throw console.error(`Error fetching translation: ${e}`), e;
    }
  }
  async getFileTranslations(t, e, n) {
    let { content: a, timestamp: s } = await this.fetchCrowdinManifest(e);
    t = Array.isArray(t) ? t : [t];
    let r = {};
    for (let i of t) {
      if (a && !Object.values(a).some((m) => m.includes(i))) throw Error(`File ${i} not found in the manifest`);
      let h = await this.fetchCrowdinTranslation(i, e, s);
      n?.merge ? r = { ...r, ...h } : r[i] = h;
    }
    return r;
  }
  async getLocaleTranslations(t, e, n, a) {
    if (n) return this.fetchPhraseTranslation(t, e, n, a);
    let { languages: s, content: r, custom_languages: i, timestamp: h } = await this.fetchCrowdinManifest(e), m = this.findMatchingLocale(t, [...s, ...i ? Object.keys(i) : []]), L = r[m];
    if (L.length === 0) throw Error(`No translation files found for locale ${m}`);
    let d = {};
    return await Promise.all(L.map(async (b) => {
      let R = await this.fetchCrowdinTranslation(b, e, h);
      d = { ...d, ...R };
    })), d;
  }
  findMatchingLocale(t, e) {
    if (e.includes(t)) return t;
    if (e.includes(t.toLowerCase())) return t.toLowerCase();
    let [n] = t.split("-"), a = e.find((s) => s === n);
    if (!a) throw Error(`Locale ${t} not found in the manifest. Available languages: ${e.join(", ")}`);
    return a;
  }
}.getInstance();
function P(o, t, e, n) {
  return _.getLocaleTranslations(o, t, e, n);
}
const U = "https://i18n.oneweb.mercedes-benz.com", B = "locales", E = "translations", v = "localesTimestamp", x = 1440 * 60 * 1e3;
function C(o) {
  return {
    storedLocalesStorageKey: `${E}.${o}.${B}`,
    storedLocalesStorageTimestampKey: `${E}.${o}.${v}`
  };
}
async function M(o) {
  const { storedLocalesStorageKey: t, storedLocalesStorageTimestampKey: e } = C(o), n = T.local.get(t), a = T.local.get(e);
  if (n && a) {
    const s = new Date(a);
    if ((/* @__PURE__ */ new Date()).getTime() - s.getTime() < x)
      return n;
  }
  return await F(o);
}
async function F(o) {
  const t = p(), { storedLocalesStorageKey: e, storedLocalesStorageTimestampKey: n } = C(o), a = `${U}/${o}/manifest.json`, { data: s, error: r } = await S(
    (async () => (await fetch(a, {
      method: "GET",
      headers: {
        "Content-Type": "application/json"
      }
    })).json())()
  ), i = s?.languages ?? [];
  return r || !i.length ? (t("Error fetching i18n locales", {
    args: [r ?? "Locales require to be fetched after a timeout"],
    group: w.TRANSLATION,
    level: r ? g.ERROR : g.INFO
  }), []) : (T.local.set(e, i), T.local.set(n, (/* @__PURE__ */ new Date()).toISOString()), i);
}
async function A(o, t) {
  const e = await M(t), [n] = o.split("-");
  return e.includes(o) || e.includes(n);
}
const N = "GB", y = "en", f = `${y}-${N}`;
let c = f;
const u = /* @__PURE__ */ new Map();
function G() {
  return c;
}
async function z(o, t) {
  const e = p(), { country: n = N, language: a = y } = (await I(t)).pageEnvironmentVariables;
  c = `${a}-${n}`;
  const s = c;
  let r = await A(c, o);
  if (!r) {
    if (c = f, r = await A(c, o), !r)
      return e(
        `Neither ${c} nor the default locale ${f} translations were found. No translations will be loaded.`,
        {
          args: [],
          level: g.INFO,
          group: w.TRANSLATION
        }
      ), u;
    e(`Translations for ${s} were not found, falling back to default locale ${f}.`, {
      args: [],
      level: g.INFO,
      group: w.TRANSLATION
    });
  }
  return await O(f, o), c === f || await O(c, o), u;
}
function $(o, t = "") {
  const e = {};
  for (const [n, a] of Object.entries(o)) {
    const s = t ? `${t}.${n}` : n;
    typeof a == "string" ? e[s] = a : K(a) && Object.assign(e, $(a, s));
  }
  return e;
}
function K(o) {
  return typeof o == "object" && o !== null;
}
async function O(o, t) {
  const e = p(), { data: n, error: a } = await S(P(o, t));
  if (a) {
    e(`Error fetching translations for ${o}.`, {
      args: [a],
      level: g.ERROR,
      group: w.TRANSLATION
    });
    return;
  }
  const s = $(n);
  u.set(o, {
    locale: o,
    messages: {
      [o]: s
    }
  });
}
function W() {
  const o = G(), { locale: t, messages: e } = u.get(o) ?? { locale: "unknown", messages: {} }, { locale: n, messages: a } = u.get(f) ?? {
    locale: "unknown",
    messages: {}
  }, s = e[t] || {}, r = a[n] || {};
  return {
    exist: (i) => i in s || i in r,
    t: (i) => s[i] || r[i] || i,
    availableLocales: Array.from(u.keys()),
    locale: t
  };
}
const J = {
  PRIVATE: "profile.context.private.registered",
  COMPANY: "profile.context.company.registered",
  GUEST_PRIVATE: "profile.context.private.guest",
  GUEST_COMPANY: "profile.context.company.guest",
  ADD_COMPANY: "profile.context.company.add",
  NOT_AVAILABLE: "profile.context.not.available",
  NOTIFICATIONS_PROFILES_NOT_AVAILABLE: "profile.context.notifications.profilesNotAvailable",
  PROFILE_NOT_AVAILABLE: "profile.context.not.available",
  NOTIFICATIONS_CONTEXT_CHANGE_PRIVATE: "profile.context.notification.change.private",
  NOTIFICATIONS_CONTEXT_CHANGE_BUSINESS: "profile.context.notification.change.business",
  NOTIFICATIONS_CONTEXT_CHANGE_BUSINESS_GUEST: "profile.context.notification.change.business.guest",
  LOGIN: "profile.context.navigation.login",
  DROPDOWN_LABEL: "profile.context.dropdown.label"
};
export {
  J as P,
  z as l,
  W as u
};
//# sourceMappingURL=profile-context-keys.mjs.map
