---
import '../../styles/normalize.css';
import DefaultHome from '../../components/DefaultHome.astro';
import YarHome from '../../components/themes/yar/YarHome.astro';
import { site } from '../../lib/siteConfig';
import { getPaginatedPosts, getPublishedPosts, getTotalPages } from '../../lib/posts';

export function getStaticPaths() {
  const allPosts = getPublishedPosts();
  const totalPages = getTotalPages(allPosts);

  return Array.from({ length: Math.max(0, totalPages - 1) }, (_, index) => {
    const page = index + 2;
    return {
      params: { page: String(page) },
      props: { page }
    };
  });
}

const { page } = Astro.props;
const allPosts = getPublishedPosts();
const posts = getPaginatedPosts(allPosts, page);
const totalPages = getTotalPages(allPosts);
const theme = site.theme?.trim().toLowerCase() ?? 'default';
const Home = theme === 'yar' ? YarHome : DefaultHome;
---

<!doctype html>
<html lang={site.language}>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>第 {page} 页 | {site.title}</title>
    <meta name="description" content={site.description} />
  </head>
  <body>
    <Home posts={posts} currentPage={page} totalPages={totalPages} />
  </body>
</html>
