/**
 * Trends extension for the agent context aggregator.
 *
 * Wraps the existing src/trends/sources/ scrapers behind a single
 * getTrends() facade so the bridge and CLI don't have to know which
 * platform implementations exist or how to combine them.
 *
 * H0.4 ships a minimal facade — scrapers are platform-by-platform and
 * many require external API keys / browser sessions to actually fetch
 * live data. When a scraper isn't available, we return an empty array
 * rather than throwing, so context endpoint stays robust.
 */
import { getSource } from "../trends/sources/index.js";
function hasFetch(source) {
    return (typeof source === "object" &&
        source !== null &&
        typeof source.fetch === "function");
}
const SUPPORTED_PLATFORMS = [
    "douyin",
    "bilibili",
    "youtube",
    "xiaohongshu",
];
export async function getTrends(opts = {}) {
    const limit = opts.limit ?? 10;
    const requested = opts.platforms ?? SUPPORTED_PLATFORMS;
    const items = [];
    for (const platform of requested) {
        try {
            const source = getSource(platform);
            if (!hasFetch(source)) {
                continue;
            }
            // Each source's signature varies — best-effort invocation; on any
            // failure we just skip the platform.
            const rawItems = (await source.fetch(opts.topic).catch(() => []));
            for (const r of rawItems.slice(0, limit)) {
                if (!r.title)
                    continue;
                items.push({
                    platform,
                    title: r.title,
                    url: r.url,
                    score: r.score,
                });
            }
        }
        catch {
            // skip platform
        }
    }
    return {
        generatedAt: new Date().toISOString(),
        platforms: requested,
        items,
    };
}
//# sourceMappingURL=trends.js.map