import { j as json } from './exports-D1quPX8S.js';
import { am as isSetupComplete, aZ as resolveStackDir, aA as performSetup, av as mapDockerError, p as checkDocker } from './src-CidJbygh.js';
import { g as getState, r as resetState } from './state-fxN45gBp.js';
import { r as requireAdmin, d as getRequestId, f as getUiLoginPassword, b as createSession } from './helpers-kzwksNq1.js';
import { s as sessionCookieHeader } from './session-cookie-CHgIES2p.js';
import { p as prepareSetupRestorePoint, s as startDeploy, r as resetDeployState } from './setup-deploy-B--th9ts.js';
import './utils-BSRjJDrZ.js';
import './chunk-CLZ62Ad-.js';
import 'node:module';
import './provider-constants-DPrj3yBX.js';
import './dist-DOFcG0Zp.js';
import 'node:fs';
import 'node:path';
import 'buffer';
import 'fs';
import 'path';
import 'node:child_process';
import 'node:url';
import 'node:crypto';
import 'events';
import 'node:events';
import 'node:stream';
import 'node:string_decoder';
import 'assert';
import 'zlib';
import 'node:assert';
import 'node:fs/promises';
import 'node:os';
import './endpoints-UdQGgKsR.js';

//#region src/routes/api/setup/complete/+server.ts
var POST = async (event) => {
	if (isSetupComplete(resolveStackDir())) {
		const authError = requireAdmin(event, getRequestId(event));
		if (authError) return authError;
	}
	const { request } = event;
	let body;
	try {
		body = await request.json();
	} catch {
		return json({
			ok: false,
			error: "invalid_json",
			message: "Request body must be valid JSON"
		}, { status: 400 });
	}
	const dryRun = body.dryRun === true;
	let result;
	try {
		prepareSetupRestorePoint(getState());
		result = await performSetup(body);
	} catch (err) {
		const mapped = mapDockerError(String(err));
		return json({
			ok: false,
			error: mapped.code,
			message: mapped.message
		}, { status: 500 });
	}
	if (!result.ok) return json(result, { status: 400 });
	resetState();
	const state = getState();
	resetDeployState();
	let dockerCheck = null;
	if (!dryRun) {
		dockerCheck = await checkDocker();
		if (dockerCheck.ok) startDeploy(state);
		else return json({
			ok: false,
			error: "docker_unavailable",
			message: "Docker isn't running. Start Docker Desktop or OrbStack, then try again."
		}, {
			status: 503,
			headers: { "content-type": "application/json" }
		});
	}
	const headers = new Headers({ "content-type": "application/json" });
	if (typeof body.security?.uiLoginPassword === "string" && body.security.uiLoginPassword || getUiLoginPassword()) {
		const sessionToken = createSession();
		headers.set("set-cookie", sessionCookieHeader(sessionToken, event.request));
	}
	return new Response(JSON.stringify({
		ok: true,
		dockerAvailable: dockerCheck?.ok ?? false,
		dryRun
	}), {
		status: 200,
		headers
	});
};

export { POST };
//# sourceMappingURL=_server.ts-D89NvzFz.js.map
