/**
 * Document Summarization Service
 *
 * Generates structured summaries from chunk data.
 * Uses chunk content, metadata, and document context.
 * No external API calls - pure algorithmic extraction.
 *
 * @module clm/summarization
 */
// =============================================================================
// DOCUMENT SUMMARIZATION
// =============================================================================
/**
 * Generate summary from document chunks.
 * Pure algorithmic extraction - no API calls.
 */
export function summarizeDocument(chunks) {
    const sections = new Set();
    const contentTypes = new Set();
    let wordCount = 0;
    const pages = new Set();
    for (const chunk of chunks) {
        if (chunk.heading_context)
            sections.add(chunk.heading_context);
        if (chunk.section_path)
            sections.add(chunk.section_path);
        if (chunk.page_number !== null && chunk.page_number !== undefined) {
            pages.add(chunk.page_number);
        }
        if (chunk.content_types) {
            try {
                const parsed = JSON.parse(chunk.content_types);
                if (Array.isArray(parsed)) {
                    for (const ct of parsed) {
                        if (typeof ct === 'string' && ct.trim())
                            contentTypes.add(ct.trim());
                    }
                }
            }
            catch {
                // Fallback: if not valid JSON, treat as comma-separated
                for (const ct of chunk.content_types.split(',')) {
                    const trimmed = ct.trim();
                    if (trimmed)
                        contentTypes.add(trimmed);
                }
            }
        }
        wordCount += chunk.text.split(/\s+/).filter((w) => w.length > 0).length;
    }
    // Build summary from first chunks of each section
    const summaryParts = [];
    const seenSections = new Set();
    for (const chunk of chunks.slice(0, 20)) {
        const section = chunk.heading_context ?? chunk.section_path ?? 'General';
        if (!seenSections.has(section)) {
            seenSections.add(section);
            // Take first 200 chars of the section
            const preview = chunk.text.substring(0, 200).replace(/\s+/g, ' ').trim();
            summaryParts.push(`[${section}]: ${preview}...`);
        }
    }
    return {
        document_id: '', // Set by caller
        total_chunks: chunks.length,
        total_pages: pages.size,
        key_sections: Array.from(sections).slice(0, 20),
        content_types: Array.from(contentTypes),
        word_count: wordCount,
        summary_text: summaryParts.join('\n\n'),
    };
}
//# sourceMappingURL=summarization.js.map