/**
 * Shared filesystem walker — used by both grep-only and codegraph backends
 * for `listFiles()`. The richer backends index *symbols* in their preferred
 * languages, but the file inventory itself comes from a plain fs walk so
 * adapters can find arbitrary file types (schema.prisma, package.json,
 * config files, etc.).
 */
import { readdirSync, statSync } from 'fs';
import { join, extname } from 'path';
/** Common dirs to exclude from any walk. */
const EXCLUDE_DIRS = new Set([
    'node_modules', '.git', '.svn', '.hg',
    'dist', 'build', 'out', 'target', '__pycache__',
    '.codegraph', '.gitnexus',
    'coverage', '.next', '.nuxt',
]);
/** Walk dir recursively, returning absolute file paths. */
export function walkSourceTree(dir) {
    const out = [];
    const stack = [dir];
    while (stack.length > 0) {
        const cur = stack.pop();
        let entries;
        try {
            entries = readdirSync(cur);
        }
        catch {
            continue;
        }
        for (const entry of entries) {
            if (EXCLUDE_DIRS.has(entry))
                continue;
            // Skip dotfiles + hidden dirs (except known ones we'd want to expose later)
            if (entry.startsWith('.'))
                continue;
            const full = join(cur, entry);
            let st;
            try {
                st = statSync(full);
            }
            catch {
                continue;
            }
            if (st.isDirectory()) {
                stack.push(full);
            }
            else if (st.isFile()) {
                out.push(full);
            }
        }
    }
    return out;
}
/** Map common file extensions to language identifiers used by the indexer. */
export function detectLanguage(filePath) {
    const ext = extname(filePath).toLowerCase();
    switch (ext) {
        case '.ts':
        case '.tsx':
            return 'typescript';
        case '.js':
        case '.jsx':
        case '.mjs':
        case '.cjs':
            return 'javascript';
        case '.py':
            return 'python';
        case '.go':
            return 'go';
        case '.rs':
            return 'rust';
        case '.java':
            return 'java';
        case '.kt':
            return 'kotlin';
        case '.swift':
            return 'swift';
        case '.cs':
            return 'csharp';
        case '.rb':
            return 'ruby';
        case '.php':
            return 'php';
        case '.c':
        case '.h':
            return 'c';
        case '.cpp':
        case '.cc':
        case '.hpp':
            return 'cpp';
        default:
            return null;
    }
}
/** Convert a minimal glob (`**`, `*`, `?`) to a regex for file path matching. */
export function globToRegex(glob) {
    const escaped = glob.replace(/[.+^${}()|\\]/g, '\\$&');
    const pattern = escaped
        .replace(/\*\*/g, '__DOUBLESTAR__')
        .replace(/\*/g, '[^/]*')
        .replace(/__DOUBLESTAR__/g, '.*')
        .replace(/\?/g, '[^/]');
    return new RegExp(pattern + '$');
}
//# sourceMappingURL=walk.js.map