import "../../../../../../../dialect-helpers-DRI5pyY3.mjs";
import "../../../../../../../after-B1IIdH3Y.mjs";
import "../../../../../../../object-cache-Bok5j2ae.mjs";
import { t as ContentRepository } from "../../../../../../../content-DhgEth4k.mjs";
import "../../../../../../../base64-CmWvODNW.mjs";
import "../../../../../../../types-CuW0zWDA.mjs";
import { t as TaxonomyRepository } from "../../../../../../../taxonomy-BxGxZZdu.mjs";
import "../../../../../../../request-cache-KCNHp_RJ.mjs";
import "../../../../../../../loader-CB6YHeul.mjs";
import "../../../../../../../resolve-DR3wqMTy.mjs";
import { u as invalidateTermCache } from "../../../../../../../taxonomies-Cnb4F-Pb.mjs";
import { 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 { fn as contentTermsBody } 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";

//#region src/astro/routes/api/content/[collection]/[id]/terms/[taxonomy].ts
const prerender = false;
/**
* Get terms assigned to an entry
*/
const GET = async ({ params, locals }) => {
	const { emdash, user } = locals;
	const { collection, id, taxonomy } = params;
	const denied = requirePerm(user, "content:read");
	if (denied) return denied;
	if (!collection || !id || !taxonomy) return apiError("VALIDATION_ERROR", "Collection, id, and taxonomy required", 400);
	const dbErr = requireDb(emdash?.db);
	if (dbErr) return dbErr;
	try {
		const entry = await new ContentRepository(emdash.db).findByIdOrSlug(collection, id);
		if (!entry) return apiError("NOT_FOUND", "Content not found", 404);
		const locale = entry.locale ?? void 0;
		return apiSuccess({ terms: (await new TaxonomyRepository(emdash.db).getTermsForEntry(collection, entry.id, taxonomy, locale)).map((t) => ({
			id: t.id,
			name: t.name,
			slug: t.slug,
			label: t.label,
			parentId: t.parentId
		})) });
	} catch (error) {
		return handleError(error, "Failed to get entry terms", "TERMS_GET_ERROR");
	}
};
/**
* Set terms for an entry (replaces existing)
*/
const POST = async ({ params, request, locals }) => {
	const { emdash, user } = locals;
	const { collection, id, taxonomy } = params;
	if (!collection || !id || !taxonomy) return apiError("VALIDATION_ERROR", "Collection, id, and taxonomy required", 400);
	const denied = requirePerm(user, "content:edit_own");
	if (denied) return denied;
	const dbErr = requireDb(emdash?.db);
	if (dbErr) return dbErr;
	if (!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 ?? "NOT_FOUND", existing.error?.message ?? "Content not found", existing.error?.code === "NOT_FOUND" ? 404 : 500);
	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;
	const canonicalId = typeof existingItem?.id === "string" ? existingItem.id : id;
	const entryLocale = typeof existingItem?.locale === "string" ? existingItem.locale : void 0;
	try {
		const body = await parseBody(request, contentTermsBody);
		if (isParseError(body)) return body;
		const { termIds } = body;
		const repo = new TaxonomyRepository(emdash.db);
		for (const termId of termIds) {
			const term = await repo.findById(termId);
			if (!term) return apiError("NOT_FOUND", `Term ID '${termId}' not found`, 404);
			if (term.name !== taxonomy) return apiError("VALIDATION_ERROR", `Term ID '${termId}' does not belong to taxonomy '${taxonomy}'`, 400);
		}
		await repo.setTermsForEntry(collection, canonicalId, taxonomy, termIds);
		invalidateTermCache();
		return apiSuccess({ terms: (await repo.getTermsForEntry(collection, canonicalId, taxonomy, entryLocale)).map((t) => ({
			id: t.id,
			name: t.name,
			slug: t.slug,
			label: t.label,
			parentId: t.parentId
		})) });
	} catch (error) {
		return handleError(error, "Failed to set entry terms", "TERMS_SET_ERROR");
	}
};

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