import { TileMatrixSets } from '@basemaps/geo';
import { Projection } from '@basemaps/geo';
import { WindowUrl } from './url.js';
export class ConfigData {
    static getTileSet(tileSetId) {
        const tileSetUrl = WindowUrl.toConfigUrl(tileSetId);
        let existing = this.tileSets.get(tileSetUrl);
        if (existing)
            return existing;
        existing = fetch(tileSetUrl).then((res) => {
            if (res.ok)
                return res.json();
            return null;
        });
        this.tileSets.set(tileSetUrl, existing);
        return existing;
    }
    static getImagery(tileSetId, imageryId) {
        const imageryUrl = WindowUrl.toConfigImageryUrl(tileSetId, imageryId);
        let existing = this.imagery.get(imageryUrl);
        if (existing)
            return existing;
        existing = fetch(imageryUrl).then((res) => {
            if (res.ok)
                return res.json();
            return null;
        });
        this.imagery.set(imageryUrl, existing);
        return existing;
    }
    static getGeoJson(imagery) {
        const tileMatrix = TileMatrixSets.find(imagery.tileMatrix);
        if (tileMatrix == null)
            return;
        const gj = Projection.get(tileMatrix).toGeoJson(imagery.files);
        // Improve the GeoJSON with some attributes that are useful
        for (const f of gj.features) {
            f.properties = f.properties ?? {};
            const fileName = f.properties['name'];
            f.properties['location'] = new URL(fileName, imagery.uri).href;
            f.properties['epsg'] = imagery.projection;
            f.properties['tileMatrix'] = imagery.tileMatrix;
            const source = imagery.files.find((f) => f.name === fileName);
            if (source != null) {
                f.properties['x'] = source.x;
                f.properties['y'] = source.y;
                f.properties['width'] = source.width;
                f.properties['height'] = source.height;
            }
        }
        return gj;
    }
}
Object.defineProperty(ConfigData, "tileSets", {
    enumerable: true,
    configurable: true,
    writable: true,
    value: new Map()
});
Object.defineProperty(ConfigData, "imagery", {
    enumerable: true,
    configurable: true,
    writable: true,
    value: new Map()
});
//# sourceMappingURL=config.layer.js.map