import "../../../../../../after-B1IIdH3Y.mjs";
import "../../../../../../object-cache-Bok5j2ae.mjs";
import "../../../../../../base64-CmWvODNW.mjs";
import "../../../../../../types-CuW0zWDA.mjs";
import "../../../../../../comment-CzeatwBV.mjs";
import "../../../../../../options-DTTML-Tx.mjs";
import { a as unwrapResult, i as requireDb, n as apiSuccess, r as handleError, t as apiError } from "../../../../../../error-CkAseSqZ.mjs";
import { n as parseBody, t as isParseError } from "../../../../../../parse-GmjecGct.mjs";
import { vt as commentStatusBody } from "../../../../../../redirects-DXEXBAII.mjs";
import "../../../../../../byline-fields-2Lp5Hbgf.mjs";
import "../../../../../../status-CqJQX1QN.mjs";
import "../../../../../../api/schemas/index.mjs";
import { n as requirePerm } from "../../../../../../authorize-Dz88F_Fw.mjs";
import { a as handleCommentGet } from "../../../../../../comments-BLE-jv5k.mjs";
import { t as getSiteBaseUrl } from "../../../../../../site-url-BBfDTgZB.mjs";
import { i as sendCommentNotification, n as moderateComment, r as lookupContentAuthor } from "../../../../../../service-DQmiBMzt.mjs";

//#region src/astro/routes/api/admin/comments/[id]/status.ts
const prerender = false;
const PUT = async ({ params, request, locals }) => {
	const { emdash, user } = locals;
	const { id } = params;
	if (!id) return apiError("VALIDATION_ERROR", "Comment ID required", 400);
	const dbErr = requireDb(emdash?.db);
	if (dbErr) return dbErr;
	const denied = requirePerm(user, "comments:moderate");
	if (denied) return denied;
	try {
		const body = await parseBody(request, commentStatusBody);
		if (isParseError(body)) return body;
		const newStatus = body.status;
		const hookRunner = {
			async runBeforeCreate(event) {
				return emdash.hooks.runCommentBeforeCreate(event);
			},
			async runModerate(event) {
				const result = await emdash.hooks.invokeExclusiveHook("comment:moderate", event);
				if (!result) return {
					status: "pending",
					reason: "No moderator configured"
				};
				if (result.error) return {
					status: "pending",
					reason: "Moderation error"
				};
				return result.result;
			},
			fireAfterCreate(event) {
				emdash.hooks.runCommentAfterCreate(event).catch((err) => console.error("[comments] afterCreate error:", err instanceof Error ? err.message : err));
			},
			fireAfterModerate(event) {
				emdash.hooks.runCommentAfterModerate(event).catch((err) => console.error("[comments] afterModerate error:", err instanceof Error ? err.message : err));
			}
		};
		const existing = await handleCommentGet(emdash.db, id);
		if (!existing.success) return unwrapResult(existing);
		const previousStatus = existing.data.status;
		const updated = await moderateComment(emdash.db, id, newStatus, {
			id: user.id,
			name: user.name ?? null
		}, hookRunner);
		if (!updated) return apiError("NOT_FOUND", "Comment not found", 404);
		if (newStatus === "approved" && previousStatus !== "approved" && emdash.email) try {
			const adminBaseUrl = await getSiteBaseUrl(emdash.db, request);
			const content = await lookupContentAuthor(emdash.db, updated.collection, updated.contentId);
			if (content?.author) await sendCommentNotification({
				email: emdash.email,
				comment: updated,
				contentAuthor: content.author,
				adminBaseUrl
			});
		} catch (err) {
			console.error("[comments] notification error:", err instanceof Error ? err.message : err);
		}
		return apiSuccess(updated);
	} catch (error) {
		return handleError(error, "Failed to update comment status", "COMMENT_STATUS_ERROR");
	}
};

//#endregion
export { PUT, prerender };
//# sourceMappingURL=status.mjs.map