globalThis.process ??= {};
globalThis.process.env ??= {};
import { r as __exportAll } from "./rolldown-runtime_-UtK6b3T.mjs";
import { A as getById, M as list, P as put, W as ContentValidationError, c as isEntityId, d as mapContentError, u as jsonOk } 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 { a as requireEditor, c as assertCsrf } from "./auth_B0DWM0G6.mjs";
import { t as sanitizeContentBody } from "./sanitize_CLHlWQbC.mjs";
import { n as getContentTranslationStatusesFor, r as markContentTranslationsStale } from "./content-translation_DEN5c2_w.mjs";
import { env } from "cloudflare:workers";
//#region src/pages/api/admin/content.ts
var content_exports = /* @__PURE__ */ __exportAll({
	GET: () => GET,
	POST: () => POST,
	prerender: () => false
});
function parseContentInput(data) {
	if (typeof data !== "object" || data === null) throw new ContentValidationError("input", "must be an object");
	const raw = data;
	if (typeof raw.slug !== "string" || !raw.slug.trim()) throw new ContentValidationError("slug", "required string");
	if (typeof raw.type !== "string" || !raw.type.trim()) throw new ContentValidationError("type", "required string");
	if (typeof raw.title !== "string" || !raw.title.trim()) throw new ContentValidationError("title", "required string");
	if (raw.body !== void 0 && typeof raw.body !== "string") throw new ContentValidationError("body", "must be a string when provided");
	let id;
	if (raw.id !== void 0) {
		if (!isEntityId(raw.id)) throw new ContentValidationError("id", "must be a valid UUID");
		id = raw.id;
	}
	let visibility;
	if (raw.visibility !== void 0) {
		if (raw.visibility !== "public" && raw.visibility !== "private" && raw.visibility !== "members") throw new ContentValidationError("visibility", "must be public|private|members");
		visibility = raw.visibility;
	}
	return {
		...id !== void 0 ? { id } : {},
		slug: raw.slug,
		type: raw.type,
		title: raw.title,
		body: typeof raw.body === "string" ? raw.body : "",
		...visibility !== void 0 ? { visibility } : {}
	};
}
var POST = async ({ request, cookies }) => {
	try {
		const { actor, authMode } = await requireEditor(request, env, cookies);
		assertCsrf(request, {
			authMode,
			siteOrigin: env.SITE_ORIGIN
		});
		const { db } = getFullDb(env);
		const input = parseContentInput(await request.json());
		const previousEntry = input.id ? await getById(db, input.id, actor) : null;
		const entry = await put(db, input, actor, sanitizeContentBody);
		await markContentTranslationsStale(db, previousEntry, entry);
		await recordAudit(db, {
			actorId: actor.id,
			ip: clientIp(request)
		}, {
			action: input.id ? "content.update" : "content.create",
			entityType: "content",
			entityId: entry.id
		});
		return jsonOk(entry);
	} catch (error) {
		return mapContentError(error);
	}
};
var GET = async ({ request, locals, url, cookies }) => {
	try {
		const { actor, authMode } = await requireEditor(request, env, cookies);
		const { db } = getFullDb(env);
		const status = url.searchParams.get("status") ?? void 0;
		const type = url.searchParams.get("type") ?? void 0;
		const entries = await list(db, {
			status,
			type
		}, actor);
		const translationStatuses = await getContentTranslationStatusesFor(db, entries.map((entry) => entry.id));
		return jsonOk({ entries: entries.map((entry) => ({
			...entry,
			translationStatusByLocale: translationStatuses.get(entry.id) ?? {}
		})) });
	} catch (error) {
		return mapContentError(error);
	}
};
//#endregion
//#region \0virtual:astro:page:src/pages/api/admin/content@_@ts
var page = () => content_exports;
//#endregion
export { page };
