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

export const prerender = false;

const all = await getPublishedArticles();
const categories = getCategories(all, await getCategoryNames());
const categoryName = slugToCategory(Astro.params.slug!, categories.map((item) => item.name));
if (!categoryName) return Astro.rewrite("/404");
const filtered = all.filter((item) => item.data.category === categoryName);
const page = Number(Astro.params.page);
if (!Number.isInteger(page) || page < 2 || page > lastPageOf(filtered.length)) return Astro.rewrite("/404");
---
<ArticlesView items={pageSlice(filtered, page)} categories={categories} totalCount={all.length} activeSlug={Astro.params.slug!} activeName={categoryName} currentPage={page} lastPage={lastPageOf(filtered.length)} basePath={`/articles/topic/${Astro.params.slug}`} />
