globalThis.process ??= {};
globalThis.process.env ??= {};
import { r as __exportAll } from "./rolldown-runtime_-UtK6b3T.mjs";
import { T as setSetting, l as jsonError, m as SETTINGS_KEYS } from "./crypto_CY2zBtG3.mjs";
import { n as getFullDb } from "./db_DCN9iNkm.mjs";
import { i as finalizeInstall, r as claimInstall, t as applyD1Schema } from "./install_DEn_LvOD.mjs";
import { r as buildAuthEngine } from "./admin-engine_Cqzo5vQB.mjs";
import { l as isSameOrigin, n as loginWithCredentials, u as verifyDoubleSubmit } from "./auth_B0DWM0G6.mjs";
import { o as parseActiveSelection } from "./theme_BaEzwWHZ.mjs";
import { n as isSupportedLocale } from "./locales_CSRGAxBp.mjs";
import { env } from "cloudflare:workers";
//#region src/pages/api/install.ts
var install_exports = /* @__PURE__ */ __exportAll({
	POST: () => POST,
	prerender: () => false
});
var EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
var POST = async ({ request, cookies }) => {
	let body;
	try {
		body = await request.json();
	} catch {
		return jsonError(400, "validation_error", "Invalid request body.");
	}
	const raw = body;
	const siteTitle = typeof raw.siteTitle === "string" ? raw.siteTitle.trim() : "";
	const adminEmail = typeof raw.adminEmail === "string" ? raw.adminEmail.trim().toLowerCase() : "";
	const adminPassword = typeof raw.adminPassword === "string" ? raw.adminPassword : "";
	const csrf = typeof raw.csrf === "string" ? raw.csrf : "";
	if (!siteTitle || siteTitle.length > 100) return jsonError(400, "validation_error", "Site title is required.");
	if (!EMAIL_RE.test(adminEmail)) return jsonError(400, "validation_error", "That email is not valid.");
	if (adminPassword.length < 12) return jsonError(400, "validation_error", "Password must be at least 12 characters.");
	if (adminPassword.length > 1024) return jsonError(400, "validation_error", "Password is too long.");
	const cookieToken = cookies.get("install_csrf")?.value ?? "";
	if (!verifyDoubleSubmit(cookieToken, csrf)) return jsonError(403, "forbidden", "Invalid request.");
	if (!isSameOrigin(request.headers, [env.SITE_ORIGIN])) return jsonError(403, "forbidden", "Invalid request.");
	const { db, dialect } = getFullDb(env);
	if (dialect === "sqlite") await applyD1Schema(db);
	if (!await claimInstall(db, /* @__PURE__ */ new Date(), dialect)) return jsonError(409, "install_in_progress", "Setup is already in progress or complete.");
	try {
		await buildAuthEngine(env).createUser({
			email: adminEmail,
			password: adminPassword,
			roles: ["admin"]
		});
	} catch {
		return jsonError(409, "conflict", "That account could not be created.");
	}
	await finalizeInstall(db);
	try {
		await setSetting(db, SETTINGS_KEYS.theme, parseActiveSelection(raw.appearance));
		await setSetting(db, SETTINGS_KEYS.siteName, siteTitle);
		if (typeof raw.tagline === "string" && raw.tagline.trim().length > 0) await setSetting(db, "tagline", raw.tagline.trim());
		const method = raw.setupMethod === "agent" ? "agent" : "manual";
		await setSetting(db, SETTINGS_KEYS.setupMethod, method);
		const localeValue = typeof raw.locale === "string" ? raw.locale.trim() : "";
		if (isSupportedLocale(localeValue)) await setSetting(db, "locale", localeValue);
	} catch {}
	let target = "/admin";
	try {
		await loginWithCredentials(buildAuthEngine(env), cookies, {
			email: adminEmail,
			password: adminPassword
		});
	} catch {
		target = "/admin/login";
	}
	return new Response(null, {
		status: 303,
		headers: { Location: target }
	});
};
//#endregion
//#region \0virtual:astro:page:src/pages/api/install@_@ts
var page = () => install_exports;
//#endregion
export { page };
