/**
 * Typed selectors for the runtime store.
 *
 * Each domain has a primary selector plus derived selectors for
 * common access patterns. All selectors are pure functions of RuntimeState.
 *
 * No ad hoc direct `set` from arbitrary modules.
 * Selectors are the read path; mutations go through DomainDispatch.
 */
// ---------------------------------------------------------------------------
// Primary domain selectors (one per domain)
// ---------------------------------------------------------------------------
/** Select the full session domain slice. */
export function selectSession(state) {
    return state.session;
}
/** Select the full model domain slice. */
export function selectModel(state) {
    return state.model;
}
/** Select the full conversation domain slice. */
export function selectConversation(state) {
    return state.conversation;
}
/** Select the full overlays domain slice. */
export function selectOverlays(state) {
    return state.overlays;
}
/** Select the full permissions domain slice. */
export function selectPermissions(state) {
    return state.permissions;
}
/** Select the full tasks domain slice. */
export function selectTasks(state) {
    return state.tasks;
}
/** Select the full agents domain slice. */
export function selectAgents(state) {
    return state.agents;
}
/** Select the full provider health domain slice. */
export function selectProviderHealth(state) {
    return state.providerHealth;
}
/** Select the full MCP domain slice. */
export function selectMcp(state) {
    return state.mcp;
}
/** Select the full plugins domain slice. */
export function selectPlugins(state) {
    return state.plugins;
}
/** Select the full daemon domain slice. */
export function selectDaemon(state) {
    return state.daemon;
}
/** Select the full ACP domain slice. */
export function selectAcp(state) {
    return state.acp;
}
/** Select the full integrations domain slice. */
export function selectIntegrations(state) {
    return state.integrations;
}
/** Select the full telemetry domain slice. */
export function selectTelemetry(state) {
    return state.telemetry;
}
/** Select the full git domain slice. */
export function selectGit(state) {
    return state.git;
}
/** Select the full discovery domain slice. */
export function selectDiscovery(state) {
    return state.discovery;
}
/** Select the full intelligence domain slice. */
export function selectIntelligence(state) {
    return state.intelligence;
}
/** Select the full orchestration domain slice. */
export function selectOrchestration(state) {
    return state.orchestration;
}
/** Select the full communication domain slice. */
export function selectCommunication(state) {
    return state.communication;
}
/** Select the full automation domain slice. */
export function selectAutomation(state) {
    return state.automation;
}
/** Select the full routes domain slice. */
export function selectRoutes(state) {
    return state.routes;
}
/** Select the full control plane domain slice. */
export function selectControlPlane(state) {
    return state.controlPlane;
}
/** Select the full deliveries domain slice. */
export function selectDeliveries(state) {
    return state.deliveries;
}
/** Select the full watchers domain slice. */
export function selectWatchers(state) {
    return state.watchers;
}
/** Select the full surfaces domain slice. */
export function selectSurfaces(state) {
    return state.surfaces;
}
/** Select the full surface performance domain slice. */
export function selectSurfacePerf(state) {
    return state.surfacePerf;
}
/**
 * Returns the active model identity fields.
 * Use this in components that need to display or route by model.
 */
export function selectActiveModel(state) {
    const { activeProviderId, activeModelId, displayName } = state.model;
    return { providerId: activeProviderId, modelId: activeModelId, displayName };
}
/**
 * Returns all tasks currently in 'running' state, ordered by startedAt.
 */
export function selectRunningTasks(state) {
    const { tasks, runningIds } = state.tasks;
    return runningIds
        .map((id) => tasks.get(id))
        .filter((t) => t !== undefined)
        .sort((a, b) => (a.startedAt ?? 0) - (b.startedAt ?? 0));
}
/**
 * Returns all agents currently in non-terminal states.
 */
export function selectRunningAgents(state) {
    const { agents, activeAgentIds } = state.agents;
    return activeAgentIds
        .map((id) => agents.get(id))
        .filter((a) => a !== undefined);
}
/**
 * Derives a simplified HealthStatus for a named domain.
 * Returns 'healthy', 'degraded', 'critical', or 'unknown'.
 */
