import { createRoot } from "react-dom/client";
import { createInertiaApp } from "@inertiajs/react";
import { csrfHeaders } from "litestar-vite-plugin/helpers";
import { resolvePageComponent } from "litestar-vite-plugin/inertia-helpers";
import type { ComponentType } from "react";
import "./App.css";

const pages = import.meta.glob("./pages/**/*.tsx") as Record<string, () => Promise<{ default: ComponentType }>>;

createInertiaApp({
  // litestar-vite defaults to the script-element bootstrap for Inertia.
  // - Inertia v3 reads the #app_page payload automatically.
  // - If you pin Inertia v2, add defaults: { future: { useScriptElementForInitialPage: true } }
  //   or set use_script_element=False on the Python side.
  // Litestar's default CSRF middleware uses x-csrftoken.
  // The generated app forwards the injected token on every Inertia visit,
  // which keeps cookie_httponly=True compatible with Inertia's client requests.
  resolve: async (name) => (await resolvePageComponent(`./pages/${name}.tsx`, pages)).default,
  defaults: {
    visitOptions: (_href, options) => ({
      headers: csrfHeaders(options.headers ?? {}),
    }),
  },
  setup({ el, App, props }) {
    createRoot(el).render(<App {...props} />);
  },
});
