globalThis.process ??= {};
globalThis.process.env ??= {};
import { r as __exportAll } from "./rolldown-runtime_-UtK6b3T.mjs";
import { d as mapContentError, l as jsonError, u as jsonOk } from "./crypto_CY2zBtG3.mjs";
import { C as setStatus, S as remove, b as listForModeration } from "./db_DZJ1iBSP.mjs";
import { f as requireRole } from "./engine-custom_BJVTPCgT.mjs";
import { i as buildServiceTokenResolver, r as buildAuthEngine, t as ROLE_HIERARCHY } from "./admin-engine_Cqzo5vQB.mjs";
import { c as assertCsrf, s as resolveSession } from "./auth_B0DWM0G6.mjs";
import { a as moderatorActorFromPrincipal, i as mapCommentError, n as getCommentsDb } from "./comments_D1h5giZi.mjs";
import { env } from "cloudflare:workers";
//#region src/pages/api/admin/comments.ts
var comments_exports = /* @__PURE__ */ __exportAll({
	GET: () => GET,
	POST: () => POST,
	prerender: () => false
});
var MODERATION_STATUSES = [
	"pending",
	"spam",
	"published"
];
function parseStatus(raw) {
	if (!raw) return void 0;
	return MODERATION_STATUSES.includes(raw) ? raw : void 0;
}
var GET = async ({ request, cookies, url }) => {
	try {
		const { principal } = await resolveSession(request, buildAuthEngine(env), cookies, buildServiceTokenResolver(env));
		requireRole("admin", ROLE_HIERARCHY)(principal);
		const actor = moderatorActorFromPrincipal(principal);
		const db = getCommentsDb(env);
		const status = parseStatus(url.searchParams.get("status"));
		const cursor = url.searchParams.get("cursor") ?? void 0;
		const result = await listForModeration(db, {
			...status ? { status } : {},
			...cursor ? { cursor } : {}
		}, actor);
		return jsonOk(result);
	} catch (error) {
		const mapped = mapCommentError(error);
		if (mapped.status !== 500) return mapped;
		return mapContentError(error);
	}
};
function parseActionBody(data) {
	if (typeof data !== "object" || data === null) throw new Error("validation");
	const raw = data;
	const action = raw.action;
	const id = raw.id;
	if (action !== "approve" && action !== "spam" && action !== "trash" && action !== "delete") throw new Error("validation");
	if (typeof id !== "string" || !id.trim()) throw new Error("validation");
	return {
		action,
		id: id.trim()
	};
}
var POST = async ({ request, cookies }) => {
	try {
		const { principal, authMode } = await resolveSession(request, buildAuthEngine(env), cookies, buildServiceTokenResolver(env));
		requireRole("admin", ROLE_HIERARCHY)(principal);
		assertCsrf(request, {
			authMode,
			siteOrigin: env.SITE_ORIGIN
		});
		const actor = moderatorActorFromPrincipal(principal);
		const db = getCommentsDb(env);
		let body;
		try {
			body = await request.json();
		} catch {
			return jsonError(400, "validation_error", "Invalid request body.");
		}
		const { action, id } = parseActionBody(body);
		if (action === "approve") await setStatus(db, id, "published", actor);
		else if (action === "spam") await setStatus(db, id, "spam", actor);
		else if (action === "trash") await setStatus(db, id, "trashed", actor);
		else await remove(db, id, actor);
		return jsonOk({
			ok: true,
			id
		});
	} catch (error) {
		if (error.message === "validation") return jsonError(400, "validation_error", "Invalid moderation action.");
		const mapped = mapCommentError(error);
		if (mapped.status !== 500) return mapped;
		return mapContentError(error);
	}
};
//#endregion
//#region \0virtual:astro:page:src/pages/api/admin/comments@_@ts
var page = () => comments_exports;
//#endregion
export { page };
