import { getExtension, EXTENSION_TO_TYPE } from '../utils.js';
import { parseDocx } from './docx.js';
import { parsePdf } from './pdf.js';
import { parseXlsx } from './xlsx.js';
import { parsePptx } from './pptx.js';
import { parseOdf } from './odf.js';
const PARSER_MAP = {
    docx: parseDocx,
    pdf: parsePdf,
    xlsx: parseXlsx,
    pptx: parsePptx,
    odt: parseOdf,
    ods: parseOdf,
    odp: parseOdf,
};
export function suggestFromError(error, ext) {
    const msg = error.message.toLowerCase();
    if (msg.includes('invalid zip') || msg.includes('not a valid zip') || msg.includes('end of central directory')) {
        return `File may be corrupted or not a valid .${ext} file.`;
    }
    if (msg.includes('password') || msg.includes('encrypted')) {
        return 'Document is password-protected. Remove the password and retry.';
    }
    // Must precede the ENOENT branch: a heavy-dep load failure (e.g. pdf-parse)
    // can surface as an ENOENT for a path inside the package.
    if (/cannot find (module|package)|ERR_MODULE_NOT_FOUND|failed to load/i.test(error.message)) {
        return 'A required dependency failed to load. Try reinstalling (npm install).';
    }
    if (msg.includes('enoent') || msg.includes('no such file')) {
        return 'File not found. Check the file path.';
    }
    if (msg.includes('eacces') || msg.includes('permission denied')) {
        return 'Permission denied. Check file permissions.';
    }
    if (msg.includes('out of memory') || msg.includes('allocation failed')) {
        return 'File is too large to parse. Try increasing the memory limit or use --large-file-limit.';
    }
    if (msg.includes('content.xml')) {
        return `File may be corrupted or not a valid .${ext} document (missing content.xml).`;
    }
    return undefined;
}
function failureResult(filePath, size, ext, suggestion) {
    return {
        filePath,
        size,
        success: false,
        fileType: EXTENSION_TO_TYPE[ext] || ext.toUpperCase(),
        metrics: null,
        suggestion,
    };
}
export async function parseFile(filePath, size) {
    const ext = getExtension(filePath);
    const parser = PARSER_MAP[ext];
    if (!parser) {
        return failureResult(filePath, size, ext);
    }
    try {
        const result = await parser(filePath);
        return {
            filePath,
            size,
            success: true,
            fileType: result.fileType,
            metrics: result.metrics,
            confidence: result.confidence,
        };
    }
    catch (err) {
        const error = err instanceof Error ? err : new Error(String(err));
        return failureResult(filePath, size, ext, suggestFromError(error, ext));
    }
}
export async function parseFiles(files, concurrency = 10, onProgress) {
    const results = [];
    for (let i = 0; i < files.length; i += concurrency) {
        const batch = files.slice(i, i + concurrency);
        const batchResults = await Promise.allSettled(batch.map(f => parseFile(f.path, f.size)));
        for (let j = 0; j < batchResults.length; j++) {
            const r = batchResults[j];
            results.push(r.status === 'fulfilled'
                ? r.value
                : {
                    filePath: batch[j]?.path ?? '',
                    size: 0,
                    success: false,
                    fileType: 'Unreadable',
                    metrics: null,
                });
            if (onProgress)
                onProgress(1, batch[j]?.path);
        }
    }
    return results;
}
//# sourceMappingURL=index.js.map