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 { b as inviteCreateBody } from "../../../../../byline-fields-2Lp5Hbgf.mjs";
import "../../../../../status-CqJQX1QN.mjs";
import "../../../../../api/schemas/index.mjs";
import { t as getSiteBaseUrl } from "../../../../../site-url-BBfDTgZB.mjs";
import { createKyselyAdapter } from "@emdash-cms/auth/adapters/kysely";
import { InviteError, Role, createInvite } from "@emdash-cms/auth";

//#region src/astro/routes/api/auth/invite/index.ts
const prerender = false;
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);
	const adapter = createKyselyAdapter(emdash.db);
	try {
		const body = await parseBody(request, inviteCreateBody);
		if (isParseError(body)) return body;
		const role = body.role ?? Role.AUTHOR;
		const siteName = await new OptionsRepository(emdash.db).get("emdash:site_title") || "EmDash";
		const baseUrl = await getSiteBaseUrl(emdash.db, request);
		const emailSend = emdash.email?.isAvailable() ? (message) => emdash.email.send(message, "system") : void 0;
		const result = await createInvite({
			baseUrl,
			siteName,
			email: emailSend
		}, adapter, body.email, role, user.id);
		if (emailSend) return apiSuccess({
			success: true,
			message: `Invite sent to ${body.email}`
		});
		return apiSuccess({
			success: true,
			message: "Invite created. No email provider configured — share the link manually.",
			inviteUrl: result.url
		}, 200);
	} catch (error) {
		if (error instanceof InviteError) return apiError(error.code.toUpperCase(), error.message, {
			user_exists: 409,
			invalid_token: 400,
			token_expired: 400
		}[error.code] ?? 400);
		return handleError(error, "Failed to create invite", "INVITE_CREATE_ERROR");
	}
};

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