<script setup lang="ts">
	import { storyEditMiddleware } from "@client/middleware";
	import { SingleChapterResult } from "@client/types/slightlyDifferentStory";
	import SingleChapter from "~/components/story/create/singleChapter.vue";
	import { toFormChapter } from "@client/types/form/story";

	definePageMeta({
		middleware: ["auth", storyEditMiddleware],
	});

	const rtr = useRoute();
	const {
		data: { value: originalStory },
	} = await useApiFetch<SingleChapterResult | null>(`/story/${rtr.params.id}/${rtr.params.cidx}`);
	if (originalStory === null) {
		throw createError({
			statusCode: 404,
			message: "That story doesn't exist...",
		});
	}
	useHead({
		title: `Editing chapter: ${originalStory.currentChapter.title}`,
	});
</script>

<template>
	<single-chapter :data="toFormChapter(originalStory!.currentChapter)" :name="originalStory!.currentChapter.title" />
</template>