import { render } from 'vike/abort';
import { fetchEntityHealths } from '#/src/entities.js';

export async function onBeforeRender(pageContext) {
	if (!pageContext.user || pageContext.user.role === 'user') {
		throw render(404);
	}

	const {
		entities,
	} = await fetchEntityHealths({
		sort: pageContext.urlParsed.search.sort || 'releases',
		order: pageContext.urlParsed.search.order || 'desc',
	}, pageContext.user);

	return {
		pageContext: {
			title: pageContext.routeParams.section,
			pageProps: {
				entities,
			},
			routeParams: {
				section: 'entities',
			},
		},
	};
}
