import { r as createLazyRuntimeModule } from "./lazy-runtime-CgCh8H_K.js";
import { a as unregisterAcpRuntimeBackend, n as registerAcpRuntimeBackend, t as getAcpRuntimeBackend } from "./registry-CDGouO6a.js";
import "./acp-runtime-backend-DLeWck4L.js";
import { t as createLazyAcpRuntimeProxy } from "./runtime-proxy-CYoFLiqb.js";
//#region extensions/acpx/register.runtime.ts
/**
* Lazy ACPX runtime service registration. The plugin exposes an ACP backend
* immediately, then imports the heavier service only when a session needs it.
*/
const ACPX_BACKEND_ID = "acpx";
const loadServiceModule = createLazyRuntimeModule(() => import("./service-_GESUrqq.js"));
function unregisterOwnedRuntime(runtime) {
	if (runtime && getAcpRuntimeBackend(ACPX_BACKEND_ID)?.runtime === runtime) unregisterAcpRuntimeBackend(ACPX_BACKEND_ID);
}
async function startRealService(state, lifecycleRevision, deferredRuntime) {
	if (state.lifecycleRevision !== lifecycleRevision || !state.ctx) throw new Error("ACPX runtime service is not started");
	if (state.realRuntime) return state.realRuntime;
	if (state.startPromise) return await state.startPromise;
	const ctx = state.ctx;
	state.startPromise = (async () => {
		let publishedRuntime = null;
		const { createAcpxRuntimeService: createAcpxRuntimeServiceLocal } = await loadServiceModule();
		const service = createAcpxRuntimeServiceLocal({
			...state.params,
			backendLifecycle: {
				publish(backend) {
					if (state.lifecycleRevision !== lifecycleRevision || state.ctx !== ctx) throw new Error("ACPX runtime service stopped during activation");
					if (getAcpRuntimeBackend(ACPX_BACKEND_ID)?.runtime !== deferredRuntime) throw new Error("ACPX runtime service lost registry ownership during activation");
					registerAcpRuntimeBackend({
						id: ACPX_BACKEND_ID,
						...backend
					});
					publishedRuntime = backend.runtime;
					state.ownedRuntime = backend.runtime;
				},
				retract(runtime) {
					unregisterOwnedRuntime(runtime);
				}
			}
		});
		state.realService = service;
		await service.start(ctx);
		if (state.lifecycleRevision !== lifecycleRevision || state.ctx !== ctx) throw new Error("ACPX runtime service stopped during activation");
		if (!publishedRuntime) throw new Error("ACPX runtime service did not register an ACP backend");
		if (getAcpRuntimeBackend(ACPX_BACKEND_ID)?.runtime !== publishedRuntime) throw new Error("ACPX runtime service lost registry ownership during activation");
		state.realRuntime = publishedRuntime;
		return publishedRuntime;
	})();
	try {
		return await state.startPromise;
	} catch (error) {
		if (state.lifecycleRevision === lifecycleRevision) {
			state.startPromise = null;
			state.realService = null;
		}
		throw error;
	}
}
function createDeferredRuntime(state, lifecycleRevision) {
	const deferredRuntime = createLazyAcpRuntimeProxy(() => startRealService(state, lifecycleRevision, deferredRuntime));
	return deferredRuntime;
}
/** Creates the plugin service that registers ACPX as an ACP runtime backend. */
function createAcpxRuntimeService(params = {}) {
	const state = {
		ctx: null,
		lifecycleRevision: 0,
		ownedRuntime: null,
		params,
		realRuntime: null,
		realService: null,
		startPromise: null,
		stopPromise: null
	};
	return {
		id: "acpx-runtime",
		async start(ctx) {
			if (process.env.OPENCLAW_SKIP_ACPX_RUNTIME === "1") {
				ctx.logger.info("skipping embedded acpx runtime backend (OPENCLAW_SKIP_ACPX_RUNTIME=1)");
				return;
			}
			if (state.stopPromise) await state.stopPromise;
			state.lifecycleRevision += 1;
			const lifecycleRevision = state.lifecycleRevision;
			state.ctx = ctx;
			const deferredRuntime = createDeferredRuntime(state, lifecycleRevision);
			state.ownedRuntime = deferredRuntime;
			registerAcpRuntimeBackend({
				id: ACPX_BACKEND_ID,
				runtime: deferredRuntime
			});
			ctx.logger.info("embedded acpx runtime backend registered lazily");
		},
		async stop(ctx) {
			if (state.stopPromise) return await state.stopPromise;
			state.lifecycleRevision += 1;
			state.ctx = null;
			const ownedRuntime = state.ownedRuntime;
			unregisterOwnedRuntime(ownedRuntime);
			const startPromise = state.startPromise;
			state.stopPromise = (async () => {
				await startPromise?.catch(() => void 0);
				try {
					await state.realService?.stop?.(ctx);
				} finally {
					unregisterOwnedRuntime(ownedRuntime);
					state.ownedRuntime = null;
					state.realRuntime = null;
					state.realService = null;
					state.startPromise = null;
				}
			})();
			try {
				await state.stopPromise;
			} finally {
				state.stopPromise = null;
			}
		}
	};
}
//#endregion
export { createAcpxRuntimeService as t };
