---
import Layout from "@layouts/Layout.astro";
import { getCollection } from "astro:content";
import { useTranslatedPath } from "@i18n/utils";

const collections = ["blog", "photos", "projects"];

const tp = useTranslatedPath(Astro);

export async function getStaticPaths() {
const collections = ["blog", "photos", "projects"];
const posts = await Promise.all(
  collections.map((collection) => {
    return getCollection(collection, {
      fields: ["slug", "title", "date", "tags"],
    });
  }),
);

const tags = new Set();

posts.flat().forEach((post) => {
  if (!post.data?.tags) return;
  post.data.tags.forEach((tag) => {
    tags.add(tag.toLowerCase());
  });
});

  return [...tags.values()].map((tag) => {
    return {
      params: {
        tag,
      },
    };
  });
}

const { tag } = Astro.params;


const allPosts = (await Promise.all(
  collections.map((collection) => {
    return getCollection(collection, {
      fields: ["slug", "title", "date", "tags"],
    });
  }),
)).flat();



const posts = allPosts.filter((post) => {
  return post.data?.tags?.find(t => t.toLowerCase() === tag);
});


---

<Layout title="Max Richter">
  <article>
    <h1>Tags</h1>
    {posts.length}
    <div class="flex flex-col gap-2">
      {
        posts.map((post) => {
          return <a href={tp("/"+post.collection+"/" + post.slug)}>{post.slug}</a>;
        })
      }
    </div>
  </article>
</Layout>

