import { ClientSharedSessionMessageService } from '../../client-shared/services/session-messages/index.js';
/**
 * Owns cli-v2 workspace/session loading and selection lifecycle.
 *
 * This is not a generic API proxy: it owns the ordering that makes a selected
 * session coherent for the TUI, including active-state reset, session detail
 * fetch, runtime context fetch, run-state mirror, pending approval refresh, and
 * event-stream subscription.
 */
export class ControlPlaneSessionLoader {
    options;
    constructor(options) {
        this.options = options;
    }
    async refreshSessions() {
        const workspaceId = this.options.state.requireWorkspaceId();
        const sessions = await this.options.api.listSessions(workspaceId);
        this.options.state.patch({ sessions });
        return sessions;
    }
    async createSession(input = {}) {
        const workspaceId = this.options.state.requireWorkspaceId();
        const session = await this.options.api.createSession(workspaceId, input);
        await this.refreshSessions();
        return session;
    }
    async selectSession(sessionId) {
        const workspaceId = this.options.state.requireWorkspaceId();
        this.options.assistantStreamBuffer.reset();
        this.options.state.patch({
            activeSessionId: sessionId,
            activeSession: null,
            runtimeContext: undefined,
            pendingApproval: null,
            pendingDirectShellConfirmation: undefined,
            liveStatus: undefined,
            currentActivity: undefined,
            activePlan: undefined,
            recentEditDiffs: [],
            latestUpdate: undefined,
            error: undefined,
            loading: true,
            streamConnected: false,
        });
        try {
            const session = await this.options.api.getSession(workspaceId, sessionId);
            const runtimeContext = await this.options.api.getRuntimeContext(workspaceId, sessionId);
            const running = await this.options.api.getRunning(workspaceId, sessionId);
            this.options.state.patch({
                activeSession: session,
                runtimeContext,
                running: running.running,
                loading: false,
            });
            await this.options.onRefreshPendingApproval(sessionId);
            this.options.subscriptions.subscribeToSessionEvents(workspaceId, sessionId);
        }
        catch (error) {
            this.options.state.patch({ error: this.options.onError(error), loading: false });
        }
    }
    async refreshSession(sessionId, options = {}) {
        const workspaceId = this.options.state.requireWorkspaceId();
        if (!options.silent) {
            this.options.state.patch({ loading: true });
        }
        try {
            const next = await this.options.api.getSession(workspaceId, sessionId);
            const runtimeContext = await this.options.api.getRuntimeContext(workspaceId, sessionId);
            this.options.state.patch((current) => ({
                activeSession: options.silent
                    ? ClientSharedSessionMessageService.mergeTransientMessages(current.activeSession, next)
                    : next,
                runtimeContext,
                loading: false,
            }));
        }
        catch (error) {
            this.options.state.patch({ error: this.options.onError(error), loading: false });
        }
    }
}
//# sourceMappingURL=control-plane-session-loader.js.map