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

type Props = CollectionEntry<"stories">["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 commissionersList = props.commissioners && (await getEntries(props.commissioners));
const requestersList = props.requesters && (await getEntries(props.requesters));
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);
const wordCount = props.wordCount?.toString();
const metaDescription = t(props.lang, "story/warnings", wordCount, props.contentWarning);
---

<PublishedContentLayout
  publishedContentType="story"
  title={props.title}
  lang={props.lang}
  isDraft={props.isDraft}
  isAgeRestricted={props.isAgeRestricted}
  pubDate={props.pubDate}
  description={props.description}
  summary={props.summary}
  tags={props.tags}
  thumbnail={props.thumbnail}
  thumbnailWidth={props.thumbnailWidth}
  thumbnailHeight={props.thumbnailHeight}
  copyrightedCharacters={props.copyrightedCharacters}
  series={series}
  prev={prev && !prev.data.isDraft
    ? {
        link: `/stories/${prev.slug}`,
        title: t(props.lang, "story/previous_story", prev.data.shortTitle || prev.data.title),
      }
    : undefined}
  next={next && !next.data.isDraft
    ? {
        link: `/stories/${next.slug}`,
        title: t(props.lang, "story/next_story", next.data.shortTitle || next.data.title),
      }
    : undefined}
  relatedStories={relatedStories}
  relatedGames={relatedGames}
  relatedBlogPosts={relatedBlogPosts}
  posts={props.posts}
  labelReturnTo={{ title: t(props.lang, "story/return_to_stories"), link: "/stories" }}
  labelPreviousContent={t(props.lang, "story/previous_story_aria_label")}
  labelNextContent={t(props.lang, "story/next_story_aria_label")}
  labelTitleSection={t(props.lang, "story/title_aria_label")}
  labelInformationSection={t(props.lang, "story/information_aria_label")}
  labelArticleSection={t(props.lang, "story/article_aria_label")}
>
  <Fragment slot="head">
    <meta property="og:description" content={metaDescription} />
    <meta name="description" content={metaDescription} />
  </Fragment>
  <Fragment slot="section-information">
    <Authors lang={props.lang}>
      {authorsList.map((author) => <UserComponent rel="author" class="p-author" user={author} lang={props.lang} />)}
    </Authors>
    {
      requestersList && (
        <Requesters lang={props.lang}>
          {requestersList.map((requester) => (
            <UserComponent user={requester} lang={props.lang} />
          ))}
        </Requesters>
      )
    }
    {
      commissionersList && (
        <Commissioners lang={props.lang}>
          {commissionersList.map((commissioner) => (
            <UserComponent user={commissioner} lang={props.lang} />
          ))}
        </Commissioners>
      )
    }
  </Fragment>
  <div slot="section-content-warning" id="content-warning">
    <p>
      {t(props.lang, "story/warnings", wordCount, props.contentWarning)}
    </p>
  </div>
  <Fragment slot="section-article">
    <Prose>
      <slot />
    </Prose>
  </Fragment>
</PublishedContentLayout>
