---
import { getCollection } from "astro:content";
import PostLayout from "../../layouts/PostLayout.astro";
import * as collections from "../../content/config";

export async function getStaticPaths() {
  const categories = Object.keys(collections.collections).filter(
    (c) => c !== "git" && c !== "gists",
  );

  const entries = [];
  for (const category of categories) {
    const docs = await getCollection(category);
    for (const doc of docs) {
      entries.push({
        params: { category, slug: doc.slug },
        props: { post: doc },
      });
    }
  }
  return entries;
}

const { post } = Astro.props;
const category = Astro.params.category;
const { Content } = await post.render();
const pageTitle = `${category}/${post.data.title ?? post.slug}`;

console.log(Astro.props);
if (post.data?.redirect) {
  return Astro.redirect(post.data.redirect, 301);
}
---

<PostLayout frontmatter={post.data} post={post}>
  <Fragment slot="head">
    <title>{pageTitle}</title>
    <script type="module" src="/scripts/index.js"></script>
  </Fragment>
  <Content />
</PostLayout>
