import { compareCodeUnits } from '../utils.js';
const PARSER_CAPABILITY_KEYS = [
    'definitions',
    'imports',
    'calls',
    'inheritance',
    'exports',
    'exactSpans',
];
const COVERAGE_RANK = {
    unavailable: 0,
    partial: 1,
    full: 2,
};
function summarizeFiles(files) {
    const parserVersions = new Set();
    let provenanceFiles = 0;
    let checkerFiles = 0;
    let syntaxFiles = 0;
    let checkerFallbackFiles = 0;
    let recoveredFiles = 0;
    let fallbackFiles = 0;
    let degradedFiles = 0;
    for (const file of files) {
        const provenance = file.provenance;
        if (!provenance) {
            degradedFiles++;
            continue;
        }
        provenanceFiles++;
        parserVersions.add(provenance.parserVersion);
        if (provenance.resolver.tier === 'typescript-checker')
            checkerFiles++;
        if (provenance.resolver.tier === 'syntax')
            syntaxFiles++;
        if (provenance.resolver.diagnostics?.some(diagnostic => diagnostic.reason === 'checker-unavailable')) {
            checkerFallbackFiles++;
        }
        if (provenance.syntax.status === 'recovered')
            recoveredFiles++;
        if (provenance.tier === 'regex-fallback')
            fallbackFiles++;
        const capabilitiesFull = Object.values(provenance.capabilities).every(coverage => coverage === 'full');
        if (provenance.syntax.status !== 'valid' || provenance.resolver.coverage !== 'full' || !capabilitiesFull) {
            degradedFiles++;
        }
    }
    return {
        totalFiles: files.length,
        provenanceFiles,
        legacyFiles: files.length - provenanceFiles,
        parserVersions: [...parserVersions].sort((left, right) => left - right),
        checkerFiles,
        syntaxFiles,
        checkerFallbackFiles,
        recoveredFiles,
        fallbackFiles,
        degradedFiles,
    };
}
function minimumCapabilities(files) {
    const capabilities = {
        definitions: 'full',
        imports: 'full',
        calls: 'full',
        inheritance: 'full',
        exports: 'full',
        exactSpans: 'full',
    };
    for (const key of PARSER_CAPABILITY_KEYS) {
        let minimum = 'full';
        for (const file of files) {
            const observed = file.provenance?.capabilities[key] ?? 'unavailable';
            if (COVERAGE_RANK[observed] < COVERAGE_RANK[minimum])
                minimum = observed;
        }
        capabilities[key] = minimum;
    }
    if (files.some(file => file.provenance?.capabilities.templateUses !== undefined)) {
        let minimum = 'full';
        for (const file of files) {
            const observed = file.provenance?.capabilities.templateUses ?? 'unavailable';
            if (COVERAGE_RANK[observed] < COVERAGE_RANK[minimum])
                minimum = observed;
        }
        capabilities.templateUses = minimum;
    }
    return capabilities;
}
/** Summarize parser evidence actually carried by the supplied index files. */
export function summarizeParserEvidence(index) {
    return summarizeFiles(index.files);
}
/**
 * Roll up the weakest observed parser capability and evidence counts for each
 * language. Languages are inserted in code-unit order so serialized payloads
 * remain deterministic across hosts.
 */
export function summarizeObservedCapabilities(index) {
    const filesByLanguage = new Map();
    for (const file of index.files) {
        const files = filesByLanguage.get(file.language) ?? [];
        files.push(file);
        filesByLanguage.set(file.language, files);
    }
    const observed = {};
    for (const language of [...filesByLanguage.keys()].sort(compareCodeUnits)) {
        const files = filesByLanguage.get(language);
        if (!files)
            continue;
        observed[language] = {
            ...summarizeFiles(files),
            capabilities: minimumCapabilities(files),
        };
    }
    return observed;
}
//# sourceMappingURL=parser-evidence.js.map