import { error } from '@sveltejs/kit';
import { createApiClient } from '$lib/shared/api.server';
import type { components } from '$lib/generated/api';

type TagTreeNodeDTO = components['schemas']['TagTreeNodeDTO'];

export async function load({ fetch }: Parameters<import('./$types').PageServerLoad>[0]) {
	const api = createApiClient(fetch);
	const result = await api.GET('/api/tags/tree');
	if (!result.response.ok) throw error(500, 'Themen konnten nicht geladen werden.');
	return { tree: (result.data ?? []) as TagTreeNodeDTO[] };
}
