import { API_URL } from '$env/static/private';
import type { Leaderboard } from '$lib/types';

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

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

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

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

	if (!res.ok) {
		return {
			leaderboard: [] as Leaderboard[]
		};
	}

	const leaderboard = (await res.json()) as Leaderboard[];

	return {
		leaderboard
	};
}) satisfies PageServerLoad;
