---
import { getCollection } from "astro:content";
import Post from "@layouts/Post.astro";
import { getLocale } from "astro-i18n-aut";
import { filterCollection, parseSlug } from "@i18n/utils";

const locale = getLocale(Astro.url);

export async function getStaticPaths() {
  const pages = await getCollection("blog");

  const paths = pages.map((page) => {
    const [slug] = parseSlug(page.id);
    return { params: { slug }, props: { ...page } };
  });

  return paths;
}

const pages = await getCollection("blog");
const page = filterCollection(pages, locale).find((page) => {
  const [slug] = parseSlug(page.id);
  return slug === Astro.params.slug;
});

if (!page) {
  return new Response("Not found", {
    status: 404,
  });
}

const { Content } = await page.render();
---

<Post {...page.data}>
  <Content />
</Post>
