// tools/suggest-tooling/hypermedia-catalog.ts — SPEC-043
// Loads and formats hypermedia tooling recommendations from the config catalog.
import { readFile } from 'node:fs/promises';
let catalogCache = null;
/** Reset the in-memory cache (for testing only). */
export function resetHypermediaCatalogCache() {
    catalogCache = null;
}
async function loadHypermediaCatalog() {
    if (catalogCache) {
        return catalogCache;
    }
    try {
        const configPath = new URL('../../config/hypermedia-tooling.json', import.meta.url);
        const content = await readFile(configPath, 'utf-8');
        const raw = JSON.parse(content);
        // Remove meta keys
        delete raw._description;
        delete raw._htmxExtensions;
        catalogCache = raw;
        return catalogCache;
    }
    catch {
        return {};
    }
}
const SPA_FRAMEWORKS = ['react', 'vue', 'angular', 'svelte', 'solid', 'qwik', 'next', 'nuxt'];
const BUNDLER_TOOLS = ['webpack', 'vite', 'esbuild', 'parcel', 'rollup'];
/**
 * Build tooling recommendation lines for hypermedia/backend-first projects.
 * - If rendering is server-side: explicitly notes that SPA/bundler tools are excluded.
 * - Loads catalog entry for 'framework:language' key.
 */
export async function buildHypermediaToolingLines(knowledge) {
    const hypermediaFw = knowledge.hypermediaFramework;
    const rendering = knowledge.rendering;
    // No hypermedia framework → skip
    if (!hypermediaFw || hypermediaFw === 'none') {
        return [];
    }
    const lines = [
        '## Hypermedia & Backend-First Tooling',
        '',
        `**Hypermedia framework detected:** \`${hypermediaFw}\``,
        `**Rendering mode:** ${rendering ?? 'server-side'}`,
        '',
    ];
    // Guard: no SPA/bundler recommendations for server-side projects
    if (rendering === 'server-side') {
        lines.push('> **Note:** SPA frameworks and bundlers are NOT recommended for this project.');
        lines.push(`> Excluded: ${[...SPA_FRAMEWORKS, ...BUNDLER_TOOLS].map((f) => `\`${f}\``).join(', ')}`);
        lines.push('');
    }
    // Load catalog and find matching entry
    const catalog = await loadHypermediaCatalog();
    const catalogKey = `${hypermediaFw}:${knowledge.language}`;
    const entry = catalog[catalogKey];
    if (entry) {
        if (entry.frameworks.length > 0) {
            lines.push('### Recommended Frameworks');
            lines.push('');
            for (const fw of entry.frameworks) {
                lines.push(`**${fw.name}**`);
                lines.push(fw.description);
                lines.push(`- Install: \`${fw.install}\``);
                lines.push(`- Docs: ${fw.docsUrl}`);
                lines.push('');
            }
        }
        if (entry.testing.length > 0) {
            lines.push('### Recommended Testing');
            lines.push('');
            for (const t of entry.testing) {
                lines.push(`**${t.name}**`);
                lines.push(t.description);
                lines.push(`- Install: \`${t.install}\``);
                lines.push(`- Docs: ${t.docsUrl}`);
                lines.push('');
            }
        }
    }
    else {
        // Generic recommendations when no specific catalog entry
        lines.push('### Testing Strategy for Hypermedia Projects');
        lines.push('');
        lines.push('Hypermedia projects use **integration tests over unit tests** — verify HTTP endpoints return correct HTML fragments.');
        lines.push('- Test: status 200, Content-Type: text/html, expected DOM elements in response body');
        lines.push('- Validate: HX-Request header handling, graceful degradation without JS');
        lines.push('');
    }
    return lines;
}
//# sourceMappingURL=hypermedia-catalog.js.map