// capsuleCorpus.ts — synchronous full-corpus reader for QI ingestion (Epic #710, Issue #717).
//
// Reads every indexed document text for a capsule from the `document_texts` table. The QI
// ingestion pipeline uses this to build atoms directly from the corpus (no retrieval query),
// so knowledge workers can generate test cases from a connected Local Knowledge connector
// without needing a search query.
//
// SYNC: keiko-local-knowledge uses node:sqlite (synchronous API) throughout; this function
// follows the same pattern used in capsule-lifecycle.ts (_internal.db.prepare().all()).
import { getCapsuleSet } from "../capsule-set-lifecycle.js";
const SELECT_DOCUMENT_TEXTS_SQL = "SELECT document_id, normalized_text FROM document_texts WHERE capsule_id = ? ORDER BY document_id";
/**
 * Returns the full normalized text for every indexed document in a capsule, ordered by
 * document_id for deterministic output. Documents with empty text are skipped. Sync — reads the
 * node:sqlite handle directly like the capsule-lifecycle queries.
 */
export function listCapsuleDocumentTexts(store, capsuleId) {
    const rows = store._internal.db.prepare(SELECT_DOCUMENT_TEXTS_SQL).all(String(capsuleId));
    const result = [];
    for (const row of rows) {
        const documentId = row.document_id;
        const text = row.normalized_text;
        if (typeof documentId === "string" && typeof text === "string" && text.length > 0) {
            result.push({ documentId, text });
        }
    }
    return result;
}
/**
 * Returns the full corpus text for every member capsule of a capsule-set, concatenated in
 * membership order (ordinal). Each member's documents stay ordered by document_id. An unknown
 * capsule-set yields an empty list, which the QI ingestion layer maps to QI_CAPSULE_UNAVAILABLE.
 * Capsule-sets are logical compositions (no own documents), so this fans out over the members and
 * reuses {@link listCapsuleDocumentTexts} — it does not touch a new storage path (Epic #710,
 * Issue #716/#717). Sync, like the single-capsule reader.
 */
export function listCapsuleSetDocumentTexts(store, capsuleSetId) {
    const set = getCapsuleSet(store, capsuleSetId);
    if (set === undefined)
        return [];
    const result = [];
    for (const capsuleId of set.capsuleIds) {
        for (const doc of listCapsuleDocumentTexts(store, capsuleId)) {
            result.push(doc);
        }
    }
    return result;
}
