import { render } from 'vike/abort';

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

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

	const { actors } = await fetchActors(curateActorsQuery(pageContext.urlParsed.search), {
		limit: 100,
		order: pageContext.urlParsed.search.order?.split('.') || ['likes', 'desc'],
	}, pageContext.user);

	return {
		pageContext: {
			title: 'Actors',
			pageProps: {
				actors,
			},
		},
	};
}
