// Tiny history-API router for the operator app — no library, no bundler.
// Routes: / (overview) · /demo (demo line) · /projects/new[/:sessionId] (create) ·
//   /projects/:slug (board) · /projects/:slug/plan · /projects/:slug/reviews ·
//   /projects/:slug/line (line setup) · /projects/:slug/launch (launch) ·
//   /projects/:slug/planning (read-only post-launch planning canvas — R9c)
// The return carries an optional `sessionId` — set only on the create route; every
// other route leaves it null.
const { useState: useStateRouter, useEffect: useEffectRouter } = React;

function matchRoute(pathname) {
  if (pathname === "/" || pathname === "") return { name: "overview", slug: null, sessionId: null };
  if (pathname === "/demo" || pathname === "/demo/") return { name: "demo", slug: null, sessionId: null }; // demo line view — never "unknown" (redline 1)
  // Create-project surface — MUST precede the /projects/:slug match so `new` is a
  // reserved segment, not a board slug. `/projects/new-thing` still falls through to
  // the board match below (the `new` here is a whole segment, not a prefix).
  // The optional trailing segment is the workspace VIEW: the board lives in the
  // canvas, brief/timeline/stations are document-flow pages. Only the known view
  // names match, so a session id is never mistaken for a view. `/projects/new/<id>`
  // (no view) stays the canonical board link — every existing deep link still works.
  const n = pathname.match(/^\/projects\/new(?:\/([^/]+?))?(?:\/(board|brief|timeline|stations))?\/?$/);
  if (n) return { name: "create", slug: null, sessionId: n[1] ? decodeURIComponent(n[1]) : null, view: n[2] || "board" };
  const m = pathname.match(/^\/projects\/([^/]+)(?:\/(plan|reviews|line|launch|planning))?\/?$/);
  if (m) return { name: m[2] || "board", slug: decodeURIComponent(m[1]), sessionId: null };
  return { name: "unknown", slug: null, sessionId: null };
}

// `replace` swaps the current history entry instead of pushing a new one. Redirects
// want it: a draft slug bouncing to its create workspace would otherwise leave the
// dead-end URL in history, and Back would land on it and redirect forward again.
function navigate(path, { replace = false } = {}) {
  if (path === window.location.pathname) return;
  if (replace) window.history.replaceState({}, "", path);
  else window.history.pushState({}, "", path);
  window.dispatchEvent(new Event("al-navigate"));
}

function useRoute() {
  const [path, setPath] = useStateRouter(() => window.location.pathname);
  useEffectRouter(() => {
    const onChange = () => setPath(window.location.pathname);
    window.addEventListener("popstate", onChange);   // back/forward buttons
    window.addEventListener("al-navigate", onChange); // in-app navigate()
    return () => { window.removeEventListener("popstate", onChange); window.removeEventListener("al-navigate", onChange); };
  }, []);
  return { path, ...matchRoute(path) };
}

window.matchRoute = matchRoute;
window.navigate = navigate;
window.useRoute = useRoute;
