import "../../../../../base64-CmWvODNW.mjs";
import "../../../../../types-CuW0zWDA.mjs";
import { a as unwrapResult, r as handleError, t as apiError } from "../../../../../error-CkAseSqZ.mjs";
import { n as parseBody, t as isParseError } from "../../../../../parse-GmjecGct.mjs";
import { i as handleOAuthClientList, t as handleOAuthClientCreate } from "../../../../../oauth-clients-BmTI5rsl.mjs";
import { z } from "zod";
import { Role } from "@emdash-cms/auth";

//#region src/astro/routes/api/admin/oauth-clients/index.ts
/**
* OAuth client management endpoints
*
* GET  /_emdash/api/admin/oauth-clients — List all registered OAuth clients
* POST /_emdash/api/admin/oauth-clients — Register a new OAuth client
*/
const prerender = false;
const createClientSchema = z.object({
	id: z.string().min(1, "Client ID is required").max(255, "Client ID must be at most 255 characters"),
	name: z.string().min(1, "Name is required").max(255, "Name must be at most 255 characters"),
	redirectUris: z.array(z.string().url("Each redirect URI must be a valid URL")).min(1, "At least one redirect URI is required"),
	scopes: z.array(z.string()).optional()
});
/**
* List all registered OAuth clients.
*/
const GET = async ({ locals }) => {
	const { emdash, user } = locals;
	if (!emdash?.db) return apiError("NOT_CONFIGURED", "EmDash is not initialized", 500);
	if (!user || user.role < Role.ADMIN) return apiError("FORBIDDEN", "Admin privileges required", 403);
	return unwrapResult(await handleOAuthClientList(emdash.db));
};
/**
* Register a new OAuth client.
*/
const POST = async ({ request, locals }) => {
	const { emdash, user } = locals;
	if (!emdash?.db) return apiError("NOT_CONFIGURED", "EmDash is not initialized", 500);
	if (!user || user.role < Role.ADMIN) return apiError("FORBIDDEN", "Admin privileges required", 403);
	try {
		const body = await parseBody(request, createClientSchema);
		if (isParseError(body)) return body;
		return unwrapResult(await handleOAuthClientCreate(emdash.db, body), 201);
	} catch (error) {
		return handleError(error, "Failed to create OAuth client", "CLIENT_CREATE_ERROR");
	}
};

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