import { msg as h, LOCALE_STATUS_EVENT as g } from "@lit/localize";
import { targetLocales as p } from "./generated/locale-codes.js";
import { _installMsgImplementation as L } from "@lit/localize/init/install.js";
import { runtimeMsg as w } from "@lit/localize/internal/runtime-msg.js";
const n = "@polyfea/user-locale", u = "register-locale-module", m = "load-locales", b = (l, t) => t ? h(l, { id: t }) : h(l), S = (l, t, s) => {
  const e = new CustomEvent(u, {
    detail: { locales: t, module: l, loader: s },
    bubbles: !0,
    composed: !0
  });
  window.dispatchEvent(e);
}, f = async (l) => {
  l ? localStorage.setItem(n, l) : localStorage.removeItem(n), window.dispatchEvent(new CustomEvent(m, {
    detail: { locale: l },
    bubbles: !0,
    composed: !0
  }));
}, T = () => {
  const l = localStorage.getItem(n);
  return f(l || ""), l || "";
}, $ = () => localStorage.getItem(n) || "";
class A {
  #o = /* @__PURE__ */ new Map();
  #s = {};
  #l = {};
  #t = "";
  #r = /* @__PURE__ */ new Set();
  #i = {};
  #e = "";
  /**
   * Configures the localization mechanism with override paths and initializes the runtime msg implementation.
   */
  configureLocalization(t, s) {
    this.#r = new Set(t), this.#t = s, this.#t.endsWith("/") || (this.#t += "/"), this.#t = new URL(this.#t, document.baseURI).toString(), globalThis.__polyfea_localization_installed || L(((e, o) => w(
      globalThis.__polyfea_localization_installed.templates(),
      e,
      o
    ))), globalThis.__polyfea_localization_installed = {
      templates: () => this.#i
    }, this.#n(
      "@polyfea/polyfea-md-shell",
      [...p],
      (e) => import(
        /* @vite-ignore */
        `./generated/locales/${e}.js`
      )
    ), window.addEventListener(u, async (e) => {
      const o = e, { module: a, locales: i, loader: r } = o.detail;
      await this.#n(a, i, r);
    }), window.addEventListener(m, async (e) => {
      const o = e, { locale: a } = o.detail;
      await this.#h(a);
    });
  }
  /**
   * Registers a dynamic locale loader for a specific module.
   */
  async #n(t, s, e) {
    if (!this.#o.has(t) && (this.#o.set(t, { locales: new Set(s), loader: e }), this.#e)) {
      this.#a({ status: "loading", locale: this.#e });
      const o = await this.#d(t, this.#e);
      this.#s = { ...this.#s, ...o }, this.#i = { ...this.#s, ...this.#l }, this.#a({ status: "ready", locale: this.#e });
    }
  }
  /**
   * Sets the active locale and triggers the loading of all registered module translations and overrides.
   */
  async #h(t) {
    let s = {};
    this.#a({ status: "loading", locale: t });
    const e = Array.from(this.#o.keys()).map(
      (a) => this.#d(a, t)
    );
    this.#t && e.push(
      this.#u(t).then((a) => (s = a, {}))
    );
    const o = await Promise.all(e);
    this.#s = o.reduce((a, i) => ({ ...a, ...i }), {}), this.#l = { ...s }, this.#i = { ...this.#s, ...this.#l }, this.#e = t, this.#a({ status: "ready", locale: t });
  }
  /**
   * Resolves the best supported locale from a list of preferred locales against a set of available locales.
   */
  #c(t, s) {
    let e = "";
    for (const o of t) {
      if (s.has(o)) {
        e = o;
        break;
      }
      const a = o.split("-")[0];
      if (s.has(a)) {
        e = a;
        break;
      }
    }
    return e;
  }
  #a(t) {
    window.dispatchEvent(new CustomEvent(g, {
      detail: t,
      bubbles: !0,
      composed: !0
    }));
  }
  async #d(t, s) {
    const e = this.#o.get(t), { locales: o, loader: a } = e;
    let i = {}, r = navigator.languages || [navigator.language];
    s && (r = [s, ...r]);
    const c = this.#c(
      r,
      e.locales
    );
    if (o.has(c))
      try {
        i = (await a(c)).templates || {};
      } catch (d) {
        console.warn(`Failed to load locale ${this.#e} for module ${t}: ${d}`);
      }
    return i;
  }
  async #u(t) {
    let s = navigator.languages || [navigator.language];
    t && (s = [t, ...s]);
    const e = this.#c(
      s,
      this.#r
    );
    let o = {};
    try {
      o = (await import(
        /* @vite-ignore */
        this.#t + `${e}.js`
      )).templates || {};
    } catch (a) {
      console.warn(
        `Failed to load locale ${t} for system overrides from  ${this.#t + `${e}.js`}: ${a}`
      );
    }
    return o;
  }
}
export {
  n as LOCALE_STORAGE_KEY,
  A as LocalizationRegistry,
  u as REGISTER_LOCALE_EVENT,
  $ as getLocale,
  T as loadLocale,
  b as loc,
  S as registerLocaleModule,
  f as setLocale
};
