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 { n as VALID_SCOPES } from "../../../../../api-tokens-DsvvCtdh.mjs";
import { n as handleApiTokenCreate, r as handleApiTokenList } from "../../../../../api-tokens-DvqeZmVq.mjs";
import { z } from "zod";
import { Role } from "@emdash-cms/auth";

//#region src/astro/routes/api/admin/api-tokens/index.ts
/**
* API token management endpoints
*
* GET  /_emdash/api/admin/api-tokens — List tokens for current user
* POST /_emdash/api/admin/api-tokens — Create a new token
*/
const prerender = false;
const createTokenSchema = z.object({
	name: z.string().min(1).max(100),
	scopes: z.array(z.enum(VALID_SCOPES)).min(1),
	expiresAt: z.string().datetime().optional()
});
/**
* List API tokens for the current user.
* Admins can list all tokens (future: add ?userId= filter).
*/
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 handleApiTokenList(emdash.db, user.id));
};
/**
* Create a new API token.
* Returns the raw token once — it cannot be retrieved again.
*/
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, createTokenSchema);
		if (isParseError(body)) return body;
		return unwrapResult(await handleApiTokenCreate(emdash.db, user.id, body), 201);
	} catch (error) {
		return handleError(error, "Failed to create API token", "TOKEN_CREATE_ERROR");
	}
};

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