import { render } from 'vike/abort';

import { fetchActorRevisions } from '#/src/actors.js';
import verifyAbility from '#/utils/verify-ability.js';

export async function onBeforeRender(pageContext) {
	if (!pageContext.user || !verifyAbility(pageContext.user, 'actor', 'update')) {
		throw render(404);
	}

	const {
		revisions,
		avatars,
	} = await fetchActorRevisions(null, {
		isFinalized: false,
		limit: 50,
	}, pageContext.user);

	return {
		pageContext: {
			title: pageContext.routeParams.section,
			pageProps: {
				revisions,
				avatars,
			},
		},
	};
}
