globalThis.process ??= {};
globalThis.process.env ??= {};
import { r as __exportAll } from "./rolldown-runtime_-UtK6b3T.mjs";
import { T as setSetting, W as ContentValidationError, b as parseDiscussionConfig, d as mapContentError, g as getSetting, m as SETTINGS_KEYS, u as jsonOk, v as parseBlocklistInput } from "./crypto_CY2zBtG3.mjs";
import { n as getFullDb } from "./db_DCN9iNkm.mjs";
import { n as recordAudit, t as clientIp } from "./audit_C_5e8RPb.mjs";
import { c as assertCsrf, i as requireAdmin } from "./auth_B0DWM0G6.mjs";
import { env } from "cloudflare:workers";
//#region src/pages/api/admin/discussion.ts
var discussion_exports = /* @__PURE__ */ __exportAll({
	GET: () => GET,
	POST: () => POST,
	prerender: () => false
});
var DISCUSSION_NESTING_MAX = 10;
var DISCUSSION_BLOCKLIST_MAX = 200;
var DISCUSSION_BLOCKLIST_TERM_MAX = 100;
var GET = async ({ request, cookies }) => {
	try {
		const { actor: _actor } = await requireAdmin(request, env, cookies);
		const { db } = getFullDb(env);
		const raw = await getSetting(db, SETTINGS_KEYS.discussion);
		const config = parseDiscussionConfig(raw);
		return jsonOk({ config });
	} catch (e) {
		return mapContentError(e);
	}
};
var POST = async ({ request, cookies }) => {
	try {
		const { actor, authMode } = await requireAdmin(request, env, cookies);
		await assertCsrf(request, {
			authMode,
			siteOrigin: env.SITE_ORIGIN
		});
		const body = await request.json();
		if (typeof body !== "object" || body === null) throw new ContentValidationError("input", "must be an object");
		if (typeof body.enabled !== "boolean") throw new ContentValidationError("enabled", "must be boolean");
		if (body.whoCanComment !== "everyone" && body.whoCanComment !== "registered") throw new ContentValidationError("whoCanComment", "must be everyone or registered");
		if (typeof body.nestingDepth !== "number" || !Number.isFinite(body.nestingDepth)) throw new ContentValidationError("nestingDepth", "must be a finite number");
		const nestingDepth = Math.round(body.nestingDepth);
		if (nestingDepth < 0 || nestingDepth > DISCUSSION_NESTING_MAX) throw new ContentValidationError("nestingDepth", "must be integer 0–10");
		if (!Array.isArray(body.blocklist)) throw new ContentValidationError("blocklist", "must be array");
		const rawTerms = body.blocklist.filter((t) => typeof t === "string");
		if (rawTerms.length > DISCUSSION_BLOCKLIST_MAX) throw new ContentValidationError("blocklist", "too many terms");
		for (const term of rawTerms) if (term.length > DISCUSSION_BLOCKLIST_TERM_MAX) throw new ContentValidationError("blocklist", "term too long");
		const blocklist = parseBlocklistInput(rawTerms.join("\n"));
		const { db } = getFullDb(env);
		const valueToSave = {
			enabled: body.enabled,
			whoCanComment: body.whoCanComment,
			nestingDepth,
			blocklist
		};
		await setSetting(db, SETTINGS_KEYS.discussion, valueToSave);
		await recordAudit(db, {
			actorId: actor.id,
			ip: clientIp(request)
		}, {
			action: "discussion.update",
			entityType: "settings",
			entityId: "discussion"
		});
		const saved = await getSetting(db, SETTINGS_KEYS.discussion);
		return jsonOk({ config: parseDiscussionConfig(saved) });
	} catch (e) {
		return mapContentError(e);
	}
};
//#endregion
//#region \0virtual:astro:page:src/pages/api/admin/discussion@_@ts
var page = () => discussion_exports;
//#endregion
export { page };
