"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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CachedFileReader = exports.cachedFileReader = void 0;
const fs = __importStar(require("fs"));
/**
 * Cached file reader for non-changing files.
 * Reads from filesystem on first access, then serves from cache.
 */
class CachedFileReader {
    cache = new Map();
    /**
     * Reads a file from cache if available, otherwise reads from filesystem and caches the result.
     * @param filePath - Absolute path to the file to read
     * @param encoding - File encoding (default: 'utf-8')
     * @returns The file contents as a string
     */
    read(filePath, encoding = 'utf-8') {
        const cacheKey = `${filePath}:${encoding}`;
        if (this.cache.has(cacheKey)) {
            return this.cache.get(cacheKey);
        }
        const content = fs.readFileSync(filePath, encoding);
        this.cache.set(cacheKey, content);
        return content;
    }
    /**
     * Asynchronously reads a file from cache if available, otherwise reads from filesystem and caches the result.
     * @param filePath - Absolute path to the file to read
     * @param encoding - File encoding (default: 'utf-8')
     * @returns Promise resolving to the file contents as a string
     */
    async readAsync(filePath, encoding = 'utf-8') {
        const cacheKey = `${filePath}:${encoding}`;
        if (this.cache.has(cacheKey)) {
            return this.cache.get(cacheKey);
        }
        const content = await fs.promises.readFile(filePath, encoding);
        this.cache.set(cacheKey, content);
        return content;
    }
    /**
     * Flushes the entire cache.
     */
    flushCache() {
        this.cache.clear();
    }
    /**
     * Flushes a specific file from the cache.
     * @param filePath - Absolute path to the file to flush
     * @param encoding - File encoding (default: 'utf-8')
     */
    flushFile(filePath, encoding = 'utf-8') {
        const cacheKey = `${filePath}:${encoding}`;
        this.cache.delete(cacheKey);
    }
    /**
     * Checks if a file is cached.
     * @param filePath - Absolute path to the file
     * @param encoding - File encoding (default: 'utf-8')
     * @returns true if the file is cached, false otherwise
     */
    isCached(filePath, encoding = 'utf-8') {
        const cacheKey = `${filePath}:${encoding}`;
        return this.cache.has(cacheKey);
    }
    /**
     * Gets the number of cached files.
     * @returns The number of files currently in cache
     */
    getCacheSize() {
        return this.cache.size;
    }
}
exports.CachedFileReader = CachedFileReader;
// Export singleton instance for convenience
exports.cachedFileReader = new CachedFileReader();
//# sourceMappingURL=cachedFileReader.js.map