import "../../../../../../dialect-helpers-DRI5pyY3.mjs";
import "../../../../../../after-B1IIdH3Y.mjs";
import "../../../../../../object-cache-Bok5j2ae.mjs";
import "../../../../../../base64-CmWvODNW.mjs";
import "../../../../../../types-CuW0zWDA.mjs";
import "../../../../../../taxonomy-BxGxZZdu.mjs";
import "../../../../../../request-cache-KCNHp_RJ.mjs";
import "../../../../../../loader-CB6YHeul.mjs";
import "../../../../../../resolve-DR3wqMTy.mjs";
import "../../../../../../taxonomies-Cnb4F-Pb.mjs";
import { a as handleTermGet, c as handleTermUpdate, i as handleTermDelete } from "../../../../../../taxonomies-BGBd16Eu.mjs";
import { a as unwrapResult, i as requireDb, r as handleError, t as apiError } from "../../../../../../error-CkAseSqZ.mjs";
import { i as parseQuery, n as parseBody, t as isParseError } from "../../../../../../parse-GmjecGct.mjs";
import { Fn as localeFilterQuery, Q as updateTermBody } from "../../../../../../redirects-DXEXBAII.mjs";
import "../../../../../../byline-fields-2Lp5Hbgf.mjs";
import "../../../../../../status-CqJQX1QN.mjs";
import "../../../../../../api/schemas/index.mjs";
import { n as requirePerm } from "../../../../../../authorize-Dz88F_Fw.mjs";

//#region src/astro/routes/api/taxonomies/[name]/terms/[slug].ts
const prerender = false;
/**
* Get a single term
*/
const GET = async ({ params, request, locals }) => {
	const { emdash, user } = locals;
	const { name, slug } = params;
	if (!name || !slug) return apiError("VALIDATION_ERROR", "Taxonomy name and slug required", 400);
	const dbErr = requireDb(emdash?.db);
	if (dbErr) return dbErr;
	const denied = requirePerm(user, "taxonomies:read");
	if (denied) return denied;
	const query = parseQuery(new URL(request.url), localeFilterQuery);
	if (isParseError(query)) return query;
	try {
		return unwrapResult(await handleTermGet(emdash.db, name, slug, { locale: query.locale }));
	} catch (error) {
		return handleError(error, "Failed to get term", "TERM_GET_ERROR");
	}
};
/**
* Update a term
*/
const PUT = async ({ params, request, locals }) => {
	const { emdash, user } = locals;
	const { name, slug } = params;
	if (!name || !slug) return apiError("VALIDATION_ERROR", "Taxonomy name and slug required", 400);
	const dbErr = requireDb(emdash?.db);
	if (dbErr) return dbErr;
	const denied = requirePerm(user, "taxonomies:manage");
	if (denied) return denied;
	const query = parseQuery(new URL(request.url), localeFilterQuery);
	if (isParseError(query)) return query;
	try {
		const body = await parseBody(request, updateTermBody);
		if (isParseError(body)) return body;
		return unwrapResult(await handleTermUpdate(emdash.db, name, slug, body, { locale: query.locale }));
	} catch (error) {
		return handleError(error, "Failed to update term", "TERM_UPDATE_ERROR");
	}
};
/**
* Delete a term
*/
const DELETE = async ({ params, request, locals }) => {
	const { emdash, user } = locals;
	const { name, slug } = params;
	if (!name || !slug) return apiError("VALIDATION_ERROR", "Taxonomy name and slug required", 400);
	const dbErr = requireDb(emdash?.db);
	if (dbErr) return dbErr;
	const denied = requirePerm(user, "taxonomies:manage");
	if (denied) return denied;
	const query = parseQuery(new URL(request.url), localeFilterQuery);
	if (isParseError(query)) return query;
	try {
		return unwrapResult(await handleTermDelete(emdash.db, name, slug, { locale: query.locale }));
	} catch (error) {
		return handleError(error, "Failed to delete term", "TERM_DELETE_ERROR");
	}
};

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