/** Limit the caching of tiffs */
export class TiffCache {
    static get(id) {
        const cacheA = TiffCache.cacheA.get(id);
        if (cacheA)
            return cacheA;
        const cacheB = TiffCache.cacheB.get(id);
        if (cacheB == null)
            return null;
        // If a object is still useful move it into the main cache
        TiffCache.cacheA.set(id, cacheB);
        TiffCache.cacheB.delete(id);
        return cacheB;
    }
    /** Reset the cache */
    static clear() {
        this.cacheA.clear();
        this.cacheB.clear();
    }
    static set(id, tiff) {
        TiffCache.cacheA.set(id, tiff);
        TiffCache.check();
    }
    /** Validate the size of the cache has not exploded */
    static check() {
        if (TiffCache.MaxCacheSizeBytes <= 0)
            return;
        if (TiffCache.currentSize <= TiffCache.MaxCacheSizeBytes)
            return;
        TiffCache.cacheB = TiffCache.cacheA;
        TiffCache.cacheA = new Map();
    }
    /** Calculate the total number of bytes used by this tiff cache */
    static get currentSize() {
        let size = 0;
        for (const value of TiffCache.cacheA.values()) {
            size += value.source.chunkSize * value.source.chunks.size;
        }
        return size;
    }
}
TiffCache.cacheA = new Map();
TiffCache.cacheB = new Map();
/** 256 MB Cache */
TiffCache.MaxCacheSizeBytes = 256 * 1024 * 1024;
//# sourceMappingURL=tiff.cache.js.map