import { API_URL } from '$env/static/private';

import type { PageServerLoad } from './$types';

import type { Chapter } from '$lib/types';

export const load = (async ({ parent, fetch, cookies }) => {
	await parent();

	const session = cookies.get('session');

	const res = await fetch(`${API_URL}/chapters`, {
		headers: {
			Authorization: `Bearer ${session}`
		}
	});

	if (!res.ok) {
		return {
			chapters: []
		};
	}

	const chapters = (await res.json()) as Chapter[];

	return {
		chapters
	};
}) satisfies PageServerLoad;
