import { ConfigProviderMemory } from '@basemaps/config';
import { fsa } from '@basemaps/shared';
import { SwappingLru } from './swapping.lru.js';
class LruConfig {
    constructor(config) {
        this.configProvider = config
            .then((c) => {
            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) {
        this.cache = new SwappingLru(maxSize);
    }
    get(location) {
        var _a;
        const existing = (_a = this.cache.get(location)) === null || _a === void 0 ? void 0 : _a.configProvider;
        if (existing != null)
            return existing;
        const configJson = fsa.readJson(location);
        const config = new LruConfig(configJson);
        this.cache.set(location, config);
        return config.configProvider;
    }
}
/** Cache 20 configs (Around <30KB -> 5MB each)*/
export const CachedConfig = new ConfigCache(20);
//# sourceMappingURL=config.cache.js.map