import { ConfigProviderMemory } from '@basemaps/config';
import { fsa } from '@basemaps/shared';
import { SwappingLru } from './swapping.lru.js';
class LruConfig {
    constructor(config) {
        Object.defineProperty(this, "configProvider", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        this.configProvider = config
            .then((c) => {
            if (c == null)
                return null;
            const configProvider = ConfigProviderMemory.fromJson(c);
            configProvider.createVirtualTileSets();
            return configProvider;
        })
            .catch((e) => {
            if (e.code === 404)
                return null;
            throw e;
        });
    }
    get size() {
        // Return size 1 for the config and cache the number of configs based on size number.
        return 1;
    }
}
export class ConfigCache {
    constructor(maxSize) {
        Object.defineProperty(this, "cache", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        this.cache = new SwappingLru(maxSize);
    }
    get(location) {
        const existing = this.cache.get(location.href)?.configProvider;
        if (existing != null)
            return existing;
        const configJson = fsa.readJson(location).catch(() => {
            return null;
        });
        const config = new LruConfig(configJson);
        this.cache.set(location.href, config);
        return config.configProvider;
    }
}
/** Cache 20 configs (Around <30KB -> 5MB each)*/
export const CachedConfig = new ConfigCache(20);
//# sourceMappingURL=config.cache.js.map