import createServer from "@inertiajs/react/server";
import { createInertiaApp } from "@inertiajs/react";
import { renderToString } from "react-dom/server";

import { resolvePageComponent } from "litestar-vite-plugin/inertia-helpers";

createServer(
  async (page) => {
    const inertia = await createInertiaApp({
      page,
      render: renderToString,
      resolve: (name) => resolvePageComponent(`./pages/${name}.tsx`, import.meta.glob("./pages/**/*.tsx")),
      setup: ({ App, props }) => <App {...props} />,
    });

    return inertia;
  },
  Number.parseInt(process.env.INERTIA_SSR_PORT || "13714"),
);
