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 { a as handleOAuthClientUpdate, n as handleOAuthClientDelete, r as handleOAuthClientGet } from "../../../../../oauth-clients-BmTI5rsl.mjs";
import { z } from "zod";
import { Role } from "@emdash-cms/auth";

//#region src/astro/routes/api/admin/oauth-clients/[id].ts
/**
* Single OAuth client endpoints
*
* GET    /_emdash/api/admin/oauth-clients/:id — Get a client
* PUT    /_emdash/api/admin/oauth-clients/:id — Update a client
* DELETE /_emdash/api/admin/oauth-clients/:id — Delete a client
*/
const prerender = false;
const updateClientSchema = z.object({
	name: z.string().min(1).max(255).optional(),
	redirectUris: z.array(z.string().url("Each redirect URI must be a valid URL")).min(1, "At least one redirect URI is required").optional(),
	scopes: z.array(z.string()).nullable().optional()
});
/**
* Get a single OAuth client.
*/
const GET = async ({ params, 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);
	const clientId = params.id;
	if (!clientId) return apiError("VALIDATION_ERROR", "Client ID is required", 400);
	return unwrapResult(await handleOAuthClientGet(emdash.db, clientId));
};
/**
* Update an OAuth client.
*/
const PUT = async ({ params, 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);
	const clientId = params.id;
	if (!clientId) return apiError("VALIDATION_ERROR", "Client ID is required", 400);
	try {
		const body = await parseBody(request, updateClientSchema);
		if (isParseError(body)) return body;
		return unwrapResult(await handleOAuthClientUpdate(emdash.db, clientId, body));
	} catch (error) {
		return handleError(error, "Failed to update OAuth client", "CLIENT_UPDATE_ERROR");
	}
};
/**
* Delete an OAuth client.
*/
const DELETE = async ({ params, 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);
	const clientId = params.id;
	if (!clientId) return apiError("VALIDATION_ERROR", "Client ID is required", 400);
	try {
		return unwrapResult(await handleOAuthClientDelete(emdash.db, clientId));
	} catch (error) {
		return handleError(error, "Failed to delete OAuth client", "CLIENT_DELETE_ERROR");
	}
};

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