import { handlePlanCommand } from '../core/plan-command-handler.js';
import { exportRemoteArtifactForAgent } from './remote/runner-registry.js';
export function createShellOpsServices(options) {
    const { agentManager, acpManager, automationManager, modeManager, planManager, adaptivePlanner, sessionOrchestration, remoteRuntime, planRuntime, } = options;
    return {
        agentManager,
        acpManager,
        automationManager,
        modeManager,
        planManager,
        adaptivePlanner,
        sessionOrchestration,
        remoteRuntime,
        planRuntime,
    };
}
export function createShellRemoteCommandService(options) {
    const { readModels, remoteRunnerRegistry, runtimeStore } = options;
    if (!remoteRunnerRegistry)
        return undefined;
    return {
        listActiveConnections: () => readModels.remote.getSnapshot().acp.activeConnections,
        getSnapshot: () => readModels.remote.getSnapshot(),
        listPools: () => remoteRunnerRegistry.listPools(),
        getPool: (id) => remoteRunnerRegistry.getPool(id),
        createPool: (input) => remoteRunnerRegistry.createPool(input),
        assignRunnerToPool: (poolId, runnerId) => remoteRunnerRegistry.assignRunnerToPool(poolId, runnerId),
        removeRunnerFromPool: (poolId, runnerId) => remoteRunnerRegistry.removeRunnerFromPool(poolId, runnerId),
        listContracts: () => remoteRunnerRegistry.listContracts(),
        getContract: (runnerId) => remoteRunnerRegistry.getContract(runnerId),
        registerContract: (contract) => remoteRunnerRegistry.registerContract(contract),
        upsertContractForAgent: (runnerId) => remoteRunnerRegistry.upsertContractForAgent(runnerId, runtimeStore),
        listArtifacts: () => remoteRunnerRegistry.listArtifacts(),
        getArtifact: (artifactId) => remoteRunnerRegistry.getArtifact(artifactId),
        buildReviewSummary: (artifactId) => remoteRunnerRegistry.buildReviewSummary(artifactId),
        exportArtifact: (artifactId, path) => remoteRunnerRegistry.exportArtifact(artifactId, path),
        exportArtifactForAgent: async (agentId, path) => (await exportRemoteArtifactForAgent(remoteRunnerRegistry, agentId, runtimeStore, path)
            ?? await (async () => {
                const artifact = remoteRunnerRegistry.captureArtifactForRunner(agentId, runtimeStore);
                if (!artifact)
                    return null;
                return remoteRunnerRegistry.exportArtifact(artifact.id, path);
            })()),
        importArtifact: (path) => remoteRunnerRegistry.importArtifact(path),
        exportSessionBundle: (path) => remoteRunnerRegistry.exportSessionBundle(runtimeStore, path),
        importSessionBundle: (path) => remoteRunnerRegistry.importSessionBundle(path),
    };
}
export function createShellPlanRuntime(options) {
    const { adaptivePlanner, runtimeBus } = options;
    if (!adaptivePlanner)
        return undefined;
    return (subcommand, args) => handlePlanCommand({ adaptivePlanner, runtimeBus }, subcommand, args);
}
