import { mount } from "svelte";

import App from "./App.svelte";
import "./app.css";
import { installGlobalErrorTelemetry } from "./lib/telemetry";

const THEME_KEY = "gitlite.theme.v1";
const target = document.getElementById("app");

installGlobalErrorTelemetry();

if (!target) {
  throw new Error("App target element was not found.");
}

try {
  const storedTheme = localStorage.getItem(THEME_KEY);
  const preference = storedTheme === "light" || storedTheme === "dark" || storedTheme === "system"
    ? storedTheme
    : "system";
  const resolved = preference === "system" && window.matchMedia("(prefers-color-scheme: light)").matches
    ? "light"
    : preference === "system"
      ? "dark"
      : preference;
  document.documentElement.dataset.themePreference = preference;
  document.documentElement.dataset.theme = resolved;
} catch {
  // Storage is best-effort; the CSS system theme fallback still applies.
}

const app = mount(App, { target });

export default app;
