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

//#region src/astro/routes/api/content/[collection]/[id]/publish.ts
/**
* Publish content - promotes draft to live
*
* POST /_emdash/api/content/{collection}/{id}/publish
*
* Optional JSON body: { publishedAt?: string }
*   publishedAt — ISO 8601 datetime to backdate the publish (e.g. when
*   migrating content). Writing publishedAt requires content:publish_any.
*   Without it, the existing published_at is preserved on re-publish and
*   falls back to the current time on first publish.
*/
const prerender = false;
const POST = async ({ params, request, locals, url, cache }) => {
	const { emdash, user } = locals;
	const collection = params.collection;
	const id = params.id;
	if (!emdash?.handleContentPublish || !emdash?.handleContentGet) return apiError("NOT_CONFIGURED", "EmDash is not initialized", 500);
	const body = await parseOptionalBody(request, contentPublishBody, {});
	if (isParseError(body)) return body;
	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 existingData = existing.data && typeof existing.data === "object" ? existing.data : void 0;
	const existingItem = existingData?.item && typeof existingData.item === "object" ? existingData.item : existingData;
	const denied = requireOwnerPerm(user, typeof existingItem?.authorId === "string" ? existingItem.authorId : "", "content:publish_own", "content:publish_any");
	if (denied) return denied;
	const publishedAt = body?.publishedAt;
	if (publishedAt !== void 0 && !hasPermission(user, "content:publish_any")) return apiError("FORBIDDEN", "Setting publishedAt requires content:publish_any permission", 403);
	const resolvedId = typeof existingItem?.id === "string" ? existingItem.id : id;
	const result = await emdash.handleContentPublish(collection, resolvedId, { publishedAt });
	if (!result.success) return unwrapResult(result);
	if (cache?.enabled) await cache.invalidate({ tags: [collection, resolvedId] });
	return unwrapResult(result);
};

//#endregion
export { POST, prerender };
//# sourceMappingURL=publish.mjs.map