import fg from 'fast-glob';
import { stat } from 'node:fs/promises';
import path from 'node:path';
import { DEFAULT_EXCLUDE_DIRS, buildExcludeDirGlobs, createIgnoreMatcher, normalizeRelativePath } from '../utils.js';
export const DOCUMENT_EXTENSIONS = ['docx', 'pdf', 'pptx', 'xlsx', 'odt', 'ods', 'odp'];
export const PROSE_DOCUMENT_EXTENSIONS = ['md', 'markdown', 'mdx', 'txt', 'rst', 'adoc', 'asciidoc'];
export const DATA_DOCUMENT_EXTENSIONS = ['yaml', 'yml', 'json', 'jsonc', 'toml'];
function getDiscoverableExtensions(includeDataFiles) {
    return includeDataFiles
        ? [...DOCUMENT_EXTENSIONS, ...PROSE_DOCUMENT_EXTENSIONS, ...DATA_DOCUMENT_EXTENSIONS]
        : [...DOCUMENT_EXTENSIONS, ...PROSE_DOCUMENT_EXTENSIONS];
}
function isDocumentFormat(ext, extensions) {
    return extensions.includes(ext);
}
export async function discoverDocumentSet(rootDir, options) {
    const resolvedRoot = path.resolve(rootDir);
    const excludeDir = options?.excludeDir ?? [...DEFAULT_EXCLUDE_DIRS];
    const maxSizeBytes = options?.maxSizeBytes ?? 50 * 1024 * 1024; // 50MB
    const ignoreMatcher = await createIgnoreMatcher(resolvedRoot, {
        excludeDir,
        noGitignore: options?.noGitignore,
        ignorePatterns: options?.ignorePatterns,
        overlayIgnorePatterns: options?.overlayIgnorePatterns,
    });
    const extensions = getDiscoverableExtensions(options?.includeDataFiles ?? false);
    const pattern = `**/*.{${extensions.join(',')}}`;
    const discovered = await fg(pattern, {
        cwd: resolvedRoot,
        absolute: true,
        onlyFiles: true,
        dot: false,
        followSymbolicLinks: false,
        ignore: buildExcludeDirGlobs(excludeDir),
    });
    const documents = [];
    const skipped = [];
    for (const filePath of discovered.sort()) {
        try {
            const fileStat = await stat(filePath);
            if (!fileStat.isFile())
                continue;
            if (fileStat.size > maxSizeBytes) {
                skipped.push({
                    path: filePath,
                    reason: `Exceeds ${Math.round(maxSizeBytes / 1024 / 1024)}MB limit`,
                    size: fileStat.size,
                });
                continue;
            }
            const ext = path.extname(filePath).toLowerCase().replace('.', '');
            if (!isDocumentFormat(ext, extensions))
                continue;
            const relativePath = normalizeRelativePath(path.relative(resolvedRoot, filePath));
            if (ignoreMatcher.ignores(relativePath))
                continue;
            documents.push({
                filePath,
                relativePath,
                format: ext,
                sizeBytes: fileStat.size,
            });
        }
        catch (err) {
            const error = err;
            skipped.push({
                path: filePath,
                reason: error.code === 'EACCES' ? 'Permission denied' : error.message,
                size: 0,
            });
        }
    }
    return { documents, skipped };
}
export async function discoverDocuments(rootDir, options) {
    return (await discoverDocumentSet(rootDir, options)).documents;
}
//# sourceMappingURL=discover.js.map