import { combineSubscriptions, createStoreBackedReadModel } from './ui-read-model-helpers.js';
export function createOperationsReadModels(runtimeServices, options = {}) {
    const { runtimeStore } = runtimeServices;
    return {
        automation: createStoreBackedReadModel(runtimeServices, () => {
            const state = runtimeStore.getState();
            const jobs = state.automation.jobIds
                .map((id) => state.automation.jobs.get(id))
                .filter((job) => job !== undefined)
                .sort((a, b) => (b.nextRunAt ?? 0) - (a.nextRunAt ?? 0) || a.name.localeCompare(b.name));
            const runs = state.automation.runIds
                .map((id) => state.automation.runs.get(id))
                .filter((run) => run !== undefined)
                .sort((a, b) => b.queuedAt - a.queuedAt || a.id.localeCompare(b.id));
            return {
                jobs,
                runs,
                totalJobs: state.automation.totalJobs,
                totalRuns: state.automation.totalRuns,
                activeRunIds: state.automation.activeRunIds,
                totalFailed: state.automation.totalFailed,
                sourceCount: state.automation.sourceIds.length,
                deliveryTotals: {
                    succeeded: state.deliveries.totalSucceeded,
                    failed: state.deliveries.totalFailed,
                    deadLettered: state.deliveries.totalDeadLettered,
                },
            };
        }),
        routes: createStoreBackedReadModel(runtimeServices, () => {
            const state = runtimeStore.getState().routes;
            const bindings = state.bindingIds
                .map((id) => state.bindings.get(id))
                .filter((binding) => binding !== undefined)
                .sort((a, b) => b.lastSeenAt - a.lastSeenAt || a.id.localeCompare(b.id));
            return {
                bindings,
                bindingIdsBySurface: state.bindingIdsBySurface,
                totalBindings: state.totalBindings,
                activeBindingIds: state.activeBindingIds,
                totalResolved: state.totalResolved,
                totalFailures: state.totalFailures,
            };
        }),
        watchers: createStoreBackedReadModel(runtimeServices, () => {
            const state = runtimeStore.getState().watchers;
            const watchers = state.watcherIds
                .map((id) => state.watchers.get(id))
                .filter((watcher) => watcher !== undefined)
                .sort((a, b) => (b.lastHeartbeatAt ?? 0) - (a.lastHeartbeatAt ?? 0) || a.id.localeCompare(b.id));
            return {
                watchers,
                totalWatchers: state.watcherIds.length,
                activeWatcherIds: state.activeWatcherIds,
                totalDegraded: state.totalDegraded,
                totalLagged: state.totalLagged,
            };
        }),
        orchestration: createStoreBackedReadModel(runtimeServices, () => {
            const state = runtimeStore.getState().orchestration;
            const graphs = [...state.graphs.values()].sort((a, b) => b.createdAt - a.createdAt);
            return {
                graphs,
                totalGraphs: state.totalGraphs,
                activeGraphIds: state.activeGraphIds,
                totalCompletedGraphs: state.totalCompletedGraphs,
                totalFailedGraphs: state.totalFailedGraphs,
                recursionGuardTrips: state.recursionGuardTrips,
            };
        }),
        communication: createStoreBackedReadModel(runtimeServices, () => {
            const state = runtimeStore.getState().communication;
            const records = state.recentRecordIds
                .map((id) => state.records.get(id))
                .filter((record) => record !== undefined)
                .sort((a, b) => b.timestamp - a.timestamp);
            return {
                records,
                totalSent: state.totalSent,
                totalDelivered: state.totalDelivered,
                totalBlocked: state.totalBlocked,
            };
        }),
        controlPlane: {
            getSnapshot() {
                const state = runtimeStore.getState().controlPlane;
                const clients = state.clientIds
                    .map((id) => state.clients.get(id))
                    .filter((client) => client !== undefined)
                    .sort((a, b) => (b.lastSeenAt ?? 0) - (a.lastSeenAt ?? 0) || a.id.localeCompare(b.id));
                return {
                    connectionState: state.connectionState,
                    activeClientIds: state.activeClientIds,
                    requestCount: state.requestCount,
                    errorCount: state.errorCount,
                    host: state.host,
                    port: state.port,
                    clients,
                    approvals: runtimeServices.approvalBroker.listApprovals(6),
                    sessions: runtimeServices.sessionBroker.listSessions(6),
                    recentEvents: options.getControlPlaneRecentEvents?.(6) ?? [],
                };
            },
            subscribe(listener) {
                const unsubs = [
                    runtimeStore.subscribe(listener),
                    runtimeServices.approvalBroker.subscribe(listener),
                ];
                return combineSubscriptions(...unsubs);
            },
        },
    };
}
