import { mergeTripleLists } from './triple-chunk-merge.js';
import { splitTextIntoChunks } from './triple-text-chunker.js';
function chunkSucceeded(result) {
    return (result.triples.length > 0 ||
        result.extractionInfo.failureReason === undefined);
}
function failureMessageFromResult(result) {
    const raw = result.extractionInfo.rawLLMOutput;
    if (typeof raw === 'string' && raw.length > 0) {
        return raw;
    }
    return undefined;
}
export class TriplePipelineOrchestrator {
    constructor(_config) {
        void _config;
    }
    async run(params, extract) {
        void params.extractionOptions;
        const trimmed = params.text.trim();
        if (trimmed === '') {
            return { triples: [], chunkErrors: [], chunksProcessed: 0 };
        }
        const chunks = splitTextIntoChunks(trimmed, params.chunkSize, params.chunkOverlap);
        const chunkTriples = [];
        const chunkErrors = [];
        for (let i = 0; i < chunks.length; i++) {
            const chunkText = chunks[i];
            try {
                const result = await extract(chunkText);
                if (chunkSucceeded(result)) {
                    chunkTriples.push(result.triples);
                }
                else {
                    chunkTriples.push([]);
                    const reason = result.extractionInfo.failureReason ?? 'llm_api_error';
                    chunkErrors.push({
                        chunkIndex: i,
                        reason,
                        message: failureMessageFromResult(result),
                    });
                }
            }
            catch (err) {
                chunkTriples.push([]);
                chunkErrors.push({
                    chunkIndex: i,
                    reason: 'llm_api_error',
                    message: err instanceof Error ? err.message : String(err),
                });
            }
        }
        const merged = mergeTripleLists(chunkTriples);
        return {
            triples: merged,
            chunkErrors,
            chunksProcessed: chunks.length,
        };
    }
}
//# sourceMappingURL=triple-pipeline-orchestrator.js.map