// engine/ecosystem-absorber/ecosystem-watcher.ts — Orchestrates crawling all enabled sources
import { crawlDocsSource, crawlGitHubSource } from './platform-crawler.js';
import { readCache, writeCache, mergeCapabilities, isCacheStale, markCrawled, } from './knowledge-cache.js';
/**
 * Crawl a single source based on its type. Never throws.
 */
async function crawlSource(source) {
    if (source.type === 'github') {
        return crawlGitHubSource(source);
    }
    return crawlDocsSource(source);
}
/**
 * Orchestrate crawling all enabled sources, respecting TTL.
 * Returns a summary of what was crawled and discovered.
 */
export async function runEcosystemWatcher(sources, forceRefresh = false) {
    const cache = readCache();
    const crawledSources = [];
    const skippedSources = [];
    let allNew = [];
    const enabled = sources.filter((s) => s.enabled);
    for (const source of enabled) {
        const stale = forceRefresh || isCacheStale(cache.lastCrawled, source.id, source.crawlIntervalHours);
        if (!stale) {
            skippedSources.push(source.id);
            continue;
        }
        const discovered = await crawlSource(source);
        allNew = allNew.concat(discovered);
        crawledSources.push(source.id);
        cache.lastCrawled = markCrawled(cache.lastCrawled, source.id);
    }
    const merged = mergeCapabilities(cache.capabilities, allNew);
    const updatedCache = {
        ...cache,
        capabilities: merged,
    };
    if (crawledSources.length > 0) {
        writeCache(updatedCache);
    }
    return {
        crawledSources,
        skippedSources,
        newCapabilities: allNew.length,
        totalCapabilities: merged.length,
        lastCrawled: updatedCache.lastCrawled,
        capabilities: merged,
    };
}
/**
 * Load cached capabilities without re-crawling.
 */
export function loadCachedCapabilities() {
    return readCache().capabilities;
}
//# sourceMappingURL=ecosystem-watcher.js.map