"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.createValidationTypeScriptAstCache = createValidationTypeScriptAstCache;
const path = __importStar(require("node:path"));
const ts = __importStar(require("typescript"));
function normalizeAbsolutePath(value) {
    const resolved = path.resolve(value);
    return process.platform === 'win32' ? resolved.toLowerCase() : resolved;
}
function createValidationTypeScriptAstCache(options) {
    const createSourceFile = options?.createSourceFile ?? ts.createSourceFile;
    const now = options?.now ?? (() => Date.now());
    const astCache = new Map();
    const stats = {
        cacheHits: 0,
        cacheMisses: 0,
        physicalParses: 0,
        entries: 0,
        parseTimeMs: 0,
    };
    const buildParserKey = (request) => JSON.stringify({
        filePath: normalizeAbsolutePath(request.filePath),
        scriptTarget: request.scriptTarget,
        scriptKind: request.scriptKind ?? null,
        setParentNodes: request.setParentNodes,
    });
    return {
        parseSourceFile(request) {
            const key = buildParserKey(request);
            const compatibleEntries = astCache.get(key) ?? [];
            const cached = compatibleEntries.find((entry) => entry.sourceText === request.sourceText);
            if (cached) {
                stats.cacheHits += 1;
                return cached.sourceFile;
            }
            stats.cacheMisses += 1;
            stats.physicalParses += 1;
            const started = now();
            const sourceFile = createSourceFile(request.filePath, request.sourceText, request.scriptTarget, request.setParentNodes, request.scriptKind);
            stats.parseTimeMs += Math.max(0, now() - started);
            compatibleEntries.push({ sourceText: request.sourceText, sourceFile });
            astCache.set(key, compatibleEntries);
            stats.entries += 1;
            return sourceFile;
        },
        getStats: () => ({ ...stats }),
    };
}
//# sourceMappingURL=validationTypeScriptAstCache.js.map