"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.initRetrieval = initRetrieval;
exports.retrieve = retrieve;
exports.search = search;
const bm25_1 = require("./bm25");
const corpus_1 = require("./corpus");
let index = null;
async function initRetrieval() {
    if (index)
        return;
    const chunks = await (0, corpus_1.loadCorpusFromUrlsFile)();
    const bm25 = new bm25_1.BM25Index();
    bm25.addDocuments(chunks);
    index = bm25;
    console.log(`✅ BM25 ready. Indexed ${chunks.length} chunks.`);
}
// If you want the same “state shape” as before:
async function retrieve(state) {
    if (!index)
        throw new Error("Retrieval not initialized. Call initRetrieval() first.");
    const results = index.search(state.question, 5);
    return { documents: results }; // documents now include score + text + source
}
// Or a plain function:
async function search(query, k = 5) {
    if (!index)
        throw new Error("Retrieval not initialized. Call initRetrieval() first.");
    return index.search(query, k);
}
