import { checkRegistryHealth, discoverDocsUrl, } from '../engine/web-fetcher/registry-auto-discovery.js';
import { loadDocsRegistry } from '../engine/web-fetcher/registry-loader.js';
/** Flatten all docs-registry categories into a single { key: base-url } map. */
async function flattenRegistryUrls() {
    const registry = await loadDocsRegistry();
    const result = new Map();
    for (const [key, entry] of Object.entries(registry.frameworks)) {
        result.set(key, entry.base);
    }
    for (const [key, entry] of Object.entries(registry.languages)) {
        result.set(key, entry.base);
    }
    for (const [key, entry] of Object.entries(registry.services)) {
        result.set(key, entry.base);
    }
    return result;
}
/** Handle validate_docs_registry tool call. */
export async function handleValidateDocsRegistry(_input) {
    const urlMap = await flattenRegistryUrls();
    const entries = {};
    for (const [key, base] of urlMap.entries()) {
        entries[key] = { base };
    }
    const total = urlMap.size;
    const health = await checkRegistryHealth(entries);
    const lines = [
        `Docs registry health check complete.`,
        `Total entries: ${total}`,
        `Healthy: ${health.healthy.length}`,
        `Slow (>2s): ${health.slow.length}`,
        `Broken: ${health.broken.length}`,
    ];
    if (health.broken.length > 0) {
        lines.push(`\nBroken entries:\n${health.broken.map((b) => `  - ${b}`).join('\n')}`);
    }
    if (health.slow.length > 0) {
        lines.push(`\nSlow entries:\n${health.slow.map((s) => `  - ${s}`).join('\n')}`);
    }
    return {
        content: [{ type: 'text', text: lines.join('\n') }],
        total,
        healthy: health.healthy.length,
        broken: health.broken.map((key) => ({ key, url: urlMap.get(key) ?? '' })),
        slowMs: health.slow.map((key) => ({ key, url: urlMap.get(key) ?? '' })),
    };
}
/** Handle discover_docs_url tool call. */
export async function handleDiscoverDocsUrl(input) {
    const { tech } = input;
    const entry = await discoverDocsUrl(tech);
    if (!entry) {
        return {
            content: [
                {
                    type: 'text',
                    text: `No docs URL found for "${tech}" in npm, PyPI, or crates.io registries.`,
                },
            ],
        };
    }
    const verifiedLabel = entry.verified ? 'verified alive' : 'unverified';
    const text = [
        `Found docs for "${tech}" via ${entry.source} (${verifiedLabel}):`,
        `  URL: ${entry.docsUrl}`,
        `  Discovered at: ${entry.discoveredAt}`,
    ].join('\n');
    return {
        content: [{ type: 'text', text }],
        entry,
    };
}
//# sourceMappingURL=docs-registry-handler.js.map