// SPEC-597-EXEMPT: canonical mapping (npm-package↔framework, keyword catalogue)
// — must enumerate framework names by design; no detection logic to migrate.
// engine/registry/core.ts — Orchestrator for spec accuracy validation and doc enrichment
// Composes loader → matcher → validator → reporter into two public functions.
import { loadRegistry } from './loader.js';
import { detectFrameworks, extractReferences, detectUnknownFrameworks } from './matcher.js';
import { validateReferences } from './validator.js';
import { buildReport, formatReport } from './reporter.js';
/**
 * Validate spec content against the framework registry.
 * Returns a complete SpecAccuracyReport with issues, score, and stale warnings.
 * This is the primary entry point for the check_spec_accuracy tool.
 *
 * @param specContent  Full text of the spec (HU.md or fragment)
 * @param frameworks   Optional list of framework IDs to scope the validation;
 *                     if omitted, frameworks are auto-detected from the text.
 */
export async function validateSpecContent(specContent, frameworks) {
    const registry = await loadRegistry();
    // Step 1: Determine which frameworks to check
    const frameworksToCheck = frameworks && frameworks.length > 0
        ? frameworks.filter((f) => registry.has(f))
        : detectFrameworks(specContent, registry);
    // Step 2: Detect frameworks mentioned in the text that are NOT in registry
    const unknownFrameworks = detectUnknownFrameworks(specContent, registry).filter((uf) => !frameworksToCheck.includes(uf));
    // Step 3: Extract API/pattern references
    const references = extractReferences(specContent, frameworksToCheck, registry);
    // Step 4: Validate each reference
    const validationResults = validateReferences(references, registry);
    // Step 5: Build and return report
    return buildReport(validationResults, frameworksToCheck, unknownFrameworks, registry);
}
/**
 * Enrich a documentation response with relevant registry patterns.
 * Used by consult_docs to add guardrail data when answering framework questions.
 *
 * @param topic     The topic or question being asked
 * @param framework The framework being queried (e.g. "nextjs")
 */
export async function enrichWithRegistry(topic, framework) {
    const registry = await loadRegistry();
    const normalizedId = framework.toLowerCase();
    const entry = registry.get(normalizedId);
    if (!entry) {
        return { matchedFrameworks: [], correctPatterns: [], deprecatedWarnings: [] };
    }
    const topicLower = topic.toLowerCase();
    // Filter patterns relevant to the topic
    const relevantCorrect = entry.correctPatterns
        .filter((p) => p.pattern.toLowerCase().includes(topicLower) ||
        topicLower.includes(p.pattern.toLowerCase().split(' ')[0] ?? ''))
        .map((p) => p.pattern + (p.example ? `: ${p.example}` : ''));
    const relevantDeprecated = entry.deprecatedPatterns
        .filter((p) => p.pattern.toLowerCase().includes(topicLower) ||
        topicLower.includes(p.replacedBy?.toLowerCase().split(' ')[0] ?? ''))
        .map((p) => `OBSOLETO: ${p.pattern} → Usar: ${p.replacedBy}`);
    return {
        matchedFrameworks: [normalizedId],
        correctPatterns: relevantCorrect,
        deprecatedWarnings: relevantDeprecated,
    };
}
/**
 * Format a validation report as Markdown.
 * Re-exported from reporter for convenience.
 */
export { formatReport };
/**
 * Save a custom registry entry to data/global/registry-custom.json.
 * Custom entries override base entries with the same framework ID.
 */
export async function saveCustomEntry(entry) {
    const { readJson, writeJson, globalDataDir } = await import('../../storage/base-store.js');
    const { join } = await import('node:path');
    const filePath = join(globalDataDir(), 'registry-custom.json');
    const store = await readJson(filePath, { entries: [] });
    // Replace existing entry with same framework ID, or append
    const existing = store.entries.findIndex((e) => e.framework === entry.framework);
    if (existing >= 0) {
        store.entries[existing] = entry;
    }
    else {
        store.entries.push(entry);
    }
    await writeJson(filePath, store);
}
//# sourceMappingURL=core.js.map