import { TileMatrixSets } from '@basemaps/geo';
import { Projection } from '@basemaps/shared/build/proj/projection.js';
import { joinUri } from '@chunkd/core';
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) {
        var _a;
        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 = (_a = f.properties) !== null && _a !== void 0 ? _a : {};
            const fileName = f.properties['name'];
            f.properties['location'] = joinUri(imagery.uri, f.properties['name']);
            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;
    }
}
ConfigData.tileSets = new Map();
ConfigData.imagery = new Map();
//# sourceMappingURL=config.layer.js.map