// tools/suggest-tooling/local-first-catalog.ts — SPEC-047
// Builds local-first CRDT, storage, and sync tooling recommendation lines for suggest_tooling.
import { readFile } from 'node:fs/promises';
let configCache = null;
/** Reset in-memory config cache (for testing). */
export function resetLocalFirstCatalogCache() {
    configCache = null;
}
async function loadConfig() {
    if (configCache) {
        return configCache;
    }
    try {
        const configPath = new URL('../../config/local-first-tools.json', import.meta.url);
        const content = await readFile(configPath, 'utf-8');
        configCache = JSON.parse(content);
        return configCache;
    }
    catch {
        return { crdtLibraries: [], storageEngines: [], syncBackends: [] };
    }
}
function formatToolEntry(tool) {
    const lines = [];
    lines.push(`**${tool.name}** — ${tool.description ?? tool.name}`);
    /* v8 ignore next 3 */
    if (tool.docsUrl) {
        lines.push(`- Docs: ${tool.docsUrl}`);
    }
    /* v8 ignore next 3 */
    if (tool.status === 'beta') {
        lines.push('- _Status: beta — evaluate stability for production use_');
    }
    lines.push('');
    return lines;
}
function getPlatformTag(framework, stack) {
    const stackLower = stack.map((s) => s.toLowerCase()).join(' ');
    const fw = (framework ?? '').toLowerCase();
    if (fw.includes('react-native') || fw.includes('expo') || stackLower.includes('react-native')) {
        return 'react-native';
    }
    if (fw.includes('flutter') || stackLower.includes('flutter')) {
        return 'flutter';
    }
    if (fw.includes('tauri') || fw.includes('electron') || stackLower.includes('desktop')) {
        return 'node';
    }
    return 'browser';
}
/**
 * Build local-first tooling recommendation lines.
 * Only emits when localFirst detection is present and isLocalFirst is true.
 */
export async function buildLocalFirstToolingLines(knowledge) {
    const detection = knowledge.localFirst;
    if (!detection?.isLocalFirst) {
        return [];
    }
    const config = await loadConfig();
    const platform = getPlatformTag(knowledge.framework, knowledge.stack);
    const lines = ['', '## Local-First Architecture', ''];
    // Detected signals summary
    if (detection.localFirstSignals.length > 0) {
        lines.push('**Signals detected:**');
        for (const signal of detection.localFirstSignals) {
            lines.push(`- ${signal}`);
        }
        lines.push('');
    }
    // CRDT Libraries section
    const crdtTools = config.crdtLibraries.filter((t) => t.platforms.includes(platform) || t.platforms.includes('node'));
    if (crdtTools.length > 0) {
        lines.push('### CRDT Libraries', '');
        for (const tool of crdtTools) {
            lines.push(...formatToolEntry(tool));
        }
    }
    // Storage Engines section
    const storageTools = config.storageEngines.filter((t) => t.platforms.includes(platform));
    if (storageTools.length > 0) {
        lines.push('### Local Storage Engines', '');
        for (const tool of storageTools) {
            lines.push(...formatToolEntry(tool));
        }
    }
    // Sync Backends section
    const syncTools = config.syncBackends.filter((t) => t.platforms.includes(platform));
    if (syncTools.length > 0) {
        lines.push('### Sync Backends', '');
        for (const tool of syncTools) {
            lines.push(...formatToolEntry(tool));
        }
    }
    // Anti-patterns warning
    lines.push('### Anti-patterns to Avoid', '');
    lines.push('- **Polling HTTP** — use WebSocket/SSE instead of `setInterval(() => fetch(...))`');
    lines.push('- **Full-state sync** — send only diff/operations, not the complete document');
    lines.push('- **Silent last-write-wins** — expose conflicts to users when data loss is unacceptable');
    lines.push('');
    return lines;
}
//# sourceMappingURL=local-first-catalog.js.map