import { STAGES as i } from "../constants.mjs";
const l = /* @__PURE__ */ new Map([
  ["PROD", "https://assets.oneweb.mercedes-benz.com/plugin"],
  ["PPROD", "https://preprod.assets.oneweb.mercedes-benz.com/plugin"],
  ["INT", "https://int.assets.oneweb.mercedes-benz.com/plugin"],
  ["TEST", "https://test.assets.oneweb.mercedes-benz.com/plugin"],
  ["DEV", "https://dev.assets.oneweb.mercedes-benz.com/plugin"]
]), b = (n) => typeof n.attrs.nomodule > "u";
function p(n = window.location.href) {
  const { hostname: e } = new URL(n);
  let t = i.PROD;
  return e.includes("pprod") ? t = i.PPROD : e.includes("int") ? t = i.INT : e.includes("test") ? t = i.TEST : e.includes("dev") ? t = i.DEVELOP : e.includes("localhost") && (t = i.INT), t.toUpperCase();
}
function y(n = window.location.href) {
  const e = p(n);
  return l.get(e) ?? l.get(i.PROD.toUpperCase());
}
async function d(n) {
  const { environment: e = "PROD", version: t = "", name: r } = n, c = l.has(e) ? l.get(e) : l.get("PROD"), s = `/${r}/manifest${t ? `-${t}` : ""}.json?now=${Date.now()}`, u = new URL(`${c}${s}`);
  return fetch(u).then((m) => m.json());
}
function g(n, e = {
  location: window.document.body,
  // biome-ignore lint/suspicious/noEmptyBlockStatements: <explanation>
  callback: () => {
  },
  attributes: { type: "module", async: !1, defer: !1 }
}) {
  const { attributes: t, callback: r, location: c } = e, { async: s, defer: u, type: m, ...o } = t || {}, f = document.querySelector(`script[src="${n}"]`);
  if (f !== null)
    return r.apply(e.callback, [c, f]);
  const a = document.createElement("script");
  a.src = n, a.type = m, a.async = s, a.defer = u, o && Object.keys(o).forEach((w) => a.setAttribute(w, o[w])), a.onload = () => r.apply(e.callback, [c, a]), a.onerror = () => r.apply(e.callback, [new Error("Failed to load script")]), c.appendChild(a);
}
async function $(n) {
  const { name: e, environment: t = p(), version: r = "" } = n, c = await d({ name: e, environment: t, version: r }).catch((s) => {
    throw new Error(`Failed to load manifest: plugin 
 ${s.message}`);
  });
  return `${l.get(t)}/${e}/${e}-${c.version}`;
}
async function P(n, e = window.document.body) {
  const t = await d(n).catch((s) => {
    throw new Error(`Failed to load manifest: ${n.name} 
 ${s.message}`);
  }), { scripts: r } = t.config.files, c = r.filter(b).map((s) => ({
    url: `${l.get(
      n?.environment ?? i.PROD.toUpperCase()
    )}${s.url}`,
    options: {
      attributes: { type: "module", async: !1, defer: !1, ...s.attrs }
    }
  }));
  return Promise.all(
    c.map((s) => new Promise((u, m) => {
      g(s.url, {
        ...s.options,
        location: e,
        callback: (o) => {
          o instanceof Error ? m(o) : u(o);
        }
      });
    }))
  );
}
async function v(n) {
  const { name: e, environment: t = p(), version: r = "" } = n, { importModule: c = async (o) => await import(o) } = window || {}, s = await d({ name: e, environment: t, version: r }).catch((o) => {
    throw new Error(`Failed to load manifest: ${e} 
 ${o.message}`);
  }), { scripts: u } = s.config.files, [m] = u.filter(b).map((o) => `${l.get(t)}${o.url}`);
  return c(m);
}
export {
  l as AssetsDomains,
  y as environmentAssetsUrlResolution,
  p as environmentResolution,
  $ as getPluginBasePath,
  v as importPlugin,
  d as loadManifest,
  g as loader,
  P as registerPlugin
};
//# sourceMappingURL=loader.mjs.map
