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 { t as getTrustedProxyHeaders } from "../../../../../trusted-proxy-VTPRiX2s.mjs";
import { n as getPublicOrigin } from "../../../../../public-url-Danv47cS.mjs";
import "../../../../../api-tokens-DsvvCtdh.mjs";
import "../../../../../oauth-user-lookup-Bsc_GsX-.mjs";
import { n as handleDeviceCodeRequest } from "../../../../../device-flow-CfGP18Uw.mjs";
import { n as getClientIp, r as rateLimitResponse, t as checkRateLimit } from "../../../../../rate-limit-C_Lf63Dd.mjs";
import { z } from "zod";

//#region src/astro/routes/api/oauth/device/code.ts
const prerender = false;
const deviceCodeSchema = z.object({
	client_id: z.string().optional(),
	scope: z.string().optional()
});
const POST = async ({ request, locals, url }) => {
	const { emdash } = locals;
	if (!emdash?.db) return apiError("NOT_CONFIGURED", "EmDash is not initialized", 500);
	try {
		const body = await parseBody(request, deviceCodeSchema);
		if (isParseError(body)) return body;
		const ip = getClientIp(request, getTrustedProxyHeaders(emdash.config));
		if (!(await checkRateLimit(emdash.db, ip, "device/code", 10, 60)).allowed) return rateLimitResponse(60);
		const verificationUri = new URL("/_emdash/admin/device", getPublicOrigin(url, emdash?.config)).toString();
		return unwrapResult(await handleDeviceCodeRequest(emdash.db, body, verificationUri));
	} catch (error) {
		return handleError(error, "Failed to create device code", "DEVICE_CODE_ERROR");
	}
};

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