import { ToolCallsPanel } from './panels/tool-calls.js';
import { AgentsPanel } from './panels/agents.js';
import { TasksPanel } from './panels/tasks.js';
import { EventsPanel } from './panels/events.js';
import { StateInspectorPanel } from '@pellux/goodvibes-sdk/platform/runtime/diagnostics/panels/state-inspector';
import { HealthPanel } from './panels/health.js';
import { ToolContractsPanel } from '@pellux/goodvibes-sdk/platform/runtime/diagnostics/panels/tool-contracts';
/**
 * DiagnosticsProvider — unified data access layer for all diagnostic panels.
 *
 * Each panel is self-contained and subscribes to its own event sources.
 * The provider exposes a stable interface for retrieving snapshots and
 * registering change listeners.
 */
export class DiagnosticsProvider {
    _toolCalls;
    _agents;
    _tasks;
    _events;
    _stateInspector;
    _health;
    _toolContracts;
    constructor(config) {
        const pc = config.panelConfig;
        this._toolCalls = new ToolCallsPanel(config.eventBus, pc);
        this._agents = new AgentsPanel(config.eventBus, pc);
        this._tasks = new TasksPanel(config.eventBus, pc);
        this._events = new EventsPanel(config.eventBus, pc);
        this._stateInspector = new StateInspectorPanel(config.domains ? [...config.domains] : []);
        this._health = new HealthPanel(config.healthAggregator);
        this._toolContracts = new ToolContractsPanel(pc);
    }
    // ── Data access ──────────────────────────────────────────────────────────────
    /**
     * Retrieve tool call diagnostic entries.
     *
     * @param filter - Optional filter for domain, time range, trace/session/turn/task IDs, and limit.
     * @returns Filtered tool call entries, most recent first.
     */
    getToolCalls(filter) {
        return this._toolCalls.getSnapshot(filter);
    }
    /**
     * Retrieve agent diagnostic entries.
     *
     * @param filter - Optional filter.
     * @returns Filtered agent entries, most recent first.
     */
    getAgents(filter) {
        return this._agents.getSnapshot(filter);
    }
    /**
     * Retrieve task diagnostic entries.
     *
     * @param filter - Optional filter.
     * @returns Filtered task entries, most recent first.
     */
    getTasks(filter) {
        return this._tasks.getSnapshot(filter);
    }
    /**
     * Retrieve event timeline entries.
     *
     * @param filter - Optional filter. The `domains` field filters by domain name.
     * @returns Filtered event entries, most recent first.
     */
    getEvents(filter) {
        return this._events.getSnapshot(filter);
    }
    /**
     * Capture a point-in-time snapshot of all registered runtime domain states.
     *
     * @returns A RuntimeStateSnapshot with all domain states serialized.
     */
    getStateSnapshot() {
        return this._stateInspector.getSnapshot();
    }
    /**
     * Retrieve the current health dashboard data.
     *
     * @returns Aggregated health data sorted by severity.
     */
    getHealthDashboard() {
        return this._health.getSnapshot();
    }
    /**
     * Register a domain adapter for inclusion in state inspector snapshots.
     *
     * @param domain - Domain adapter implementing InspectableDomain.
     */
    registerDomain(domain) {
        this._stateInspector.registerDomain(domain);
    }
    // ── Tool contracts ────────────────────────────────────────────────────────────
    /**
     * Load (or reload) all tool contract verification results.
     * Replaces any previously loaded results.
     *
     * @param results - Map of tool name → ContractVerificationResult from ToolContractVerifier.
     */
    loadToolContracts(results) {
        this._toolContracts.load(results);
    }
    /**
     * Upsert a single tool contract verification result.
     * Use this for live updates when a single tool is re-verified.
     *
     * @param result - The ContractVerificationResult to upsert.
     */
    upsertToolContract(result) {
        this._toolContracts.upsert(result);
    }
    /**
     * Get the contract entry for a specific tool by name.
     *
     * @param toolName - Tool name to look up.
     * @returns The entry or undefined if not verified.
     */
    getToolContract(toolName) {
        return this._toolContracts.get(toolName);
    }
    /**
     * Get all tool contract entries, sorted by tool name.
     */
    getToolContracts() {
        return this._toolContracts.getAll();
    }
    /**
     * Get only tools that failed their contract checks.
     */
    getToolContractFailures() {
        return this._toolContracts.getFailures();
    }
    /**
     * Get summary counts across all tool contract results.
     */
    getToolContractSummary() {
        return this._toolContracts.getSummary();
    }
    // ── Change subscriptions ──────────────────────────────────────────────────────
    /**
     * Subscribe to change notifications for a specific panel.
     *
     * The callback is invoked whenever the panel's data changes. Call the
     * corresponding `get*` method inside the callback to retrieve the latest data.
     *
     * @param panel - The panel to subscribe to.
     * @param callback - Called whenever the panel's data updates.
     * @returns An unsubscribe function; call it to stop receiving notifications.
     */
    subscribe(panel, callback) {
        switch (panel) {
            case 'tool-calls': return this._toolCalls.subscribe(callback);
            case 'agents': return this._agents.subscribe(callback);
            case 'tasks': return this._tasks.subscribe(callback);
            case 'events': return this._events.subscribe(callback);
            case 'state-inspector': return this._stateInspector.subscribe(callback);
            case 'health': return this._health.subscribe(callback);
            case 'tool-contracts': return this._toolContracts.subscribe(callback);
        }
    }
    // ── Lifecycle ─────────────────────────────────────────────────────────────────
    /**
     * Dispose all panel data providers, releasing event bus subscriptions
     * and clearing all internal buffers.
     *
     * Call this when the diagnostics system is shut down.
     */
    dispose() {
        this._toolCalls.dispose();
        this._agents.dispose();
        this._tasks.dispose();
        this._events.dispose();
        this._health.dispose();
        this._toolContracts.dispose();
        // StateInspectorPanel has no disposable resources (no event bus subscriptions)
    }
}
