// engine/registry/validator.ts — Compares extracted references against registry entries
// Classifies each reference as: valid, deprecated, mistake, or unknown
/** Search through all patterns in a registry entry to find a match. */
function findInPatterns(token, patterns) {
    const lowerToken = token.toLowerCase();
    return patterns.some((p) => {
        const lowerPattern = p.pattern.toLowerCase();
        const lowerExample = (p.example ?? '').toLowerCase();
        return lowerPattern.includes(lowerToken) || lowerExample.includes(lowerToken);
    });
}
/** Find a matching deprecated pattern and return its replacement. */
function findDeprecated(token, patterns) {
    const lowerToken = token.toLowerCase();
    return patterns.find((p) => {
        const lowerPattern = p.pattern.toLowerCase();
        const lowerExample = (p.example ?? '').toLowerCase();
        return lowerPattern.includes(lowerToken) || lowerExample.includes(lowerToken);
    });
}
/** Find a matching common mistake and return it. */
function findMistake(token, patterns) {
    const lowerToken = token.toLowerCase();
    return patterns.find((p) => {
        const lowerPattern = p.pattern.toLowerCase();
        const lowerExample = (p.example ?? '').toLowerCase();
        return lowerPattern.includes(lowerToken) || lowerExample.includes(lowerToken);
    });
}
/**
 * Validate a single reference against its framework registry entry.
 * Precedence: mistake > deprecated > correct > unknown
 */
export function validateReference(ref, registry) {
    const entry = registry.get(ref.framework);
    if (!entry) {
        return {
            reference: ref,
            status: 'unknown',
            issue: {
                type: 'unknown-framework',
                framework: ref.framework,
                reference: ref.text,
                suggestion: `Framework '${ref.framework}' no encontrado en el registry`,
                severity: 'warning',
            },
        };
    }
    // Check common mistakes first (highest priority — explicit errors)
    const mistake = findMistake(ref.text, entry.commonMistakes);
    if (mistake) {
        const issue = {
            type: 'invalid-api',
            framework: ref.framework,
            reference: ref.text,
            suggestion: mistake.replacedBy ? `Usar en su lugar: ${mistake.replacedBy}` : mistake.pattern,
            severity: mistake.severity ?? 'warning',
        };
        return { reference: ref, status: 'mistake', issue };
    }
    // Check deprecated patterns
    const deprecated = findDeprecated(ref.text, entry.deprecatedPatterns);
    if (deprecated) {
        const issue = {
            type: 'deprecated-pattern',
            framework: ref.framework,
            reference: ref.text,
            suggestion: `Patron obsoleto. Reemplazar con: ${deprecated.replacedBy}`,
            severity: deprecated.severity ?? 'warning',
        };
        return { reference: ref, status: 'deprecated', issue };
    }
    // Check correct patterns
    if (findInPatterns(ref.text, entry.correctPatterns)) {
        return { reference: ref, status: 'valid' };
    }
    // Not found in any pattern list — unknown but not an error
    return { reference: ref, status: 'unknown' };
}
/**
 * Validate all references and return the classification results.
 * References for frameworks not in the registry are skipped (handled separately).
 */
export function validateReferences(references, registry) {
    return references.map((ref) => validateReference(ref, registry));
}
/**
 * Detect registry entries that have not been updated in more than 180 days.
 */
export function detectStaleEntries(registry) {
    const now = Date.now();
    const MS_PER_DAY = 86_400_000;
    const STALE_DAYS = 180;
    const stale = [];
    for (const entry of registry.values()) {
        const updated = new Date(entry.lastUpdated).getTime();
        if (!isNaN(updated) && now - updated > STALE_DAYS * MS_PER_DAY) {
            stale.push(entry.framework);
        }
    }
    return stale;
}
//# sourceMappingURL=validator.js.map