import { readFile } from 'node:fs/promises';
import { z } from 'zod';
import { countWords, PDF_LOG_SUPPRESSED_PREFIXES, suppressConsoleLogWhile, wrapDependencyLoadError } from '../utils.js';
const PdfParseResultSchema = z.object({
    text: z.string(),
    numpages: z.number(),
});
// Lazy-loaded so importing this module never pulls in pdf-parse (CJS default = the parse fn).
let pdfParseModule;
async function loadPdfParse() {
    pdfParseModule ??= import('pdf-parse')
        .then(mod => mod.default)
        .catch((error) => wrapDependencyLoadError('pdf-parse', error));
    return pdfParseModule;
}
export async function parsePdf(filePath) {
    const buffer = await readFile(filePath);
    const pdf = await loadPdfParse();
    // Suppress noisy pdf.js warnings (font parsing, deprecated API, etc.) —
    // the shared helper is reference-counted, so concurrent PDFs are safe.
    const data = await suppressConsoleLogWhile(PDF_LOG_SUPPRESSED_PREFIXES, async () => PdfParseResultSchema.parse(await pdf(buffer)));
    const words = countWords(data.text);
    return {
        fileType: 'PDF',
        metrics: { words, pages: data.numpages },
        confidence: {
            words: 'estimated',
            pages: 'exact',
        },
    };
}
//# sourceMappingURL=pdf.js.map