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 { S as allowedDomainUpdateBody } from "../../../../../redirects-DXEXBAII.mjs";
import "../../../../../byline-fields-2Lp5Hbgf.mjs";
import "../../../../../status-CqJQX1QN.mjs";
import "../../../../../api/schemas/index.mjs";
import { createKyselyAdapter } from "@emdash-cms/auth/adapters/kysely";
import { Role, roleFromLevel } from "@emdash-cms/auth";

//#region src/astro/routes/api/admin/allowed-domains/[domain].ts
const prerender = false;
/**
* PATCH - Update domain settings
*/
const PATCH = async ({ params, request, locals }) => {
	const { emdash, user } = locals;
	const { domain } = params;
	if (!emdash?.db) return apiError("NOT_CONFIGURED", "Database not configured", 500);
	if (!domain) return apiError("VALIDATION_ERROR", "Domain is required", 400);
	if (!user || user.role < Role.ADMIN) return apiError("FORBIDDEN", "Admin privileges required", 403);
	const adapter = createKyselyAdapter(emdash.db);
	try {
		const body = await parseBody(request, allowedDomainUpdateBody);
		if (isParseError(body)) return body;
		const existing = await adapter.getAllowedDomain(domain);
		if (!existing) return apiError("NOT_FOUND", "Domain not found", 404);
		const defaultRole = body.defaultRole;
		const enabled = body.enabled ?? existing.enabled;
		await adapter.updateAllowedDomain(domain, enabled, defaultRole);
		const updated = await adapter.getAllowedDomain(domain);
		return apiSuccess({
			success: true,
			domain: updated ? {
				domain: updated.domain,
				defaultRole: updated.defaultRole,
				roleName: roleFromLevel(updated.defaultRole),
				enabled: updated.enabled,
				createdAt: updated.createdAt.toISOString()
			} : null
		});
	} catch (error) {
		return handleError(error, "Failed to update allowed domain", "DOMAIN_UPDATE_ERROR");
	}
};
/**
* DELETE - Remove an allowed domain
*/
const DELETE = async ({ params, locals }) => {
	const { emdash, user } = locals;
	const { domain } = params;
	if (!emdash?.db) return apiError("NOT_CONFIGURED", "Database not configured", 500);
	if (!domain) return apiError("VALIDATION_ERROR", "Domain is required", 400);
	if (!user || user.role < Role.ADMIN) return apiError("FORBIDDEN", "Admin privileges required", 403);
	const adapter = createKyselyAdapter(emdash.db);
	try {
		if (!await adapter.getAllowedDomain(domain)) return apiError("NOT_FOUND", "Domain not found", 404);
		await adapter.deleteAllowedDomain(domain);
		return apiSuccess({ success: true });
	} catch (error) {
		return handleError(error, "Failed to delete allowed domain", "DOMAIN_DELETE_ERROR");
	}
};

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