---
export const prerender = true;

export async function getStaticPaths() {
  try {
    const { getPosts } = await import("../../../lib/cms");
    const { sortPostsByDate, getTotalPages, getPostsPerPage } = await import("../../../lib/blog-utils");
    const posts = await getPosts();
    const sorted = sortPostsByDate(posts);
    const perPage = getPostsPerPage();
    const totalPages = getTotalPages(sorted.length, perPage);
    return Array.from({ length: totalPages - 1 }, (_, i) => ({
      params: { page: String(i + 2) },
    }));
  } catch {
    return [];
  }
}

const { page } = Astro.params as { page: string };
return Astro.redirect(`/posts/page/${encodeURIComponent(page)}`);
---
