import { n as definePlugin, t as defineCodemationApp } from "./CodemationAuthoring.types-BteaR3Dc.js";
import { n as logLevelPolicyFactory } from "./LogLevelPolicyFactory-DCUzIN6G.js";
import { t as ApiPaths } from "./ApiPaths-DCvrlIjg.js";
import { i as CodemationFrontendAuthSnapshotJsonCodec, n as InternalAuthBootstrapJsonCodec, r as FrontendAppConfigJsonCodec, t as PublicFrontendBootstrapJsonCodec } from "./PublicFrontendBootstrapJsonCodec-CegIF_ne.js";
import { t as CodemationConsumerConfigLoader } from "./CodemationConsumerConfigLoader-By-6tuGc.js";
import { o as BootTimer, t as ServerLoggerFactory } from "./ServerLoggerFactory-Ckk52S3w.js";
import "./decorateParam-BWxkAUSj.js";
import "./decorate-CXWmflG_.js";
import { f as ApplicationRequestError, m as ApplicationTokens } from "./CredentialServices-Bios0dM8.js";
import "./CodemationTsyringeTypeInfoRegistrar-Bj6FJYFz.js";
import { C as CodemationHonoApiApp, D as FrontendAppConfigFactory, E as InternalAuthBootstrapFactory, F as StartWorkflowRunCommand, I as UpsertLocalBootstrapUserCommand, L as ListUserAccountsQuery, N as GetRunStateQuery, O as CodemationFrontendAuthSnapshotFactory, T as PublicFrontendBootstrapFactory, a as GetCollectionQuery, c as InsertCollectionRowCommand, d as FrontendRuntime, f as CollectionSchemaSyncerHolder, i as GetCollectionRowQuery, k as WorkflowWebsocketServer, l as DeleteCollectionRowCommand, m as AppContainerLifecycle, n as ListCollectionsQuery, o as UpdateCollectionRowCommand, p as DatabaseMigrations, r as ListCollectionRowsQuery, s as SyncCollectionsCommand, t as AppContainerFactory, u as WorkerRuntime } from "./AppContainerFactory-jpYXGZGe.js";
import "./InternalPingRegistrar-BavAAnvk.js";
import "./WorkflowPolicyUiPresentationFactory-DfvD2VHk.js";
import { r as CodemationPluginPackageMetadata, t as AppConfigFactory } from "./AppConfigFactory-D4LL1aOR.js";
import { n as CodemationBootstrapRequest, t as CodemationPluginListMerger } from "./CodemationPluginListMerger-D1B1IEbt.js";
import { t as CodemationPostgresPrismaClientFactory } from "./CodemationPostgresPrismaClientFactory-C7156Fe-.js";
import { r as CodemationServerGateway } from "./server-BlG9qV5S.js";
import { workflow } from "@codemation/core-nodes";
import { createServer } from "node:http";

//#region src/bootstrap/runtime/HeadlessApiRuntime.ts
/**
* Boots the Codemation API + WebSocket servers without the Next.js UI process.
* Used by `codemation serve web --headless` for workspace pod containers where the
* UI is served externally (e.g. from the control-plane's customer-ui).
*/
var HeadlessApiRuntime = class {
	constructor(appContainerFactory, websocketServerFactory, httpServerFactory, logger) {
		this.appContainerFactory = appContainerFactory;
		this.websocketServerFactory = websocketServerFactory;
		this.httpServerFactory = httpServerFactory;
		this.logger = logger;
	}
	async start(appConfig) {
		const port = Number(appConfig.env.PORT ?? 4001);
		this.logger.info(`Starting codemation headless API runtime`);
		this.logger.info(`HTTP port: ${port}, WS port: ${appConfig.webSocketPort}`);
		const websocketServer = this.websocketServerFactory.create(appConfig);
		const container = await this.appContainerFactory.create({
			appConfig,
			sharedWorkflowWebsocketServer: websocketServer
		});
		await container.resolve(FrontendRuntime).start();
		const honoApp = container.resolve(CodemationHonoApiApp);
		const httpServer = this.httpServerFactory.create(honoApp, port, this.logger);
		await new Promise((resolve) => {
			httpServer.listen(port, () => {
				this.logger.info(`codemation headless API listening on port ${port}`);
				resolve();
			});
		});
	}
};

