// Planu — Web Fetcher: Docs registry loading + lookup
import { readFile } from 'node:fs/promises';
let docsRegistryCache = null;
let registryPath = null;
function getRegistryPath() {
    if (registryPath) {
        return registryPath;
    }
    registryPath = new URL('../../config/docs-registry.json', import.meta.url).pathname;
    return registryPath;
}
export async function loadDocsRegistry() {
    if (docsRegistryCache) {
        return docsRegistryCache;
    }
    try {
        const configPath = new URL('../../config/docs-registry.json', import.meta.url);
        const content = await readFile(configPath, 'utf-8');
        docsRegistryCache = JSON.parse(content);
        return docsRegistryCache;
    }
    catch {
        return { frameworks: {}, languages: {}, services: {} };
    }
}
export function resetRegistryCache() {
    docsRegistryCache = null;
}
export function setDocsRegistryCache(registry) {
    docsRegistryCache = registry;
}
export function getStoredRegistryPath() {
    return getRegistryPath();
}
export function findDocsEntry(key, registry) {
    const normalized = key.toLowerCase().replace(/\s+/g, '-');
    if (registry.frameworks[normalized]) {
        return registry.frameworks[normalized];
    }
    if (registry.languages[normalized]) {
        return registry.languages[normalized];
    }
    if (registry.services[normalized]) {
        return registry.services[normalized];
    }
    // Partial match on frameworks
    for (const [k, v] of Object.entries(registry.frameworks)) {
        if (k.includes(normalized) || normalized.includes(k)) {
            return v;
        }
    }
    // Partial match on languages
    for (const [k, v] of Object.entries(registry.languages)) {
        if (k.includes(normalized) || normalized.includes(k)) {
            return v;
        }
    }
    return null;
}
//# sourceMappingURL=registry-loader.js.map