import "../../../../dialect-helpers-DRI5pyY3.mjs";
import "../../../../base64-CmWvODNW.mjs";
import "../../../../types-CuW0zWDA.mjs";
import "../../../../options-DTTML-Tx.mjs";
import "../../../../redirect-IyfNirgS.mjs";
import { r as invalidateRedirectCache } from "../../../../cache-CEBAZjir.mjs";
import { a as unwrapResult, i as requireDb, r as handleError, t as apiError } from "../../../../error-CkAseSqZ.mjs";
import { n as parseBody, t as isParseError } from "../../../../parse-GmjecGct.mjs";
import { f as updateRedirectBody } 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";
import { l as handleRedirectUpdate, o as handleRedirectDelete, s as handleRedirectGet } from "../../../../redirects-Bs1g42Ri.mjs";

//#region src/astro/routes/api/redirects/[id].ts
const prerender = false;
const GET = async ({ params, locals }) => {
	const { emdash, user } = locals;
	const dbErr = requireDb(emdash?.db);
	if (dbErr) return dbErr;
	const db = emdash.db;
	const { id } = params;
	const denied = requirePerm(user, "redirects:read");
	if (denied) return denied;
	if (!id) return apiError("VALIDATION_ERROR", "id is required", 400);
	try {
		return unwrapResult(await handleRedirectGet(db, id));
	} catch (error) {
		return handleError(error, "Failed to fetch redirect", "REDIRECT_GET_ERROR");
	}
};
const PUT = async ({ params, request, locals }) => {
	const { emdash, user } = locals;
	const dbErr = requireDb(emdash?.db);
	if (dbErr) return dbErr;
	const db = emdash.db;
	const { id } = params;
	const denied = requirePerm(user, "redirects:manage");
	if (denied) return denied;
	if (!id) return apiError("VALIDATION_ERROR", "id is required", 400);
	try {
		const body = await parseBody(request, updateRedirectBody);
		if (isParseError(body)) return body;
		const result = await handleRedirectUpdate(db, id, body);
		invalidateRedirectCache();
		return unwrapResult(result);
	} catch (error) {
		return handleError(error, "Failed to update redirect", "REDIRECT_UPDATE_ERROR");
	}
};
const DELETE = async ({ params, locals }) => {
	const { emdash, user } = locals;
	const dbErr = requireDb(emdash?.db);
	if (dbErr) return dbErr;
	const db = emdash.db;
	const { id } = params;
	const denied = requirePerm(user, "redirects:manage");
	if (denied) return denied;
	if (!id) return apiError("VALIDATION_ERROR", "id is required", 400);
	try {
		const result = await handleRedirectDelete(db, id);
		invalidateRedirectCache();
		return unwrapResult(result);
	} catch (error) {
		return handleError(error, "Failed to delete redirect", "REDIRECT_DELETE_ERROR");
	}
};

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