import { Config } from './config.js';
export const WindowUrl = {
    ImageFormat: 'png',
    /**
     * Encode a location into the window.hash
     * Google uses ${lat},${lon},z${zoom}
     * TODO do we want to follow this
     */
    toHash(loc) {
        return `#@${loc.lat.toFixed(7)},${loc.lon.toFixed(7)},z${loc.zoom}`;
    },
    /**
     * Support parsing of zooms with `z14` or `14z`
     * @param zoom string to parse zoom from
     */
    parseZoom(zoom) {
        if (zoom == null || zoom === '')
            return NaN;
        if (zoom.startsWith('z'))
            return parseFloat(zoom.slice(1));
        if (zoom.endsWith('z'))
            return parseFloat(zoom);
        return NaN;
    },
    /** Parse a location from window.hash if it exists */
    fromHash(str) {
        const output = {};
        const hash = str.replace('#@', '');
        const [latS, lonS, zoomS] = hash.split(',');
        const lat = parseFloat(latS);
        const lon = parseFloat(lonS);
        if (!isNaN(lat) && !isNaN(lon)) {
            output.lat = lat;
            output.lon = lon;
        }
        const newZoom = WindowUrl.parseZoom(zoomS);
        if (!isNaN(newZoom)) {
            output.zoom = newZoom;
        }
        return output;
    },
    baseUrl() {
        const baseUrl = Config.BaseUrl;
        if (baseUrl === '')
            return window.location.origin;
        if (!baseUrl.startsWith('http'))
            throw new Error('BaseURL must start with http(s)://');
        return baseUrl;
    },
    toBaseWmts() {
        const api = Config.ApiKey == null || Config.ApiKey === '' ? '' : `?api=${Config.ApiKey}`;
        return `${this.baseUrl()}/v1/tiles/aerial/WMTSCapabilities.xml${api}`;
    },
    toImageryUrl(layerId, imageryType) {
        return `${this.baseUrl()}/v1/imagery/${layerId}/${imageryType}`;
    },
    toTileUrl(urlType, tileMatrix, layerId, style) {
        const api = Config.ApiKey == null || Config.ApiKey === '' ? '' : `?api=${Config.ApiKey}`;
        const baseTileUrl = `${this.baseUrl()}/v1/tiles/${layerId}/${tileMatrix.identifier}`;
        if (urlType === "raster" /* TileRaster */)
            return `${baseTileUrl}/{z}/{x}/{y}.${WindowUrl.ImageFormat}${api}`;
        if (urlType === "vector-xyz" /* TileVectorXyz */)
            return `${baseTileUrl}/{z}/{x}/{y}.pbf${api}`;
        if (urlType === "style" /* TileVectorStyle */)
            return `${baseTileUrl}/style/${style}.json${api}`;
        if (urlType === "wmts" /* Wmts */)
            return `${baseTileUrl}/WMTSCapabilities.xml${api}`;
        if (urlType === "attribution" /* Attribution */)
            return `${baseTileUrl}/attribution.json${api}`;
        if (urlType === "tile-wmts" /* TileWmts */) {
            return `${baseTileUrl}/{TileMatrix}/{TileCol}/{TileRow}.${WindowUrl.ImageFormat}${api}`;
        }
        throw new Error('Unknown url type: ' + urlType);
    },
};
//# sourceMappingURL=url.js.map