---
import Button from "./Button.astro";
import { type CollectionEntry, getCollection } from "astro:content";

type Props = {
  entry: CollectionEntry<"posts"> | CollectionEntry<"projects">;
};

const { entry } = Astro.props;
const { collection } = entry;

const items = (await getCollection(collection))
  .filter((post) => !post.data.draft)
  .sort((a, b) => b.data.date.getTime() - a.data.date.getTime());
const index = items.findIndex((x) => x.slug === entry.slug);
const prev = items[(index - 1 + items.length) % items.length];
const next = items[(index + 1) % items.length];
---

{
  items.length > 1 ? (
    <div class="mx-auto flex w-full flex-col-reverse items-center justify-between gap-4 md:flex-row">
      <Button
        href={`/${prev.collection}/${prev.slug}`}
        link={`Previous: ${prev.data.title}`}
      />
      <Button
        href={`/${next.collection}/${next.slug}`}
        link={`Next: ${next.data.title}`}
      />
    </div>
  ) : null
}
