import { ref as v, createApp as x } from "vue";
import { initTheme as k } from "@das-fed/web/packages/theme/index";
import { initI18n as w } from "@das-fed/web/packages/i18n/index";
import { DasWebLayout as W } from "@das-fed/web/packages/layout/index";
import { createPinia as $ } from "pinia";
import j from "pinia-plugin-persistedstate";
import { createRouter as y, createWebHistory as O } from "vue-router";
import { getConfig as P } from "@das-fed/utils/config";
const C = {
  install: (e, t) => {
    var u;
    const c = (u = t == null ? void 0 : t.plugins) == null ? void 0 : u.glob;
    c && Object.values(c).sort((a, l) => (a.index ?? 0) - (l.index ?? 0)).filter((a) => a ? !!(a.default && a.default.install || a.install) : !1).forEach((a) => {
      a.default && a.default.install && e.use(a.default, t), a.install && e.use(a, t);
    });
  }
}, m = $();
m.use(j);
const D = (e) => {
  var d, b, o, h, g;
  const t = ((d = e == null ? void 0 : e.router) == null ? void 0 : d.routerBase) ?? "/", c = ((b = e == null ? void 0 : e.router) == null ? void 0 : b.options) || {};
  let u = Object.assign(((o = e == null ? void 0 : e.router) == null ? void 0 : o.glob) || {});
  const a = Object.values(u).filter((s) => !!s).flat().sort((s, n) => (s.index ?? 0) - (n.index ?? 0)), l = [];
  (h = e == null ? void 0 : e.microApps) == null || h.map((s) => {
    var A;
    const n = Object.values(((A = s.router) == null ? void 0 : A.glob) || {}).filter((r) => !!r).flat().sort((r, R) => (r.index ?? 0) - (R.index ?? 0));
    l.push(
      ...n.map((r) => ({ ...r, path: `/${s.microAppName}${r.path}` }))
    ), l.push(
      ...n.filter((r) => !!r.path).filter((r) => r.path !== "").filter((r) => r.path !== "/").filter((r) => !r.path.startsWith(`/${r.microAppName}`)).map((r) => ({
        path: r.path,
        redirect: `/${s.microAppName}${r.path}`,
        meta: { ...r.meta || {}, isMicroAppRouteRedirect: !0 }
      }))
    );
  });
  const f = ((g = e == null ? void 0 : e.router) == null ? void 0 : g.routers) || [], p = [
    ...i(a, e),
    ...i(f, e),
    ...i(l, e)
  ];
  return { router: y({ history: O(t), routes: p, ...c }) };
}, i = (e, t) => e.map((c) => {
  const u = c.meta || {};
  return u.keepAlive = u.keepAlive ?? (t == null ? void 0 : t.keepAlive) ?? !0, { ...c, meta: u };
}), N = async () => {
  const { baseURL: e = "" } = P();
  let t = !1;
  try {
    const c = await fetch(`${e}/oss/resource/front-end/web/_config/config.json`);
    if (c.ok) {
      const u = await c.json().catch(() => ({}));
      t = (u == null ? void 0 : u.disableSaveToken) ?? !1;
    }
  } catch {
  }
  window.webConfig = { disableSaveToken: t };
}, _ = (e) => {
  e || (e = {});
  const t = v(!0);
  Promise.all([N(), k(e), w(e)]).then(() => {
    t.value = !1;
  });
  const { container: c, component: u, props: a } = (e == null ? void 0 : e.root) || {}, l = x(u || W, a);
  l.use(C, e), l.use(m);
  const { router: f } = D(e);
  return l.use(f), l.mount(c || "#app"), { app: l, router: f, loading: t, store: m };
};
export {
  _ as createDasWebApp
};
