import { resolve } from 'node:path';
import { createServerLogger, startHeddleControlPlaneServer } from '../../server/index.js';
const DEFAULT_CONTROL_PLANE_HOST = '127.0.0.1';
const DEFAULT_CONTROL_PLANE_PORT = 8765;
/**
 * Owns command-edge control-plane transport bootstrap.
 *
 * Runtime commands attach to a live server when available, or start an embedded
 * control-plane server when needed. After this bootstrap step, commands should
 * use the shared control-plane API rather than core runtime services.
 */
export class ControlPlaneCommandRuntimeService {
    static async resolve(input, dependencies = {}) {
        if (!input.forceOwnerConflict && input.runtimeHost.kind === 'server' && !input.runtimeHost.stale) {
            return {
                kind: 'attached',
                trpcUrl: ControlPlaneCommandRuntimeService.buildTrpcUrl(input.runtimeHost.endpoint),
                endpoint: input.runtimeHost.endpoint,
                serverId: input.runtimeHost.serverId,
                close: async () => undefined,
            };
        }
        const startServer = dependencies.startServer ?? startHeddleControlPlaneServer;
        const stateRoot = resolve(input.workspaceRoot, input.stateDir);
        const logger = dependencies.createLogger?.(stateRoot) ?? createServerLogger({
            stateRoot,
            console: false,
        });
        const handle = await startServer({
            mode: 'embedded-chat',
            workspaceRoot: input.workspaceRoot,
            stateRoot,
            preferApiKey: input.preferApiKey,
            heartbeatScheduler: input.heartbeatScheduler,
            host: DEFAULT_CONTROL_PLANE_HOST,
            port: DEFAULT_CONTROL_PLANE_PORT,
            logger,
        });
        return {
            kind: 'embedded',
            trpcUrl: ControlPlaneCommandRuntimeService.buildTrpcUrl(handle.endpoint),
            endpoint: handle.endpoint,
            serverId: handle.serverId,
            close: handle.close,
        };
    }
    static formatNotice(runtime, surface) {
        if (runtime.kind === 'attached') {
            return [
                `Heddle notice: attaching ${surface} to the live control-plane server.`,
                `server=http://${runtime.endpoint.host}:${runtime.endpoint.port}`,
                `serverId=${runtime.serverId}`,
            ].join(' ');
        }
        return [
            `Heddle notice: started embedded ${surface} control-plane server.`,
            `server=http://${runtime.endpoint.host}:${runtime.endpoint.port}`,
            `browser=http://${runtime.endpoint.host}:${runtime.endpoint.port}`,
            `serverId=${runtime.serverId}`,
        ].join(' ');
    }
    static installEmbeddedShutdown(runtime, label) {
        let shuttingDown = false;
        const shutdown = (signal) => {
            if (shuttingDown) {
                return;
            }
            shuttingDown = true;
            runtime.close()
                .catch((error) => {
                process.stderr.write(`Heddle embedded ${label} server failed during ${signal} shutdown: ${String(error)}\n`);
                process.exitCode = 1;
            })
                .finally(() => {
                process.exit();
            });
        };
        process.once('SIGINT', shutdown);
        process.once('SIGTERM', shutdown);
        return () => {
            process.off('SIGINT', shutdown);
            process.off('SIGTERM', shutdown);
        };
    }
    static buildTrpcUrl(endpoint) {
        return `http://${endpoint.host}:${endpoint.port}/trpc`;
    }
}
//# sourceMappingURL=control-plane-command-runtime.js.map