// Build-time render entry. Compiled by `vite build --ssr` into a Node-importable
// module; called once per static route by scripts/prerender.mjs. This is NOT a
// runtime server: it runs only during `npm run build`.
//
// The rendered tree MUST mirror main.tsx exactly (StrictMode > [App, Toaster]),
// so the client hydrates markup that matches. The only difference is App's
// children slot: here we inject StaticRouter+AppRoutes; on the client App
// defaults to <Router/> (BrowserRouter+AppRoutes). Router wrappers emit no DOM,
// so the hydrated DOM is identical.
import "./lang";
import { StrictMode } from "react";
import { renderToString } from "react-dom/server";
import { StaticRouter } from "react-router";
import { Toaster } from "@/components/ui/sonner";
import App from "@/App";
import { AppRoutes } from "@/router";
import { resetSsrMeta, renderSsrHeadTags } from "@/lib/ssr-meta";

export async function render(url: string): Promise<{ appHtml: string; headTags: string }> {
  // Per-route meta is collected synchronously during render (usePageTitle feeds
  // the collector when there is no document). Reset before each route so titles
  // never leak across routes.
  resetSsrMeta();
  const appHtml = renderToString(
    <StrictMode>
      <App>
        <StaticRouter location={url}>
          <AppRoutes />
        </StaticRouter>
      </App>
      <Toaster />
    </StrictMode>,
  );
  return { appHtml, headTags: renderSsrHeadTags() };
}
