import {
  createRouter,
  createRoute,
  createRootRoute,
  redirect,
} from '@tanstack/react-router';
import { AppShell } from './components/AppShell';
import { FolderView } from './routes/FolderView';
import { SearchView } from './routes/SearchView';

const rootRoute = createRootRoute({ component: AppShell });

const indexRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/',
  beforeLoad: () => { throw redirect({ to: '/f/$folderId', params: { folderId: 'inbox' } }); },
});

const folderRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/f/$folderId',
  component: FolderView,
});

const searchRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/search',
  component: SearchView,
});

const routeTree = rootRoute.addChildren([indexRoute, folderRoute, searchRoute]);

export const router = createRouter({ routeTree });

declare module '@tanstack/react-router' {
  interface Register { router: typeof router }
}
