export async function load({ params, fetch }) {
	try {
		const continueReadingResponse = await fetch(
			`/api/books?libraries=${params.libraryId}&progress=in_progress&orderBy=last_accessed&sortOrder=desc`
		);
		let results = await continueReadingResponse.json();
		let continueReading = results['items'];

		const discoverResponse = await fetch(
			`/api/books?libraries=${params.libraryId}&progress=unread&sortOrder=random`
		);
		results = await discoverResponse.json();
		let discover = results['items'];

		const readAgainResponse = await fetch(
			`/api/books?libraries=${params.libraryId}&progress=read&sortOrder=random`
		);
		results = await readAgainResponse.json();
		let readAgain = results['items'];

		const recentlyAddedResponse = await fetch(
			`/api/books?libraries=${params.libraryId}&orderBy=create_at&sortOrder=desc&progress=unread`
		);
		results = await recentlyAddedResponse.json();
		let recentlyAdded = results['items'];

		return {
			continueReading,
			discover,
			readAgain,
			recentlyAdded
		};
	} catch (error) {
		console.error('Error fetching books from library: ', error);
		return {
			status: error.status || 500,
			error: error.message
		};
	}
}
