"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildAssistantResultContract = buildAssistantResultContract;
function buildAssistantResultContract(params) {
    const safeEnvelopes = normalizeEnvelopes(params.dataEnvelopes);
    const diagnostics = normalizeDiagnostics(params.findings);
    const actions = buildActions(params.findings || [], diagnostics);
    return {
        version: '1.0.0',
        dataEnvelopes: safeEnvelopes,
        diagnostics,
        actions,
    };
}
function normalizeEnvelopes(dataEnvelopes) {
    if (!Array.isArray(dataEnvelopes))
        return [];
    return dataEnvelopes.filter((item) => {
        if (!item || typeof item !== 'object')
            return false;
        const candidate = item;
        return !!candidate.meta && !!candidate.data && !!candidate.display;
    });
}
function normalizeDiagnostics(findings) {
    if (!Array.isArray(findings))
        return [];
    return findings.map((finding, index) => {
        const title = toTrimmedString(finding.title) || `Finding ${index + 1}`;
        const description = toTrimmedString(finding.description) || title;
        return {
            id: toTrimmedString(finding.id) || `diag_${index + 1}`,
            category: toTrimmedString(finding.category) || undefined,
            severity: normalizeSeverity(finding.severity),
            title,
            description,
            confidence: typeof finding.confidence === 'number' ? finding.confidence : undefined,
            evidence: finding.evidence,
            timestampsNs: finding.timestampsNs,
        };
    });
}
function buildActions(findings, diagnostics) {
    const dedupe = new Map();
    for (let i = 0; i < findings.length; i++) {
        const finding = findings[i];
        const diagnostic = diagnostics[i];
        const recommendations = extractRecommendations(finding.recommendations);
        if (recommendations.length > 0) {
            for (let recIndex = 0; recIndex < recommendations.length; recIndex++) {
                const rec = recommendations[recIndex];
                const action = {
                    id: rec.id || `act_${diagnostic.id}_${recIndex + 1}`,
                    label: rec.label,
                    priority: normalizeActionPriority(rec.priority, diagnostic.severity),
                    rationale: diagnostic.title,
                    sourceDiagnosticId: diagnostic.id,
                };
                dedupeByLabel(dedupe, action);
            }
            continue;
        }
        if (diagnostic.severity === 'critical' || diagnostic.severity === 'high' || diagnostic.severity === 'warning') {
            const fallbackAction = {
                id: `act_${diagnostic.id}_investigate`,
                label: `Investigate: ${diagnostic.title}`,
                priority: normalizeActionPriority(undefined, diagnostic.severity),
                rationale: diagnostic.description,
                sourceDiagnosticId: diagnostic.id,
            };
            dedupeByLabel(dedupe, fallbackAction);
        }
    }
    return Array.from(dedupe.values()).slice(0, 12);
}
function dedupeByLabel(target, action) {
    const key = action.label.trim().toLowerCase();
    if (!key)
        return;
    if (!target.has(key)) {
        target.set(key, action);
    }
}
function extractRecommendations(recommendations) {
    if (!Array.isArray(recommendations))
        return [];
    const out = [];
    for (const recommendation of recommendations) {
        const normalized = normalizeRecommendation(recommendation);
        if (normalized)
            out.push(normalized);
    }
    return out;
}
function normalizeRecommendation(value) {
    if (typeof value === 'string') {
        const label = value.trim();
        if (!label)
            return null;
        return { label };
    }
    if (!value || typeof value !== 'object')
        return null;
    const record = value;
    const label = toTrimmedString(record.text) || toTrimmedString(record.label);
    if (!label)
        return null;
    return {
        id: toTrimmedString(record.id) || undefined,
        label,
        priority: typeof record.priority === 'number' ? record.priority : undefined,
    };
}
function normalizeSeverity(value) {
    const text = toTrimmedString(value).toLowerCase();
    if (text === 'critical' || text === 'high' || text === 'warning' || text === 'medium' || text === 'low' || text === 'info') {
        return text;
    }
    if (text === 'error')
        return 'high';
    if (text === 'warn')
        return 'warning';
    return 'info';
}
function normalizeActionPriority(numericPriority, severity) {
    if (typeof numericPriority === 'number') {
        if (numericPriority <= 1)
            return 'high';
        if (numericPriority <= 2)
            return 'medium';
        return 'low';
    }
    if (severity === 'critical' || severity === 'high')
        return 'high';
    if (severity === 'warning' || severity === 'medium')
        return 'medium';
    return 'low';
}
function toTrimmedString(value) {
    return typeof value === 'string' ? value.trim() : '';
}
//# sourceMappingURL=assistantResultContract.js.map