---
import type { GetStaticPaths } from "astro";
import { type CollectionEntry, getCollection } from "astro:content";
import getReadingTime from "reading-time";
import StoryLayout from "@layouts/StoryLayout.astro";
import { PUBLISH_DRAFTS } from "astro:env/server";

type Props = CollectionEntry<"stories">;

type Params = {
  slug: CollectionEntry<"stories">["slug"];
};

export const getStaticPaths: GetStaticPaths = async () => {
  const stories = await getCollection("stories");
  return stories
    .filter((story) => import.meta.env.DEV || PUBLISH_DRAFTS || !story.data.isDraft)
    .map((story) => ({
      params: { slug: story.slug } satisfies Params,
      props: story satisfies Props,
    }));
};

const story = Astro.props;
const readingTime = getReadingTime(story.body);
if (story.data.wordCount && Math.abs(story.data.wordCount - readingTime.words) >= 150) {
  console.warn(
    `WARNING: "wordCount" differs greatly from actual word count for published story ` +
      `${story.data.title} ("${story.slug}") ` +
      `(expected ~${story.data.wordCount}, found ${readingTime.words})`,
  );
}

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

<StoryLayout {...story.data}>
  <Content />
</StoryLayout>
