import { j as json } from './exports-D1quPX8S.js';
import { p as checkDocker, q as checkDockerCompose, Q as detectGpu, S as detectLocalProviders, T as detectRuntime } from './src-CidJbygh.js';
import { execFile } from 'node:child_process';
import { createServer } from 'node:net';
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: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';

//#region src/routes/api/setup/system-check/+server.ts
/**
* Returns true when the named port is published by an openpalm-managed
* docker container — i.e. it's "in use" but the wizard's install will
* either recreate or no-op on the same container, so flagging it as a
* conflict is a false positive. Returns `unreachable` when Docker itself
* cannot be queried, so the caller can degrade this from blocking to warning.
*/
async function portHeldByOurContainer(port) {
	return new Promise((resolve) => {
		const run = (attempt) => {
			execFile("docker", [
				"ps",
				"--format",
				"{{.Names}}	{{.Ports}}"
			], { timeout: 5e3 }, (err, stdout) => {
				if (err) {
					if (attempt === 0) return run(1);
					return resolve("unreachable");
				}
				const lines = stdout.toString().split("\n").map((l) => l.trim()).filter(Boolean);
				for (const line of lines) {
					const [name, ports] = line.split("	");
					if (!name || !name.startsWith("openpalm-")) continue;
					if (ports && ports.includes(`:${port}->`)) return resolve("held");
				}
				resolve("free");
			});
		};
		run(0);
	});
}
async function checkPortAvailable(port, timeoutMs = 1e3) {
	return new Promise((resolve) => {
		const srv = createServer();
		let settled = false;
		const finish = (ok) => {
			if (settled) return;
			settled = true;
			srv.close();
			resolve(ok);
		};
		const timer = setTimeout(() => finish(false), timeoutMs);
		srv.once("error", () => {
			clearTimeout(timer);
			finish(false);
		});
		srv.once("listening", () => {
			clearTimeout(timer);
			finish(true);
		});
		srv.listen(port, "127.0.0.1");
	});
}
function pickPort(...envNames) {
	for (const name of envNames) {
		const raw = process.env[name];
		if (!raw) continue;
		const n = Number(raw);
		if (Number.isFinite(n) && n > 0) return n;
	}
	return null;
}
function resolvePortsToCheck() {
	return [{
		port: pickPort("OP_HOST_UI_PORT") ?? 3880,
		service: "admin",
		blocking: true
	}, {
		port: pickPort("OP_HOST_ASSISTANT_PORT", "OP_ASSISTANT_PORT") ?? 3800,
		service: "assistant",
		blocking: true
	}];
}
var SERVER_PORT = Number(process.env.PORT ?? process.env.OP_HOST_UI_PORT ?? 3880);
var GET = async () => {
	const [docker, compose, gpu, localProviders, runtime] = await Promise.all([
		checkDocker(),
		checkDockerCompose(),
		detectGpu(),
		detectLocalProviders(),
		detectRuntime()
	]);
	const targets = resolvePortsToCheck();
	let portCheckReliable = docker.ok;
	const ports = await Promise.all(targets.map(async (t) => {
		if (t.port === SERVER_PORT) return {
			...t,
			available: true
		};
		if (await checkPortAvailable(t.port)) return {
			...t,
			available: true
		};
		const held = await portHeldByOurContainer(t.port);
		if (held === "held") return {
			...t,
			available: true
		};
		if (held === "unreachable") {
			portCheckReliable = false;
			return {
				...t,
				available: false,
				blocking: false
			};
		}
		return {
			...t,
			available: false,
			blocking: portCheckReliable ? t.blocking : false
		};
	}));
	return json({
		ok: true,
		docker: {
			ok: docker.ok,
			version: docker.stdout?.trim() || void 0,
			error: !docker.ok ? docker.stderr?.trim() || "Docker is not available" : void 0
		},
		compose: {
			ok: compose.ok,
			version: compose.stdout?.trim().split("\n")[0] || void 0,
			error: !compose.ok ? compose.stderr?.trim() || "Docker Compose v2 not found" : void 0
		},
		portCheckReliable,
		ports,
		platform: process.platform,
		runtime,
		gpu: gpu?.name ?? void 0,
		gpuInfo: gpu ?? void 0,
		hostProviders: localProviders.filter((p) => p.available).map(({ provider, url }) => ({
			provider,
			url
		}))
	});
};
var POST = GET;

export { GET, POST };
//# sourceMappingURL=_server.ts-B_HnVdK_.js.map
