---
import { type CollectionEntry, getEntry, getEntries } from "astro:content";
import PublishedContentLayout from "./PublishedContentLayout.astro";
import { t } from "@i18n";
import Authors from "@components/Authors.astro";
import UserComponent from "@components/UserComponent.astro";
import Prose from "@components/Prose.astro";
import { Markdown } from "@astropub/md";

type Props = CollectionEntry<"blog">["data"];

const { props } = Astro;
const prev = props.prev && (await getEntry(props.prev));
const next = props.next && (await getEntry(props.next));
const series = props.series && (await getEntry(props.series));
const authorsList = await getEntries(props.authors);
const relatedStories = (await getEntries(props.relatedStories)).filter((story) => !story.data.isDraft);
const relatedGames = (await getEntries(props.relatedGames)).filter((game) => !game.data.isDraft);
const relatedBlogPosts = (await getEntries(props.relatedBlogPosts)).filter((post) => !post.data.isDraft);
---

<PublishedContentLayout
  publishedContentType="blog post"
  title={props.title}
  lang={props.lang}
  isDraft={props.isDraft}
  isAgeRestricted={props.isAgeRestricted}
  pubDate={props.pubDate}
  tags={props.tags}
  thumbnail={props.thumbnail}
  thumbnailWidth={props.thumbnailWidth}
  thumbnailHeight={props.thumbnailHeight}
  series={series}
  prev={prev && !prev.data.isDraft
    ? {
        link: `/blog/${prev.slug}`,
        title: t(props.lang, "blog/previous_post", prev.data.title),
      }
    : undefined}
  next={next && !next.data.isDraft
    ? {
        link: `/blog/${next.slug}`,
        title: t(props.lang, "blog/next_post", next.data.title),
      }
    : undefined}
  relatedStories={relatedStories}
  relatedGames={relatedGames}
  relatedBlogPosts={relatedBlogPosts}
  posts={props.posts}
  labelReturnTo={{ title: t(props.lang, "blog/return_to_blog_posts"), link: "/blog" }}
  labelPreviousContent={t(props.lang, "blog/previous_post_aria_label")}
  labelNextContent={t(props.lang, "blog/next_post_aria_label")}
  labelTitleSection={t(props.lang, "blog/title_aria_label")}
  labelInformationSection={t(props.lang, "blog/information_aria_label")}
  labelArticleSection={t(props.lang, "blog/article_aria_label")}
>
  <Fragment slot="head">
    <meta property="og:description" content={props.description} />
    <meta name="description" content={props.description} />
  </Fragment>
  <Fragment slot="section-information">
    <Authors lang={props.lang}>
      {authorsList.map((author) => <UserComponent rel="author" class="p-author" user={author} lang={props.lang} />)}
    </Authors>
    <section id="description" class="px-2 font-serif" aria-label={t(props.lang, "published_content/description")}>
      <Prose>
        <Markdown of={props.description} />
      </Prose>
    </section>
  </Fragment>
  <Fragment slot="section-article">
    <Prose>
      <slot />
    </Prose>
  </Fragment>
</PublishedContentLayout>
