import * as path from 'node:path';
import { readFileSync } from 'node:fs';
export function makeSourceReader(workspaceRoot, project) {
    if (project) {
        const byRel = new Map();
        for (const sf of project.getSourceFiles()) {
            const rel = path.relative(workspaceRoot, sf.getFilePath());
            // Use POSIX separator so reads work the same across platforms.
            byRel.set(rel.split(path.sep).join('/'), sf.getFullText());
        }
        return (rel) => byRel.get(rel.split(path.sep).join('/')) ?? readFromDisk(workspaceRoot, rel);
    }
    return (rel) => readFromDisk(workspaceRoot, rel);
}
function readFromDisk(workspaceRoot, rel) {
    try {
        const root = path.resolve(workspaceRoot);
        const resolved = path.resolve(root, rel);
        // Refuse anything that escapes the workspace root via `..` /
        // absolute / symlink before it reaches `readFileSync`. Callers
        // are internal today, but a defensive guard here keeps any
        // future detector or test fixture from punching through.
        if (resolved !== root && !resolved.startsWith(root + path.sep)) {
            return null;
        }
        return readFileSync(resolved, 'utf-8');
    }
    catch {
        return null;
    }
}
//# sourceMappingURL=source-reader.js.map