function parseMetricLine(line) {
    if (!line || line.startsWith('#')) {
        return null;
    }
    const match = line.match(/^([a-zA-Z_:][a-zA-Z0-9_:]*)\s+(.+)$/);
    if (!match) {
        return null;
    }
    const [, nameWithLabels, valueStr] = match;
    if (!nameWithLabels || !valueStr) {
        return null;
    }
    const value = parseFloat(valueStr);
    if (isNaN(value)) {
        return null;
    }
    const labelsMatch = nameWithLabels.match(/^([a-zA-Z_:][a-zA-Z0-9_:]*)\{(.+)\}$/);
    if (labelsMatch) {
        const [, name, labelsStr] = labelsMatch;
        if (!name || !labelsStr) {
            return { name: nameWithLabels, value };
        }
        const labels = {};
        const labelPairs = labelsStr.match(/([a-zA-Z_][a-zA-Z0-9_]*)="([^"]*)"/g);
        if (labelPairs) {
            labelPairs.forEach(pair => {
                const parts = pair.split('=');
                const key = parts[0];
                const value = parts[1];
                if (key && value) {
                    labels[key] = value.replace(/"/g, '');
                }
            });
        }
        return { name, value, labels };
    }
    return { name: nameWithLabels, value };
}
export function parseMetrics(metricsText) {
    const lines = metricsText.split('\n');
    const metrics = {};
    for (const line of lines) {
        const parsed = parseMetricLine(line);
        if (!parsed)
            continue;
        const { name, value, labels } = parsed;
        if (!metrics[name]) {
            metrics[name] = [];
        }
        metrics[name].push({
            value,
            labels: labels || {}
        });
    }
    return metrics;
}
export function getMetricsSummary(metricsText) {
    const metrics = parseMetrics(metricsText);
    const getTotal = (name) => {
        if (!metrics[name])
            return 0;
        return metrics[name].reduce((sum, m) => sum + m.value, 0);
    };
    const getFirst = (name) => {
        if (!metrics[name] || metrics[name].length === 0)
            return 0;
        return metrics[name][0].value;
    };
    return {
        http: {
            requestsTotal: getTotal('http_requests_total'),
            errorsTotal: getTotal('http_errors_total'),
            requestDurationSum: getTotal('http_request_duration_seconds_sum'),
            requestDurationCount: getTotal('http_request_duration_seconds_count')
        },
        runs: {
            created: getTotal('runs_created_total'),
            started: getTotal('runs_started_total'),
            completed: getTotal('runs_completed_total'),
            active: getFirst('runs_active'),
            durationSum: getTotal('runs_duration_seconds_sum'),
            durationCount: getTotal('runs_duration_seconds_count')
        },
        sse: {
            connectionsOpened: getTotal('sse_connections_opened_total'),
            connectionsClosed: getTotal('sse_connections_closed_total'),
            activeConnections: getFirst('sse_active_connections'),
            messagesTotal: getTotal('sse_messages_total')
        },
        idempotency: {
            hits: getTotal('idempotency_hits_total'),
            misses: getTotal('idempotency_misses_total'),
            conflicts: getTotal('idempotency_conflicts_total'),
            storeSize: getFirst('idempotency_store_size')
        },
        rateLimit: {
            hits: getTotal('rate_limit_hits_total')
        },
        auth: {
            success: getTotal('auth_success_total'),
            failures: getTotal('auth_failures_total')
        },
        queue: {
            jobsQueued: getTotal('queue_jobs_queued_total'),
            jobsCompleted: getTotal('queue_jobs_completed_total'),
            jobsFailed: getTotal('queue_jobs_failed_total'),
            jobsWaiting: getFirst('queue_jobs_waiting'),
            jobsActive: getFirst('queue_jobs_active'),
            jobDurationSum: getTotal('queue_job_duration_seconds_sum'),
            jobDurationCount: getTotal('queue_job_duration_seconds_count')
        }
    };
}
//# sourceMappingURL=metrics-parser.js.map