import "../../../../../base64-CmWvODNW.mjs";
import "../../../../../types-CuW0zWDA.mjs";
import { t as OptionsRepository } from "../../../../../options-DTTML-Tx.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 { x as inviteRegisterOptionsBody } from "../../../../../byline-fields-2Lp5Hbgf.mjs";
import "../../../../../status-CqJQX1QN.mjs";
import "../../../../../api/schemas/index.mjs";
import { n as getPublicOrigin } from "../../../../../public-url-Danv47cS.mjs";
import { n as createChallengeStore } from "../../../../../challenge-store-anhwVER0.mjs";
import { t as getPasskeyConfig } from "../../../../../passkey-config-De0ahnRg.mjs";
import { ulid } from "ulidx";
import { createKyselyAdapter } from "@emdash-cms/auth/adapters/kysely";
import { InviteError, validateInvite } from "@emdash-cms/auth";
import { generateRegistrationOptions } from "@emdash-cms/auth/passkey";

//#region src/astro/routes/api/auth/invite/register-options.ts
const prerender = false;
const POST = async ({ request, locals }) => {
	const { emdash } = locals;
	if (!emdash?.db) return apiError("NOT_CONFIGURED", "EmDash is not initialized", 500);
	try {
		const body = await parseBody(request, inviteRegisterOptionsBody);
		if (isParseError(body)) return body;
		const invite = await validateInvite(createKyselyAdapter(emdash.db), body.token);
		const url = new URL(request.url);
		const passkeyConfig = getPasskeyConfig(url, await new OptionsRepository(emdash.db).get("emdash:site_title") ?? void 0, getPublicOrigin(url, emdash?.config));
		const challengeStore = createChallengeStore(emdash.db);
		return apiSuccess({ options: await generateRegistrationOptions(passkeyConfig, {
			id: ulid(),
			email: invite.email,
			name: body.name || null
		}, [], challengeStore) });
	} catch (error) {
		if (error instanceof InviteError) return apiError(error.code.toUpperCase(), error.message, {
			invalid_token: 404,
			token_expired: 410,
			user_exists: 409
		}[error.code] ?? 400);
		return handleError(error, "Failed to generate registration options", "INVITE_REGISTER_OPTIONS_ERROR");
	}
};

//#endregion
export { POST, prerender };
//# sourceMappingURL=register-options.mjs.map