/* @refresh reload */
import "solid-devtools";
import "./styles/index.css";

import { Route, Router } from "@solidjs/router";
import { render } from "solid-js/web";
import App from "./app";
import NotFound from "./errors/404";
import RepoBlob, { preloadRepoBlob } from "./routes/repo/blob";
import { RepoProvider } from "./routes/repo/context";
import RepoCommit, { preloadRepoCommit } from "./routes/repo/diff/commit";
import RepoCompare, { preloadRepoCompare } from "./routes/repo/diff/compare";
import RepoTree, { preloadRepoTree } from "./routes/repo/tree";
import User from "./routes/user";

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

if (!(root instanceof HTMLElement)) {
	throw new Error("Root element not found.");
}

render(
	() => (
		<Router root={(props) => <App>{props.children}</App>}>
			<Route path="/:user/:repo" component={RepoProvider}>
				<Route
					path="/tree/:ref/*path"
					component={RepoTree}
					preload={preloadRepoTree}
				/>
				<Route
					path="/blob/:ref/*path"
					component={RepoBlob}
					preload={preloadRepoBlob}
				/>
				<Route
					path="/commit/:ref"
					component={RepoCommit}
					preload={preloadRepoCommit}
				/>
				<Route
					path="/compare/:base/:compare"
					component={RepoCompare}
					preload={preloadRepoCompare}
				/>
				<Route component={RepoTree} preload={preloadRepoTree} />
			</Route>
			<Route path="/:user" component={User} />
			<Route path="*" component={NotFound} />
		</Router>
	),
	root,
);
