---
import ArticlesView from "../../../components/ArticlesView.astro";
import NotFoundPage from "../../../components/NotFoundPage.astro";
import { getPublishedArticles, getCategoryNames, getCategories, pageSlice, lastPageOf } from "../../../lib/articles";

export const prerender = false;

const all = await getPublishedArticles();
const pageValue = String(Astro.params.page || "");
const page = /^\d+$/.test(pageValue) ? Number(pageValue) : 0;
const lastPage = lastPageOf(all.length);
const valid = Number.isSafeInteger(page) && page >= 2 && page <= lastPage;
if (!valid) Astro.response.status = 404;
else Astro.response.headers.set("Cache-Control", "public, max-age=0, must-revalidate");
const categories = valid ? getCategories(all, await getCategoryNames()) : [];
---
{valid
  ? <ArticlesView items={pageSlice(all, page)} {categories} totalCount={all.length} activeSlug={null} activeName={null} currentPage={page} {lastPage} basePath="/articles" />
  : <NotFoundPage />}
