import path from 'node:path';
import { readFile } from 'node:fs/promises';
import { getExtension } from '../utils.js';
import { estimateTokens } from '../inspect/shared.js';
import { documentToMarkdown } from '../markdown/convert.js';
import { extractFromMarkdown } from '../structure/index.js';
import { inspectDocx } from './inspect-docx.js';
import { inspectPdf } from './inspect-pdf.js';
import { inspectOdt } from './inspect-odt.js';
const SUPPORTED_EXTENSIONS = new Set(['docx', 'pdf', 'odt']);
export async function inspectDocument(filePath, options) {
    const resolvedPath = path.resolve(filePath);
    const ext = getExtension(resolvedPath);
    if (!SUPPORTED_EXTENSIONS.has(ext)) {
        throw new Error(`Unsupported document format: .${ext || '(none)'} (expected .docx, .pdf, or .odt)`);
    }
    const buffer = await readFile(resolvedPath);
    const format = ext;
    let result;
    switch (format) {
        case 'docx':
            result = await inspectDocx(resolvedPath, buffer, options.sampleParagraphs);
            break;
        case 'pdf':
            result = await inspectPdf(resolvedPath, buffer, options.sampleParagraphs);
            break;
        case 'odt':
            result = await inspectOdt(resolvedPath, buffer, options.sampleParagraphs);
            break;
    }
    // Structure extraction
    let structure = null;
    if (options.includeStructure && options.markdown !== null) {
        try {
            const markdown = options.markdown ?? (await documentToMarkdown(resolvedPath));
            if (markdown) {
                const docStructure = extractFromMarkdown(markdown);
                if (docStructure.totalNodes > 0) {
                    const topLevelSections = docStructure.rootNodes.map(n => n.title);
                    structure = {
                        headingCount: docStructure.totalNodes,
                        maxDepth: docStructure.maxDepth,
                        topLevelSections,
                        tree: docStructure.rootNodes,
                    };
                }
            }
        }
        catch {
            /* structure extraction is best-effort */
        }
    }
    // Token estimates
    const fullText = [result.preview.paragraphs.map(p => p.text).join(' ')].join(' ');
    const fullTokenEstimate = estimateTokens(result.contentStats.characters);
    const previewTokenEstimate = estimateTokens(result.preview.paragraphs.reduce((sum, p) => sum + p.text.length, 0));
    return {
        file: resolvedPath,
        format,
        size: buffer.length,
        properties: result.properties,
        riskFlags: result.riskFlags,
        contentStats: result.contentStats,
        structure,
        contentPreview: result.preview,
        fullTokenEstimate,
        previewTokenEstimate,
    };
}
//# sourceMappingURL=inspect.js.map