import { i as runMigrations } from "../../../../runner-K8XK101B.mjs";
import "../../../../dialect-helpers-DRI5pyY3.mjs";
import "../../../../after-B1IIdH3Y.mjs";
import "../../../../object-cache-Bok5j2ae.mjs";
import "../../../../content-DhgEth4k.mjs";
import "../../../../base64-CmWvODNW.mjs";
import "../../../../types-CuW0zWDA.mjs";
import "../../../../media-BrXIaMFN.mjs";
import "../../../../taxonomy-BxGxZZdu.mjs";
import { t as OptionsRepository } from "../../../../options-DTTML-Tx.mjs";
import "../../../../redirect-IyfNirgS.mjs";
import "../../../../request-cache-KCNHp_RJ.mjs";
import "../../../../byline-registry-BOjqDOim.mjs";
import "../../../../field-defs-cache-WUI88LX5.mjs";
import "../../../../byline-Bm8ZgUP9.mjs";
import "../../../../content-refresh-CA4uKwzH.mjs";
import "../../../../fts-manager-BbFaF_V8.mjs";
import "../../../../registry-B65mUtPa.mjs";
import "../../../../loader-CB6YHeul.mjs";
import "../../../../settings-zhmdKyJQ.mjs";
import "../../../../ssrf-1g7usGBg.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 { d as setupBody } from "../../../../byline-fields-2Lp5Hbgf.mjs";
import "../../../../status-CqJQX1QN.mjs";
import "../../../../api/schemas/index.mjs";
import "../../../../ssrf-CmuGgCq3.mjs";
import { t as validateSeed } from "../../../../validate-CWSnKVIJ.mjs";
import { t as applySeed } from "../../../../apply-Bu-8nYHL.mjs";
import { t as loadSeed } from "../../../../load-Djn3HWQS.mjs";
import { n as getPublicOrigin } from "../../../../public-url-Danv47cS.mjs";
import { t as getAuthMode } from "../../../../mode-BM9QgEmt.mjs";

//#region src/astro/routes/api/setup/index.ts
const prerender = false;
const POST = async ({ request, url, locals }) => {
	const { emdash } = locals;
	if (!emdash?.db) return apiError("NOT_CONFIGURED", "EmDash is not initialized", 500);
	try {
		try {
			const setupComplete = await new OptionsRepository(emdash.db).get("emdash:setup_complete");
			if (setupComplete === true || setupComplete === "true") return apiError("ALREADY_CONFIGURED", "Setup has already been completed", 409);
		} catch {}
		const body = await parseBody(request, setupBody);
		if (isParseError(body)) return body;
		try {
			await runMigrations(emdash.db);
		} catch (error) {
			return handleError(error, "Failed to run database migrations", "MIGRATION_ERROR");
		}
		const seed = await loadSeed();
		seed.settings = {
			...seed.settings,
			title: body.title,
			tagline: body.tagline
		};
		const validation = validateSeed(seed);
		if (!validation.valid) return apiError("INVALID_SEED", `Invalid seed file: ${validation.errors.join(", ")}`, 400);
		let result;
		try {
			result = await applySeed(emdash.db, seed, {
				includeContent: body.includeContent,
				onConflict: "skip",
				storage: emdash.storage ?? void 0
			});
		} catch (error) {
			return handleError(error, "Failed to apply seed", "SEED_ERROR");
		}
		const useExternalAuth = getAuthMode(emdash.config).type === "external";
		try {
			const options = new OptionsRepository(emdash.db);
			const siteUrl = getPublicOrigin(url, emdash.config);
			await options.setIfAbsent("emdash:site_url", siteUrl);
			if (useExternalAuth) {
				await options.set("emdash:setup_complete", true);
				await options.set("emdash:site_title", body.title);
				if (body.tagline) await options.set("emdash:site_tagline", body.tagline);
			} else await options.set("emdash:setup_state", {
				step: "site_complete",
				title: body.title,
				tagline: body.tagline
			});
		} catch (error) {
			console.error("Failed to save setup state:", error);
		}
		return apiSuccess({
			success: true,
			setupComplete: useExternalAuth,
			result
		});
	} catch (error) {
		return handleError(error, "Setup failed", "SETUP_ERROR");
	}
};

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