---
import config from "@/config/config.json";
import Authors from "@/layouts/Authors.astro";
import Base from "@/layouts/Base.astro";
import Pagination from "@/layouts/components/Pagination.astro";
import { getSinglePage } from "@/lib/contentParser.astro";
import { sortByDate } from "@/lib/utils/sortFunctions";
import { markdownify } from "@/lib/utils/textConverter";

export async function getStaticPaths() {
  const authors = await getSinglePage("authors");
  const totalPages = Math.ceil(authors.length / config.settings.pagination);
  const paths = [];

  for (let i = 1; i < totalPages; i++) {
    paths.push({
      params: {
        slug: (i + 1).toString(),
      },
    });
  }
  return paths;
}

const { slug } = Astro.params;
const authors = await getSinglePage("authors");
const sortedPosts = sortByDate(authors);
const totalPages = Math.ceil(authors.length / config.settings.pagination);
const currentPage = slug && !isNaN(Number(slug)) ? Number(slug) : 1;
const indexOfLastPost = currentPage * config.settings.pagination;
const indexOfFirstPost = indexOfLastPost - config.settings.pagination;
const currentPosts = sortedPosts.slice(indexOfFirstPost, indexOfLastPost);
---

<Base title={"Authors"}>
  <section class="section">
    <div class="container text-center">
      <h1 set:text={markdownify("Authors")} class="page-heading h2" />
      <Authors authors={currentPosts} />
      <Pagination
        section={"authors"}
        currentPage={currentPage}
        totalPages={totalPages}
      />
    </div>
  </section>
</Base>
