// SPEC: PJ2026-010405 云端控制台.
// Implementation reference: draft-2026-07-13-p0-cloud-console.

import { createApp } from "vue";
import { createPinia } from "pinia";
import { PiniaColada } from "@pinia/colada";
import App from "./App.vue";
import router from "./router";
import i18n, { initI18n } from "./i18n";
import { useAppStore } from "@/stores/app";
import { workbenchRuntimePolicy } from "@/config/workbench-runtime-policy";
import { installWebRum } from "@/utils/rum";
import "./styles/tokens.css";
import "./style.css";
import "./styles/console-shell.css";
import "./styles/workbench.css";
import "./styles/console-pages.css";
import "./styles/console-foundation.css";
import "./styles/console-r2.css";
import "./styles/agent-observer.css";

function initThemeClass(): void {
  const savedTheme = localStorage.getItem("theme");
  const shouldUseDark = savedTheme === "dark" || (!savedTheme && window.matchMedia("(prefers-color-scheme: dark)").matches);
  document.documentElement.classList.toggle("dark", shouldUseDark);
}

async function bootstrap(): Promise<void> {
  initThemeClass();
  const app = createApp(App);
  const pinia = createPinia();
  const runtimePolicy = workbenchRuntimePolicy();
  app.use(pinia);
  app.use(PiniaColada, {
    queryOptions: {
      staleTime: runtimePolicy.workbenchReadFailureCooldownMs,
      gcTime: Math.max(runtimePolicy.workbenchReadFailureCooldownMs, runtimePolicy.sessionListMinRefreshIntervalMs)
    }
  });
  useAppStore().initFromInjectedConfig();
  await initI18n();
  app.use(router);
  app.use(i18n);
  await router.isReady();
  installWebRum();
  app.mount("#app");
}

void bootstrap();
