import "../../../../../base64-CmWvODNW.mjs";
import "../../../../../types-CuW0zWDA.mjs";
import { 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 "../../../../../redirects-DXEXBAII.mjs";
import { E as passkeyRenameBody } from "../../../../../byline-fields-2Lp5Hbgf.mjs";
import "../../../../../status-CqJQX1QN.mjs";
import "../../../../../api/schemas/index.mjs";
import { createKyselyAdapter } from "@emdash-cms/auth/adapters/kysely";

//#region src/astro/routes/api/auth/passkey/[id].ts
const prerender = false;
/**
* PATCH - Rename a passkey
*/
const PATCH = async ({ params, request, locals }) => {
	const { emdash, user } = locals;
	const { id } = params;
	if (!emdash?.db) return apiError("NOT_CONFIGURED", "EmDash is not initialized", 500);
	if (!user) return apiError("NOT_AUTHENTICATED", "Not authenticated", 401);
	if (!id) return apiError("MISSING_PARAM", "Passkey ID is required", 400);
	try {
		const adapter = createKyselyAdapter(emdash.db);
		const credential = await adapter.getCredentialById(id);
		if (!credential || credential.userId !== user.id) return apiError("NOT_FOUND", "Passkey not found", 404);
		const body = await parseBody(request, passkeyRenameBody);
		if (isParseError(body)) return body;
		const trimmedName = body.name.trim() || null;
		await adapter.updateCredentialName(id, trimmedName);
		return apiSuccess({ passkey: {
			id: credential.id,
			name: trimmedName,
			deviceType: credential.deviceType,
			backedUp: credential.backedUp,
			createdAt: credential.createdAt.toISOString(),
			lastUsedAt: credential.lastUsedAt.toISOString()
		} });
	} catch (error) {
		return handleError(error, "Failed to rename passkey", "PASSKEY_RENAME_ERROR");
	}
};
/**
* DELETE - Remove a passkey
*/
const DELETE = async ({ params, locals }) => {
	const { emdash, user } = locals;
	const { id } = params;
	if (!emdash?.db) return apiError("NOT_CONFIGURED", "EmDash is not initialized", 500);
	if (!user) return apiError("NOT_AUTHENTICATED", "Not authenticated", 401);
	if (!id) return apiError("MISSING_PARAM", "Passkey ID is required", 400);
	try {
		const adapter = createKyselyAdapter(emdash.db);
		const credential = await adapter.getCredentialById(id);
		if (!credential || credential.userId !== user.id) return apiError("NOT_FOUND", "Passkey not found", 404);
		if (await adapter.countCredentialsByUserId(user.id) <= 1) return apiError("LAST_PASSKEY", "Cannot remove your last passkey", 400);
		await adapter.deleteCredential(id);
		return apiSuccess({ success: true });
	} catch (error) {
		return handleError(error, "Failed to delete passkey", "PASSKEY_DELETE_ERROR");
	}
};

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