import { enrichWithAnalysis } from "./enrichment.js";
import { rankByInterests } from "./ranking.js";
import { getSource as defaultGetSource } from "./sources/index.js";
import { downloadCover as defaultDownloadCover, coversDir as defaultCoversDir, } from "./covers.js";
export async function collectPlatform(platform, deps) {
    const source = deps.getSource(platform);
    const raws = await source.collect({ limit: deps.limit ?? 20 });
    if (raws.length < 5) {
        return {
            platform, items: [], collectedAt: new Date().toISOString(),
            pipelineStatus: "failed", errors: [`source returned ${raws.length} items, need >=5`],
            validation: { passed: false, issues: [] },
        };
    }
    const enriched = await enrichWithAnalysis(raws, platform, {
        runCli: deps.runCli, maxRetries: deps.maxRetries ?? 2,
    });
    if (enriched.pipelineStatus !== "ok")
        return enriched;
    // Download covers; mutate cachedPath in-place. Safe because items are
    // freshly allocated by enrichWithAnalysis on each call — never cached or
    // shared. If enrichment ever starts memoizing return values, revisit.
    const dir = deps.coversDir(platform);
    await Promise.all(enriched.items.map(async (item) => {
        if (!item.cover.url)
            return;
        const path = await deps.downloadCover(item.cover.url, dir, item.id);
        if (path)
            item.cover.cachedPath = path;
    }));
    // S14 — rank by fit-to-channel before returning so the persisted order is
    // already personalized. No-op when interests is empty/undefined.
    const interests = deps.interests ?? [];
    if (interests.length > 0) {
        enriched.items = rankByInterests(enriched.items, interests);
    }
    return enriched;
}
export const defaultPipelineDeps = (runCli, interests = []) => ({
    getSource: defaultGetSource,
    runCli,
    downloadCover: defaultDownloadCover,
    coversDir: defaultCoversDir,
    interests,
});
//# sourceMappingURL=pipeline.js.map