import "../../../../../dialect-helpers-DRI5pyY3.mjs";
import { n as getI18nConfig } from "../../../../../config-CVssduLe.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, n as apiSuccess, 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 { En as bylinesListQuery, yn as bylineCreateBody } 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 { n as handleBylineCreate } from "../../../../../bylines-6tm9mgQb.mjs";

//#region src/astro/routes/api/admin/bylines/index.ts
const prerender = false;
const GET = async ({ url, locals }) => {
	const { emdash, user } = locals;
	if (!emdash?.db) return apiError("NOT_CONFIGURED", "EmDash is not initialized", 500);
	const denied = requirePerm(user, "bylines:read");
	if (denied) return denied;
	const query = parseQuery(url, bylinesListQuery);
	if (isParseError(query)) return query;
	const i18n = getI18nConfig();
	if (query.locale && i18n && !i18n.locales.includes(query.locale)) return apiError("VALIDATION_ERROR", `Locale "${query.locale}" is not configured for this site`, 400);
	try {
		return apiSuccess(await new BylineRepository(emdash.db).findMany({
			search: query.search,
			isGuest: query.isGuest,
			userId: query.userId,
			locale: query.locale,
			cursor: query.cursor,
			limit: query.limit
		}));
	} catch (error) {
		return handleError(error, "Failed to list bylines", "BYLINE_LIST_ERROR");
	}
};
const POST = async ({ request, locals }) => {
	const { emdash, user } = locals;
	if (!emdash?.db) return apiError("NOT_CONFIGURED", "EmDash is not initialized", 500);
	const denied = requirePerm(user, "bylines:manage");
	if (denied) return denied;
	const body = await parseBody(request, bylineCreateBody);
	if (isParseError(body)) return body;
	try {
		const result = await handleBylineCreate(emdash.db, {
			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,
			locale: body.locale,
			translationOf: body.translationOf,
			customFields: body.customFields
		});
		if (result.success) /* @__PURE__ */ invalidateBylineCache();
		return unwrapResult(result, 201);
	} catch (error) {
		return handleError(error, "Failed to create byline", "BYLINE_CREATE_ERROR");
	}
};

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