import { resolve } from 'node:path';
import { FileDaemonRegistryRepository, RuntimeDaemonRegistryService } from '../../../../core/runtime/daemon/index.js';
import { RuntimeCredentialService } from '../../../../core/runtime/credentials/index.js';
import { controlPlaneChatSessionsController } from './chat-sessions-controller.js';
import { ControlPlaneHeartbeatController } from './heartbeat.js';
import { ControlPlaneMemoryController } from './memory.js';
export class ControlPlaneStateController {
    static async load(context, workspace = context.activeWorkspace) {
        const workspaceRoot = workspace.workspaceRoot;
        const stateRoot = workspace.stateRoot;
        const [tasks, memory] = await Promise.all([
            ControlPlaneHeartbeatController.listTasks(stateRoot),
            ControlPlaneMemoryController.readStatus(stateRoot),
        ]);
        return {
            workspaceRoot,
            stateRoot,
            auth: {
                preferApiKey: context.preferApiKey,
                openai: RuntimeCredentialService.resolveCredentialSourceForModel('gpt-5.4', { preferApiKey: context.preferApiKey }),
                anthropic: RuntimeCredentialService.resolveCredentialSourceForModel('claude-sonnet-4-6', { preferApiKey: context.preferApiKey }),
            },
            activeWorkspaceId: workspace.id,
            workspace,
            workspaces: context.workspaces,
            knownWorkspaces: ControlPlaneStateController.readKnownWorkspaces(context),
            runtimeHost: context.runtimeHost,
            sessions: controlPlaneChatSessionsController.readViews({
                workspaceRoot,
                stateRoot,
                sessionStoragePath: resolve(stateRoot, 'chat-sessions.catalog.json'),
                preferApiKey: context.preferApiKey,
                workspaceId: workspace.id,
            }),
            heartbeat: {
                tasks,
                runs: [],
            },
            memory,
        };
    }
    static readKnownWorkspaces(context) {
        const registryPath = context.runtimeHost?.registryPath ?? FileDaemonRegistryRepository.resolvePath();
        RuntimeDaemonRegistryService.registerKnownWorkspaces({
            registryPath,
            workspaces: context.workspaces,
        });
        const current = new Set(context.workspaces.map((workspace) => resolve(workspace.stateRoot)));
        const known = new Map();
        for (const record of RuntimeDaemonRegistryService.read(registryPath).workspaces) {
            const stateRoot = resolve(record.workspace.stateRoot);
            if (current.has(stateRoot)) {
                continue;
            }
            const existing = known.get(stateRoot);
            if (!existing || (record.workspace.updatedAt ?? '') > (existing.updatedAt ?? '')) {
                known.set(stateRoot, record.workspace);
            }
        }
        return Array.from(known.values()).sort((left, right) => (right.updatedAt ?? '').localeCompare(left.updatedAt ?? ''));
    }
}
//# sourceMappingURL=control-plane-state.js.map