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 requirePerm, t as requireOwnerPerm } from "../../../../../../authorize-Dz88F_Fw.mjs";

//#region src/astro/routes/api/content/[collection]/[id]/duplicate.ts
const prerender = false;
const POST = async ({ params, locals, cache }) => {
	const { emdash, user } = locals;
	const collection = params.collection;
	const id = params.id;
	const denied = requirePerm(user, "content:create");
	if (denied) return denied;
	if (!emdash?.handleContentDuplicate || !emdash?.handleContentGet) return apiError("NOT_CONFIGURED", "EmDash is not initialized", 500);
	const existing = await emdash.handleContentGet(collection, id);
	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 readDenied = requireOwnerPerm(user, typeof existingItem?.authorId === "string" ? existingItem.authorId : "", "content:edit_own", "content:edit_any");
	if (readDenied) return readDenied;
	const resolvedId = typeof existingItem?.id === "string" ? existingItem.id : id;
	const result = await emdash.handleContentDuplicate(collection, resolvedId, user?.id);
	if (!result.success) return unwrapResult(result);
	if (cache?.enabled) await cache.invalidate({ tags: [collection] });
	return unwrapResult(result, 201);
};

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