//#endregion
//#region src/presentation/websocket/WorkflowWebsocketServerFactory.ts
const loggerFactory = new ServerLoggerFactory(logLevelPolicyFactory);
var WorkflowWebsocketServerFactory = class {
	create(appConfig) {
		return new WorkflowWebsocketServer(appConfig.webSocketPort, appConfig.webSocketBindHost, loggerFactory.create("codemation-websocket.server"));
	}
};

//#endregion
//#region src/presentation/http/HeadlessHttpServerFactory.ts
/**
* Creates a Node.js http.Server that bridges IncomingMessage to Hono's Fetch API.
* Used by {@link import("../../bootstrap/runtime/HeadlessApiRuntime").HeadlessApiRuntime}
* to serve the Hono API without Next.js.
*/
var HeadlessHttpServerFactory = class {
	create(honoApp, port, logger) {
		return createServer((req, res) => {
			const url = new URL(req.url ?? "/", `http://${req.headers.host ?? `127.0.0.1:${port}`}`);
			const headers = new Headers();
			for (const [key, value] of Object.entries(req.headers)) {
				if (value === void 0) continue;
				if (Array.isArray(value)) for (const v of value) headers.append(key, v);
				else headers.set(key, value);
			}
			const chunks = [];
			req.on("data", (chunk) => chunks.push(chunk));
			req.on("end", () => {
				const body = chunks.length > 0 ? Buffer.concat(chunks) : null;
				const fetchRequest = new Request(url, {
					method: req.method ?? "GET",
					headers,
					body: body?.byteLength ? body : void 0,
					duplex: "half"
				});
				Promise.resolve(honoApp.fetch(fetchRequest)).then(async (fetchResponse) => {
					const responseHeaders = {};
					fetchResponse.headers.forEach((value, key) => {
						responseHeaders[key] = value;
					});
					res.writeHead(fetchResponse.status, responseHeaders);
					const responseBody = await fetchResponse.arrayBuffer();
					res.end(Buffer.from(responseBody));
				}).catch((err) => {
					logger.error("Unhandled request error", err instanceof Error ? err : new Error(String(err)));
					if (!res.headersSent) {
						res.writeHead(500);
						res.end("Internal server error");
					}
				});
			});
		});
	}
};

//#endregion
export { ApiPaths, AppConfigFactory, AppContainerFactory, AppContainerLifecycle, ApplicationRequestError, ApplicationTokens, BootTimer, CodemationBootstrapRequest, CodemationConsumerConfigLoader, CodemationFrontendAuthSnapshotFactory, CodemationFrontendAuthSnapshotJsonCodec, CodemationPluginListMerger, CodemationPluginPackageMetadata, CodemationPostgresPrismaClientFactory, CodemationServerGateway, CollectionSchemaSyncerHolder, DatabaseMigrations, DeleteCollectionRowCommand, FrontendAppConfigFactory, FrontendAppConfigJsonCodec, FrontendRuntime, GetCollectionQuery, GetCollectionRowQuery, GetRunStateQuery, HeadlessApiRuntime, HeadlessHttpServerFactory, InsertCollectionRowCommand, InternalAuthBootstrapFactory, InternalAuthBootstrapJsonCodec, ListCollectionRowsQuery, ListCollectionsQuery, ListUserAccountsQuery, PublicFrontendBootstrapFactory, PublicFrontendBootstrapJsonCodec, StartWorkflowRunCommand, SyncCollectionsCommand, UpdateCollectionRowCommand, UpsertLocalBootstrapUserCommand, WorkerRuntime, WorkflowWebsocketServerFactory, defineCodemationApp, definePlugin, workflow };
//# sourceMappingURL=index.js.map