import { createStoreBackedReadModel, listProviderIds } from './ui-read-model-helpers.js';
export function createCoreReadModels(runtimeServices) {
    const { runtimeStore } = runtimeServices;
    return {
        providers: {
            getSnapshot() {
                return {
                    providerIds: listProviderIds(runtimeServices),
                };
            },
            subscribe(listener) {
                return runtimeServices.runtimeBus.on('PROVIDERS_CHANGED', listener);
            },
        },
        session: createStoreBackedReadModel(runtimeServices, () => {
            const state = runtimeStore.getState();
            return {
                session: state.session,
                totalTurns: state.conversation.totalTurns,
                messageCount: state.conversation.messageCount,
                estimatedContextTokens: state.conversation.estimatedContextTokens,
                contextWindow: state.model.tokenLimits.contextWindow,
                turnState: state.conversation.turnState,
                streamToolPreview: state.conversation.stream.partialToolPreview,
                contextWarningActive: state.conversation.contextWarningActive,
                pendingApproval: state.permissions.awaitingDecision,
                denialCount: state.permissions.denialCount,
            };
        }),
        agents: createStoreBackedReadModel(runtimeServices, () => {
            const state = runtimeStore.getState().agents;
            const active = state.activeAgentIds
                .map((id) => state.agents.get(id))
                .filter((agent) => agent !== undefined);
            return {
                active,
                totalSpawned: state.totalSpawned,
                totalCompleted: state.totalCompleted,
                totalFailed: state.totalFailed,
            };
        }),
        tasks: createStoreBackedReadModel(runtimeServices, () => {
            const tasksState = runtimeStore.getState().tasks;
            const tasks = [...tasksState.tasks.values()].sort((a, b) => {
                const aTime = a.startedAt ?? a.queuedAt;
                const bTime = b.startedAt ?? b.queuedAt;
                return bTime - aTime || a.title.localeCompare(b.title);
            });
            return { tasks };
        }),
    };
}
