import "../../../../../base64-CmWvODNW.mjs";
import "../../../../../types-CuW0zWDA.mjs";
import { a as unwrapResult, o as mapErrorStatus, t as apiError } from "../../../../../error-CkAseSqZ.mjs";
import { n as parseBody, t as isParseError } from "../../../../../parse-GmjecGct.mjs";
import { gn as contentUpdateBody } from "../../../../../redirects-DXEXBAII.mjs";
import "../../../../../byline-fields-2Lp5Hbgf.mjs";
import "../../../../../status-CqJQX1QN.mjs";
import "../../../../../api/schemas/index.mjs";
import { n as requirePerm, t as requireOwnerPerm } from "../../../../../authorize-Dz88F_Fw.mjs";
import { hasPermission } from "@emdash-cms/auth";

//#region src/astro/routes/api/content/[collection]/[id].ts
/**
* Single content item endpoints - injected by EmDash integration
*
* GET    /_emdash/api/content/{collection}/{id} - Get content
* PUT    /_emdash/api/content/{collection}/{id} - Update content
* DELETE /_emdash/api/content/{collection}/{id} - Delete content
*/
const prerender = false;
const GET = async ({ params, url, locals }) => {
	const { emdash, user } = locals;
	const denied = requirePerm(user, "content:read");
	if (denied) return denied;
	const collection = params.collection;
	const id = params.id;
	const locale = url.searchParams.get("locale") || void 0;
	if (!emdash?.handleContentGet) return apiError("NOT_CONFIGURED", "EmDash is not initialized", 500);
	const result = await emdash.handleContentGet(collection, id, locale);
	if (result.success && !hasPermission(user, "content:read_drafts")) {
		const data = result.data && typeof result.data === "object" ? result.data : void 0;
		const item = data?.item && typeof data.item === "object" ? data.item : void 0;
		if ((typeof item?.status === "string" ? item.status : null) !== "published") return apiError("NOT_FOUND", `Content item not found: ${id}`, 404);
		if (item) {
			if (item.liveData && typeof item.liveData === "object") item.data = item.liveData;
			delete item.liveData;
			delete item.draftRevisionId;
		}
	}
	return unwrapResult(result);
};
const PUT = async ({ params, request, locals, cache }) => {
	const { emdash, user } = locals;
	const collection = params.collection;
	const id = params.id;
	const locale = new URL(request.url).searchParams.get("locale") || void 0;
	const body = await parseBody(request, contentUpdateBody);
	if (isParseError(body)) return body;
	if (!emdash?.handleContentUpdate || !emdash?.handleContentGet) return apiError("NOT_CONFIGURED", "EmDash is not initialized", 500);
	const existing = await emdash.handleContentGet(collection, id, locale);
	if (!existing.success) return apiError(existing.error?.code ?? "UNKNOWN_ERROR", existing.error?.message ?? "Unknown error", mapErrorStatus(existing.error?.code));
	const existingData = existing.data && typeof existing.data === "object" ? existing.data : void 0;
	const existingItem = existingData?.item && typeof existingData.item === "object" ? existingData.item : existingData;
	const editDenied = requireOwnerPerm(user, typeof existingItem?.authorId === "string" ? existingItem.authorId : "", "content:edit_own", "content:edit_any");
	if (editDenied) return editDenied;
	if (body.publishedAt !== void 0 && !hasPermission(user, "content:publish_any")) return apiError("FORBIDDEN", "Writing publishedAt requires content:publish_any permission", 403);
	const resolvedId = typeof existingItem?.id === "string" ? existingItem.id : id;
	const updateBody = body.authorId !== void 0 && user && hasPermission(user, "content:edit_any") ? body : {
		...body,
		authorId: void 0
	};
	const result = await emdash.handleContentUpdate(collection, resolvedId, {
		...updateBody,
		locale,
		_rev: body._rev
	});
	if (!result.success) return unwrapResult(result);
	if (cache?.enabled) await cache.invalidate({ tags: [collection, resolvedId] });
	return unwrapResult(result);
};
const DELETE = async ({ params, locals, url, cache }) => {
	const { emdash, user } = locals;
	const collection = params.collection;
	const id = params.id;
	if (!emdash?.handleContentDelete || !emdash?.handleContentGet) return apiError("NOT_CONFIGURED", "EmDash is not initialized", 500);
	const locale = url.searchParams.get("locale") || void 0;
	const existing = await emdash.handleContentGet(collection, id, locale);
	if (!existing.success) return apiError(existing.error?.code ?? "UNKNOWN_ERROR", existing.error?.message ?? "Unknown error", mapErrorStatus(existing.error?.code));
	const deleteData = existing.data && typeof existing.data === "object" ? existing.data : void 0;
	const deleteItem = deleteData?.item && typeof deleteData.item === "object" ? deleteData.item : deleteData;
	const deleteDenied = requireOwnerPerm(user, typeof deleteItem?.authorId === "string" ? deleteItem.authorId : "", "content:delete_own", "content:delete_any");
	if (deleteDenied) return deleteDenied;
	const resolvedId = typeof deleteItem?.id === "string" ? deleteItem.id : id;
	const result = await emdash.handleContentDelete(collection, resolvedId);
	if (!result.success) return unwrapResult(result);
	if (cache?.enabled) await cache.invalidate({ tags: [collection, resolvedId] });
	return unwrapResult(result);
};

//#endregion
export { DELETE, GET, PUT, prerender };
//# sourceMappingURL=_id_.mjs.map