import { z } from 'zod';
import { countWords, wrapDependencyLoadError } from '../utils.js';
const MammothResultSchema = z.object({
    value: z.string(),
});
// Lazy-loaded so importing this module never pulls in mammoth (CJS default = the lib).
let mammothModule;
async function loadMammoth() {
    mammothModule ??= import('mammoth')
        .then(mod => mod.default)
        .catch((error) => wrapDependencyLoadError('mammoth', error));
    return mammothModule;
}
export async function parseDocx(filePath) {
    const mammoth = await loadMammoth();
    const result = MammothResultSchema.parse(await mammoth.extractRawText({ path: filePath }));
    const text = result.value || '';
    const words = countWords(text);
    const paragraphs = text.split(/\n\n+/).filter(s => s.trim().length > 0).length;
    const pages = Math.max(1, Math.ceil(words / 250));
    return {
        fileType: 'Word',
        metrics: { words, pages, paragraphs },
        confidence: {
            words: 'exact',
            pages: 'estimated',
            paragraphs: 'exact',
        },
    };
}
//# sourceMappingURL=docx.js.map