import { documentToMarkdown } from '../markdown/convert.js';
import { estimateTokens } from '../inspect/shared.js';
import { extractFromMarkdown } from '../structure/index.js';
import { discoverDocuments } from './discover.js';
import { inspectDocument } from './inspect.js';
import { countWords } from '../utils.js';
export async function inspectWorkspaceDocuments(rootDir, options) {
    const maxFiles = options?.maxFiles ?? 50;
    const includeMarkdown = options?.includeMarkdown ?? false;
    const discovered = await discoverDocuments(rootDir, {
        excludeDir: options?.excludeDir,
        noGitignore: options?.noGitignore,
        ignorePatterns: options?.ignorePatterns,
        overlayIgnorePatterns: options?.overlayIgnorePatterns,
    });
    const results = [];
    for (const doc of discovered.slice(0, maxFiles)) {
        const { summary } = await inspectDocumentSummary(doc.filePath, doc.relativePath, doc.format, { includeMarkdown });
        results.push(summary);
    }
    return results;
}
function createFallbackSummary(filePath, relativePath, format) {
    return {
        filePath,
        relativePath,
        format,
        words: 0,
        pages: 0,
        tokenEstimate: 0,
        headingCount: 0,
        topLevelSections: [],
    };
}
export async function inspectDocumentSummary(filePath, relativePath, format, options) {
    const includeMarkdown = options?.includeMarkdown ?? false;
    const convertToMarkdown = options?.convertToMarkdown ?? documentToMarkdown;
    // Try markdown conversion first (works for all formats)
    let markdownContent = null;
    let lastError;
    try {
        markdownContent = await convertToMarkdown(filePath);
    }
    catch (err) {
        const error = err;
        lastError = error.message;
    }
    // For formats supported by inspectDocument, get rich metadata
    if (format === 'docx' || format === 'pdf' || format === 'odt') {
        try {
            const inspection = await inspectDocument(filePath, {
                sampleParagraphs: 5,
                includeStructure: true,
                markdown: markdownContent,
            });
            return {
                summary: {
                    filePath,
                    relativePath,
                    format,
                    title: inspection.properties.title,
                    words: inspection.contentStats.words,
                    pages: inspection.contentStats.pages,
                    tokenEstimate: inspection.fullTokenEstimate,
                    headingCount: inspection.structure?.headingCount ?? 0,
                    topLevelSections: inspection.structure?.topLevelSections ?? [],
                    markdownContent: includeMarkdown ? (markdownContent ?? undefined) : undefined,
                },
                error: lastError,
                markdownContent: markdownContent ?? undefined,
            };
        }
        catch (err) {
            const error = err;
            lastError = error.message;
        }
    }
    // For other formats (pptx, xlsx, ods, odp), derive from markdown
    if (markdownContent) {
        const structure = extractFromMarkdown(markdownContent);
        const words = countWords(markdownContent);
        return {
            summary: {
                filePath,
                relativePath,
                format,
                words,
                pages: 0,
                tokenEstimate: estimateTokens(markdownContent),
                headingCount: structure.totalNodes,
                topLevelSections: structure.rootNodes.map(n => n.title),
                markdownContent: includeMarkdown ? markdownContent : undefined,
            },
            error: lastError,
            markdownContent,
        };
    }
    // Minimal fallback
    return {
        summary: createFallbackSummary(filePath, relativePath, format),
        error: lastError,
    };
}
//# sourceMappingURL=batch.js.map