import { p as checkDocker, w as composePs, n as buildComposeOptions, o as buildManagedServices } from './src-CidJbygh.js';
import { g as getState } from './state-fxN45gBp.js';
import { d as getRequestId, r as requireAdmin, k as jsonResponse } from './helpers-kzwksNq1.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/admin/containers/list/+server.ts
var GET = async (event) => {
	const requestId = getRequestId(event);
	const authError = requireAdmin(event, requestId);
	if (authError) return authError;
	const state = getState();
	const dockerCheck = await checkDocker();
	let dockerContainers = null;
	if (dockerCheck.ok) {
		const ps = await composePs(buildComposeOptions(state));
		if (ps.ok && ps.stdout.trim()) try {
			dockerContainers = ps.stdout.trim().split("\n").filter((l) => l.startsWith("{")).map((l) => JSON.parse(l));
		} catch (e) {
			console.warn("[containers.list] Failed to parse docker compose ps output", e);
			dockerContainers = null;
		}
	}
	if (dockerContainers) {
		const dockerStateByService = /* @__PURE__ */ new Map();
		for (const c of dockerContainers) if (c.Service && c.State) dockerStateByService.set(c.Service, c.State);
		for (const service of Object.keys(state.services)) {
			const dockerState = dockerStateByService.get(service);
			if (dockerState) state.services[service] = dockerState === "running" ? "running" : "stopped";
		}
	}
	let managedServices;
	try {
		managedServices = await buildManagedServices(state);
	} catch {
		managedServices = Object.keys(state.services);
	}
	const managedSet = new Set(managedServices);
	const containers = {};
	for (const [name, status] of Object.entries(state.services)) if (managedSet.has(name)) containers[name] = status;
	return jsonResponse(200, {
		containers,
		dockerContainers,
		dockerAvailable: dockerCheck.ok,
		managedServices
	}, requestId);
};

export { GET };
//# sourceMappingURL=_server.ts-D6vZvzLt.js.map