export function selectDomainHealth(state, domain) {
    switch (domain) {
        case 'providerHealth':
            return state.providerHealth.compositeStatus;
        case 'mcp': {
            const connected = state.mcp.connectedServerNames.length;
            const total = state.mcp.servers.size;
            if (total === 0)
                return 'unknown';
            if (connected === total)
                return 'healthy';
            if (connected === 0)
                return 'critical';
            return 'degraded';
        }
        case 'daemon': {
            const ts = state.daemon.transportState;
            if (ts === 'connected')
                return 'healthy';
            if (ts === 'degraded' || ts === 'reconnecting')
                return 'degraded';
            if (ts === 'terminal_failure')
                return 'critical';
            return 'unknown';
        }
        case 'acp': {
            const ts = state.acp.managerTransportState;
            if (ts === 'connected')
                return 'healthy';
            if (ts === 'degraded' || ts === 'reconnecting')
                return 'degraded';
            if (ts === 'terminal_failure')
                return 'critical';
            return 'unknown';
        }
        case 'integrations': {
            const { problemIds, integrations } = state.integrations;
            if (integrations.size === 0)
                return 'unknown';
            if (problemIds.length === 0)
                return 'healthy';
            if (problemIds.length === integrations.size)
                return 'critical';
            return 'degraded';
        }
    }
}
/**
 * Returns a composite system health summary across all tracked subsystems.
 */
export function selectSystemHealth(state) {
    const domains = {
        providerHealth: selectDomainHealth(state, 'providerHealth'),
        mcp: selectDomainHealth(state, 'mcp'),
        daemon: selectDomainHealth(state, 'daemon'),
        acp: selectDomainHealth(state, 'acp'),
        integrations: selectDomainHealth(state, 'integrations'),
    };
    const values = Object.values(domains);
    const hasCritical = values.includes('critical');
    const hasDegraded = values.includes('degraded');
    let status;
    if (hasCritical) {
        status = 'critical';
    }
    else if (hasDegraded) {
        status = 'degraded';
    }
    else if (values.every((v) => v === 'healthy')) {
        status = 'healthy';
    }
    else {
        status = 'unknown';
    }
    return { status, domains, hasCritical, hasDegraded };
}
/**
 * Returns the current permission mode.
 */
export function selectPermissionMode(state) {
    return state.permissions.mode;
}
/**
 * Returns whether any overlay is currently visible.
 */
export function selectAnyOverlayVisible(state) {
    return state.overlays.visibleStack.length > 0;
}
/**
 * Returns whether a specific overlay is visible.
 */
export function selectOverlayVisible(state, overlayId) {
    return state.overlays.visibleStack.includes(overlayId);
}
/**
 * Returns the current turn state from the conversation domain.
 */
export function selectTurnState(state) {
    return state.conversation.turnState;
}
/**
 * Returns the current store-owned partial tool preview for the active stream.
 */
export function selectStreamToolPreview(state) {
    return state.conversation.stream.partialToolPreview;
}
/**
 * Returns whether a turn is currently active (not idle or terminal).
 */
export function selectIsTurnActive(state) {
    const ts = state.conversation.turnState;
    return ts !== 'idle' && ts !== 'completed' && ts !== 'failed' && ts !== 'cancelled';
}
/**
 * Returns whether the session is fully initialized and ready.
 */
export function selectIsSessionReady(state) {
    return (state.session.status === 'active' &&
        state.session.recoveryState === 'ready');
}
/**
 * Returns the number of currently running tasks, grouped by kind.
 */
export function selectRunningTaskCountByKind(state) {
    const counts = {};
    for (const id of state.tasks.runningIds) {
        const task = state.tasks.tasks.get(id);
        if (task) {
            counts[task.kind] = (counts[task.kind] ?? 0) + 1;
        }
    }
    return counts;
}
