---
import { SITE, BLOG } from '~/config.mjs';

import Layout from '~/layouts/PageLayout.astro';
import BlogList from '~/components/blog/List.astro';
import Title from '~/components/blog/Title.astro';
import Pagination from '~/components/common/Pagination.astro';

import { fetchPosts } from '~/utils/blog';
import { CATEGORY_BASE } from '~/utils/permalinks';

export async function getStaticPaths({ paginate }) {
	if (BLOG?.disabled || BLOG?.category?.disabled) return [];

	const posts = await fetchPosts();
	const categories = new Set();
	posts.map((post) => {
		typeof post.category === 'string' && categories.add(post.category.toLowerCase());
	});

	return Array.from(categories).map((category: string) =>
		paginate(
			posts.filter((post) => typeof post.category === 'string' && category === post.category.toLowerCase()),
			{
				params: { category: category, blog: CATEGORY_BASE || undefined },
				pageSize: BLOG.postsPerPage,
				props: { category },
			}
		)
	);
}

const { page, category } = Astro.props;

const currentPage = page.currentPage ?? 1;
const meta = {
	title: `Category'${category}' ${currentPage > 1 ? ` — Page ${currentPage}` : ''}`,
	description: SITE.description,
	noindex: BLOG?.category?.noindex,
};
---

<Layout meta={meta}>
	<section class="px-6 sm:px-6 py-12 sm:py-16 lg:py-20 mx-auto max-w-3xl">
		<Title>Category {category}</Title>
		<BlogList posts={page.data} />
		<Pagination prevUrl={page.url.prev} nextUrl={page.url.next} />
	</section>
</Layout>
