import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from 'ink';
import { Colors } from '../colors.js';
import { useRuntimeApi } from '../contexts/RuntimeContext.js';
const METRIC_COL_WIDTH = 35;
const VALUE_COL_WIDTH = 20;
const StatRow = ({ title, value, isSubtle = false, }) => (_jsxs(Box, { children: [_jsx(Box, { width: METRIC_COL_WIDTH, children: _jsx(Text, { color: Colors.Gray, children: isSubtle ? `  ↳ ${title}` : title }) }), _jsx(Box, { width: VALUE_COL_WIDTH, justifyContent: "flex-end", children: _jsx(Text, { children: value }) })] }));
export const LBStatsDisplay = () => {
    const { getCliProviderManager } = useRuntimeApi();
    const providerManager = getCliProviderManager();
    if (!providerManager) {
        return (_jsx(Box, { borderStyle: "round", borderColor: Colors.Gray, paddingY: 1, paddingX: 2, children: _jsx(Text, { children: "Provider manager not available" }) }));
    }
    const activeProvider = providerManager.getActiveProvider();
    if (!activeProvider || activeProvider.name !== 'load-balancer') {
        return (_jsx(Box, { borderStyle: "round", borderColor: Colors.Gray, paddingY: 1, paddingX: 2, children: _jsx(Text, { color: Colors.Foreground, children: "No load balancer profile active. Use /profile load to activate a load balancer profile." }) }));
    }
    // Use getProviderByName to get the actual LoadBalancingProvider instance
    // (wrapped in LoggingProviderWrapper which delegates getStats())
    const lbProvider = providerManager.getProviderByName('load-balancer');
    // Check if provider exists and has getStats method
    if (!lbProvider ||
        !('getStats' in lbProvider) ||
        typeof lbProvider.getStats !== 'function') {
        return (_jsx(Box, { borderStyle: "round", borderColor: Colors.Gray, paddingY: 1, paddingX: 2, children: _jsxs(Text, { color: Colors.Foreground, children: ["Provider \"", activeProvider.name, "\" does not support load balancer statistics."] }) }));
    }
    // Call getStats() directly on the provider to preserve 'this' binding
    const stats = lbProvider.getStats();
    const backends = Object.keys(stats.backendMetrics);
    return (_jsxs(Box, { borderStyle: "round", borderColor: Colors.Gray, flexDirection: "column", paddingY: 1, paddingX: 2, children: [_jsx(Text, { bold: true, color: Colors.Foreground, children: "Load Balancer Statistics" }), _jsx(Box, { height: 1 }), _jsx(StatRow, { title: "Profile Name", value: _jsx(Text, { color: Colors.Foreground, children: stats.profileName }) }), _jsx(StatRow, { title: "Total Requests", value: _jsx(Text, { color: Colors.Foreground, children: stats.totalRequests.toLocaleString() }) }), stats.lastSelected && (_jsx(StatRow, { title: "Last Selected Backend", value: _jsx(Text, { color: Colors.Foreground, children: stats.lastSelected }) })), _jsx(Box, { height: 1 }), _jsx(Text, { bold: true, color: Colors.Foreground, children: "Backend Metrics" }), backends.map((backendName) => {
                const metrics = stats.backendMetrics[backendName];
                const cbState = stats.circuitBreakerStates[backendName] ?? {
                    state: 'closed',
                };
                const tpm = stats.currentTPM[backendName] || 0;
                return (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsx(Text, { bold: true, color: Colors.Foreground, children: backendName }), _jsx(StatRow, { title: "Requests", value: _jsx(Text, { color: Colors.Foreground, children: metrics.requests.toLocaleString() }), isSubtle: true }), _jsx(StatRow, { title: "Successes", value: _jsx(Text, { color: Colors.Foreground, children: metrics.successes.toLocaleString() }), isSubtle: true }), _jsx(StatRow, { title: "Failures", value: _jsx(Text, { color: metrics.failures > 0 ? Colors.AccentRed : Colors.Foreground, children: metrics.failures.toLocaleString() }), isSubtle: true }), _jsx(StatRow, { title: "Timeouts", value: _jsx(Text, { color: metrics.timeouts > 0 ? Colors.AccentRed : Colors.Foreground, children: metrics.timeouts.toLocaleString() }), isSubtle: true }), _jsx(StatRow, { title: "Tokens", value: _jsx(Text, { color: Colors.Foreground, children: metrics.tokens.toLocaleString() }), isSubtle: true }), _jsx(StatRow, { title: "Avg Latency (ms)", value: _jsx(Text, { color: Colors.Foreground, children: metrics.avgLatencyMs.toFixed(2) }), isSubtle: true }), _jsx(StatRow, { title: "Current TPM", value: _jsx(Text, { color: Colors.Foreground, children: Math.round(tpm).toLocaleString() }), isSubtle: true }), _jsx(StatRow, { title: "Circuit Breaker", value: _jsx(Text, { color: cbState.state === 'open'
                                    ? Colors.AccentRed
                                    : cbState.state === 'half-open'
                                        ? Colors.AccentYellow
                                        : Colors.Foreground, children: cbState.state.toUpperCase() }), isSubtle: true })] }, backendName));
            })] }));
};
//# sourceMappingURL=LBStatsDisplay.js.map