import { decifrarSintaxe, TEM_PARSER } from '../parsing/parser.js';
const AST_SENTINEL = {};
export class ScopeAstCache {
    cache = new Map();
    metrics = {
        hits: 0,
        misses: 0,
        parseTimeMs: 0,
        cachedFiles: 0,
    };
    async getOrParse(entry, ext) {
        const key = entry.relPath;
        const cached = this.cache.get(key);
        if (cached !== undefined) {
            this.metrics.hits++;
            return cached;
        }
        if (!TEM_PARSER(ext)) {
            this.cache.set(key, null);
            return null;
        }
        const globalStore = globalThis;
        const globalCache = globalStore.__TUTANUS_AST_CACHE__;
        const globalEntry = globalCache?.get(key);
        if (globalEntry?.ast) {
            this.cache.set(key, globalEntry.ast);
            this.metrics.hits++;
            return globalEntry.ast;
        }
        this.metrics.misses++;
        const inicio = performance.now();
        let ast = null;
        if (entry.content) {
            const parsed = await decifrarSintaxe(entry.content, ext, {
                relPath: entry.relPath,
            });
            if (parsed && typeof parsed === 'object') {
                if (Object.keys(parsed).length > 0) {
                    ast = AST_SENTINEL;
                }
            }
        }
        this.metrics.parseTimeMs += performance.now() - inicio;
        this.cache.set(key, ast);
        this.metrics.cachedFiles = this.cache.size;
        return ast;
    }
    has(relPath) {
        return this.cache.has(relPath);
    }
    getMetrics() {
        return { ...this.metrics };
    }
    clear() {
        this.cache.clear();
        this.metrics = { hits: 0, misses: 0, parseTimeMs: 0, cachedFiles: 0 };
    }
}
//# sourceMappingURL=ast-cache.js.map