import "../../../../../../dialect-helpers-DRI5pyY3.mjs";
import "../../../../../../after-B1IIdH3Y.mjs";
import "../../../../../../object-cache-Bok5j2ae.mjs";
import "../../../../../../base64-CmWvODNW.mjs";
import "../../../../../../types-CuW0zWDA.mjs";
import "../../../../../../request-cache-KCNHp_RJ.mjs";
import "../../../../../../byline-registry-BOjqDOim.mjs";
import "../../../../../../field-defs-cache-WUI88LX5.mjs";
import { t as BylineRepository } from "../../../../../../byline-Bm8ZgUP9.mjs";
import "../../../../../../loader-CB6YHeul.mjs";
import "../../../../../../resolve-DR3wqMTy.mjs";
import { a as unwrapResult, 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 { Tn as bylineUpdateBody } from "../../../../../../redirects-DXEXBAII.mjs";
import "../../../../../../byline-fields-2Lp5Hbgf.mjs";
import "../../../../../../status-CqJQX1QN.mjs";
import "../../../../../../api/schemas/index.mjs";
import { a as invalidateBylineCache } from "../../../../../../bylines-Woq87129.mjs";
import { n as requirePerm } from "../../../../../../authorize-Dz88F_Fw.mjs";
import { i as handleBylineUpdate } from "../../../../../../bylines-6tm9mgQb.mjs";

//#region src/astro/routes/api/admin/bylines/[id]/index.ts
const prerender = false;
const GET = async ({ params, locals }) => {
	const { emdash, user } = locals;
	const denied = requirePerm(user, "bylines:read");
	if (denied) return denied;
	if (!emdash?.db) return apiError("NOT_CONFIGURED", "EmDash is not initialized", 500);
	try {
		const byline = await new BylineRepository(emdash.db).findById(params.id);
		if (!byline) return apiError("NOT_FOUND", "Byline not found", 404);
		return apiSuccess(byline);
	} catch (error) {
		return handleError(error, "Failed to get byline", "BYLINE_GET_ERROR");
	}
};
const PUT = async ({ params, request, locals }) => {
	const { emdash, user } = locals;
	const denied = requirePerm(user, "bylines:manage");
	if (denied) return denied;
	const dbErr = requireDb(emdash?.db);
	if (dbErr) return dbErr;
	const body = await parseBody(request, bylineUpdateBody);
	if (isParseError(body)) return body;
	const result = await handleBylineUpdate(emdash.db, params.id, {
		slug: body.slug,
		displayName: body.displayName,
		bio: body.bio ?? null,
		avatarMediaId: body.avatarMediaId ?? null,
		websiteUrl: body.websiteUrl ?? null,
		userId: body.userId ?? null,
		isGuest: body.isGuest,
		customFields: body.customFields
	});
	if (result.success) /* @__PURE__ */ invalidateBylineCache();
	return unwrapResult(result);
};
const DELETE = async ({ params, locals }) => {
	const { emdash, user } = locals;
	const denied = requirePerm(user, "bylines:manage");
	if (denied) return denied;
	if (!emdash?.db) return apiError("NOT_CONFIGURED", "EmDash is not initialized", 500);
	try {
		if (!await new BylineRepository(emdash.db).delete(params.id)) return apiError("NOT_FOUND", "Byline not found", 404);
		/* @__PURE__ */ invalidateBylineCache();
		return apiSuccess({ deleted: true });
	} catch (error) {
		return handleError(error, "Failed to delete byline", "BYLINE_DELETE_ERROR");
	}
};

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