import { error } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';

export const load: PageServerLoad = async ({ locals, params }) => {
	const { container } = locals;
	const store = container.createLabelLoadedStore();

	const { trigger: loadLabel } = store;

	const numericId = Number(params.id);
	if (isNaN(numericId)) {
		throw error(400, { message: 'Invalid label ID' });
	}

	const state = await loadLabel(numericId);
	if (!state.data) {
		error(404, { message: 'Post not found' });
	}

	return {
		dehydratedData: state.data.dehydrate(),
	};
};
