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

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

import { App } from "./App";
import { PluginLayoutWindow, ViewWindow, parseViewWindowParams } from "./ViewWindow";
import { WebApp } from "@/features/web";
import { DIProvider, resolveTransport } from "./di";
import { RootErrorBoundary, installGlobalErrorLogging } from "./RootErrorBoundary";
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 #74, F1: publish the transport this bundle was built for. `define`
// inlines it as a literal, and assigning it is a side effect, so it survives
// minification and tree-shaking — a built bundle can be identified (by QA, or
// from devtools) without grepping for a minified symbol. Both adapter sets ship
// in either bundle, so their mere presence proves nothing.
(window as unknown as { __IDEA_TRANSPORT__?: string }).__IDEA_TRANSPORT__ =
  __IDEA_TRANSPORT__;

// 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";

installGlobalErrorLogging();

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