import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import "./index.css";

import { Providers } from "./app/providers";
import { bootSession } from "./features/auth/api";

async function start(): Promise<void> {
  if (import.meta.env.VITE_PREVIEW === "1") {
    // Preview build only: swap the transport for the mock before anything calls the API.
    const { installPreview } = await import("./preview/install");
    installPreview();
  }

  const root = document.getElementById("root");
  if (!root) throw new Error("Missing #root element");

  // Render at once (RequireAuth shows the shell skeleton while the session is read), then boot the session.
  createRoot(root).render(
    <StrictMode>
      <Providers />
    </StrictMode>,
  );
  void bootSession();
}

void start();
