/** React bootstrap: mounts the app inside the DI provider. */

import React from "react";
import ReactDOM from "react-dom/client";

import { App } from "./App";
import { ViewWindow, parseViewWindowParams } from "./ViewWindow";
import { WebApp } from "@/features/web";
import { DIProvider, resolveTransport } from "./di";
import "@/shared/styles/theme.css";

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

// #23/#47: a detached view window loads the SPA with `?panel=` (panel-only, no
// project id). When the param is present and valid, render only that view — it
// follows the main window's focused project; otherwise the full app.
const viewParams = parseViewWindowParams(window.location.search);

// Ticket #13, F2: in web (HTTP) transport mode the browser client renders the
// pairing-gated, read-only `WebApp`. Desktop (Tauri) is unchanged — it renders
// the full `App` (or a detached view window). Detached windows are desktop-only.
const isWeb = resolveTransport() === "http";

ReactDOM.createRoot(root).render(
  <React.StrictMode>
    <DIProvider>
      {isWeb ? (
        <WebApp />
      ) : viewParams ? (
        <ViewWindow panel={viewParams.panel} />
      ) : (
        <App />
      )}
    </DIProvider>
  </React.StrictMode>,
);
