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

import { registerPublicAssetServiceWorker } from "./public-asset-cache.js";
import { AdminAuthPage } from "./admin-auth.js";
import { UserAuthPage } from "./user-auth.js";
import { AccountSettingsPage } from "./account-settings.js";
import { AdminUsersPage } from "./admin-users.js";
import { AdminModelsPage } from "./admin-models.js";
import { AdminAssetsPage } from "./admin-assets.js";
import { CreditsPage } from "./credits-page.js";
import { ProjectDetailPage, ProjectsPage, WorkspacePage } from "./project-pages.js";
import { EditorPage } from "./editor-page.js";

const root = document.getElementById("root");

if (!root) {
  throw new Error("Dada web root element is missing.");
}

// Cache failure leaves public assets network-backed and must not create alternate persistence.
void registerPublicAssetServiceWorker().catch(() => undefined);

const appRoot = createRoot(root);
let authRevision = 0;

function renderAuthenticationEntry() {
  authRevision += 1;
  const editorProject = window.location.pathname.match(/^\/app\/projects\/([0-9a-f-]{36})\/editor$/i);
  const projectDetail = window.location.pathname.match(/^\/app\/projects\/([0-9a-f-]{36})$/i);
  let authenticationPage;
  if (window.location.pathname === "/app/settings") authenticationPage = <AccountSettingsPage key={authRevision} />;
  else if (window.location.pathname === "/app/credits") authenticationPage = <CreditsPage key={authRevision} />;
  else if (editorProject?.[1]) authenticationPage = <EditorPage key={authRevision} projectId={editorProject[1]} />;
  else if (projectDetail?.[1]) authenticationPage = <ProjectDetailPage key={authRevision} projectId={projectDetail[1]} />;
  else if (window.location.pathname === "/app/projects") authenticationPage = <ProjectsPage key={authRevision} />;
  else if (window.location.pathname === "/app") authenticationPage = <WorkspacePage key={authRevision} />;
  else if (window.location.pathname === "/admin/users") authenticationPage = <AdminUsersPage key={authRevision} />;
  else if (window.location.pathname === "/admin/models") authenticationPage = <AdminModelsPage key={authRevision} />;
  else if (window.location.pathname === "/admin/assets") authenticationPage = <AdminAssetsPage key={authRevision} />;
  else if (window.location.pathname.startsWith("/admin")) authenticationPage = <AdminAuthPage key={authRevision} />;
  else authenticationPage = <UserAuthPage key={authRevision} />;
  appRoot.render(
    <StrictMode>
      {authenticationPage}
    </StrictMode>,
  );
}

// Any authenticated surface can dispatch this after a revoked/invalid session response.
window.addEventListener("dada:session-invalid", () => {
  if (window.location.pathname.startsWith("/app")) window.history.replaceState(null, "", "/");
  renderAuthenticationEntry();
});
renderAuthenticationEntry();
