---
import type { GetStaticPaths } from "astro";
import { getCollection } from "astro:content";
import Layout from "@/layouts/Layout.astro";
import Header from "@/components/Header.astro";
import Breadcrumb from "@/components/Breadcrumb.astro";
import Main from "@/components/Main.astro";
import Card from "@/components/Card.astro";
import Footer from "@/components/Footer.astro";
import Pagination from "@/components/Pagination.astro";
import { getSortedPosts } from "@/utils/getSortedPosts";
import { useTranslations } from "@/i18n";
import config from "@/config";

export const getStaticPaths = (async ({ paginate }) => {
  const posts = await getCollection("posts", ({ data }) => !data.draft);
  return paginate(getSortedPosts(posts), { pageSize: config.posts.perPage });
}) satisfies GetStaticPaths;

const { page } = Astro.props;

const locale = Astro.currentLocale ?? config.site.lang;
const t = useTranslations(locale);
---

<Layout title={`${t.pages.postsTitle} | ${config.site.title}`}>
  <Header />

  <Breadcrumb />

  <Main pageTitle={t.pages.postsTitle} pageDesc={t.pages.postsDesc}>
    <ul>
      {page.data.map(data => <Card {...data} />)}
    </ul>
  </Main>

  <Pagination {page} />

  <Footer noMarginTop={page.lastPage > 1} />
</Layout>
