// app.tsx
import { routes } from 'beaver:routes';
import { matchRoutes } from 'react-router-dom';
import { PageData } from 'types';
import { Layout } from '../theme';
import siteData from 'beaver:site-data';

export async function initPageData(routePath: string): Promise<PageData> {
  const matched = matchRoutes(routes, routePath);

  if (matched) {
    const moduleInfo = await matched[0].route.preload();

    return {
      pageType: moduleInfo.frontmatter?.pageType ?? 'doc',
      siteData,
      frontmatter: moduleInfo.frontmatter,
      pagePath: routePath,
      toc: moduleInfo.toc,
      title: moduleInfo.title
    };
  }
  return {
    pageType: '404',
    siteData,
    pagePath: routePath,
    frontmatter: {},
    title: '404'
  };
}

export function App() {
  return <Layout />;
}
