"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "FileCache", {
    enumerable: true,
    get: function() {
        return FileCache;
    }
});
const _runtimeexceptions = require("../exceptions/runtime.exceptions");
class FileCache {
    printerFileStorage = {};
    totalFileCount = 0;
    logger;
    constructor({ loggerFactory }){
        this.logger = loggerFactory(FileCache.name);
    }
    cachePrinterFiles(printerId, files) {
        if (!printerId) {
            throw new Error("File Cache cant get a null/undefined printer id");
        }
        this.printerFileStorage[printerId] = files;
        this.updateCacheFileRefCount();
    }
    getPrinterFiles(printerId) {
        if (!printerId) {
            throw new Error("File Cache cant get a null/undefined printer id");
        }
        return this.printerFileStorage[printerId];
    }
    updateCacheFileRefCount() {
        let totalFiles = 0;
        for (const storage of Object.values(this.printerFileStorage)){
            totalFiles += storage?.length || 0;
        }
        if (totalFiles !== this.totalFileCount) {
            this.totalFileCount = totalFiles;
            this.logger.log(`Cache updated. ${this.totalFileCount} file storage references cached.`);
        }
        return totalFiles;
    }
    purgePrinterId(printerId) {
        if (!printerId) {
            throw new _runtimeexceptions.ValidationException("Parameter printerId was not provided.");
        }
        const fileStorage = this.printerFileStorage[printerId];
        if (!fileStorage) {
            this.logger.warn("Did not remove printer File Storage as it was not found");
            return;
        }
        delete this.printerFileStorage[printerId];
        this.logger.log(`Purged printer file cache`);
    }
    purgeFile(printerId, filePath) {
        const files = this.getPrinterFiles(printerId);
        if (!files) return;
        const fileIndex = files.findIndex((f)=>f.path === filePath);
        if (fileIndex === -1) {
            this.logger.warn(`A file removal was ordered but this file was not found in files cache for provided printer id`, filePath);
            return this.logger.log("File was not found in cached printer fileList");
        }
        files.splice(fileIndex, 1);
        this.logger.log(`File was removed from cache`);
    }
}

//# sourceMappingURL=file.cache.js.map