import { base58, isBase58 } from '@basemaps/config/build/base58.js';
import { GoogleTms } from '@basemaps/geo';
import { toQueryString } from '@basemaps/shared/build/url.js';
import { LabelsDisabledLayers } from './components/map.label.js';
import { Config } from './config.js';
export function ensureBase58(s) {
    if (s == null)
        return null;
    if (isBase58(s))
        return s;
    const text = new TextEncoder();
    const buffer = text.encode(s);
    return base58.encode(buffer);
}
export const WindowUrl = {
    ImageFormat: 'png',
    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 query = toQueryString({ api: Config.ApiKey, config: ensureBase58(Config.map.config) });
        return `${this.baseUrl()}/v1/tiles/all/WMTSCapabilities.xml${query}`;
    },
    toImageryUrl(layerId, imageryType) {
        const query = toQueryString({ config: ensureBase58(Config.map.config) });
        return `${this.baseUrl()}/v1/imagery/${layerId}/${imageryType}${query}`;
    },
    toTileUrl(params) {
        const queryParams = new URLSearchParams();
        if (Config.ApiKey != null && Config.ApiKey !== '')
            queryParams.set('api', Config.ApiKey);
        if (params.config != null)
            queryParams.set('config', ensureBase58(params.config));
        if (params.pipeline != null)
            queryParams.set('pipeline', params.pipeline);
        if (params.date?.before != null)
            queryParams.set('date[before]', params.date.before);
        if (params.terrain != null && params.urlType === "style" /* MapOptionType.Style */)
            queryParams.set('terrain', params.terrain);
        if (params.labels && params.urlType === "style" /* MapOptionType.Style */) {
            // Some layers have the label option disabled, do not append the labels request to those layers.
            if (!LabelsDisabledLayers.has(params.style ?? ''))
                queryParams.set('labels', String(params.labels));
        }
        const imageFormat = params.imageFormat ?? WindowUrl.ImageFormat;
        if (params.urlType === "style" /* MapOptionType.Style */) {
            if (params.tileMatrix.identifier !== GoogleTms.identifier)
                queryParams.set('tileMatrix', params.tileMatrix.identifier);
            if (imageFormat !== 'webp')
                queryParams.set('format', imageFormat);
        }
        // If a image format is directly requested ensure it is passed through to the WMTS
        // only some layers like terrain-rgb need a forced image format
        if (params.imageFormat && params.urlType === "wmts" /* MapOptionType.Wmts */)
            queryParams.set('tileFormat', params.imageFormat);
        const q = '?' + queryParams.toString();
        const baseTileUrl = `${this.baseUrl()}/v1/tiles/${params.layerId}/${params.tileMatrix.identifier}`;
        if (params.urlType === "raster" /* MapOptionType.TileRaster */)
            return `${baseTileUrl}/{z}/{x}/{y}.${imageFormat}${q}`;
        if (params.urlType === "vector-xyz" /* MapOptionType.TileVectorXyz */)
            return `${baseTileUrl}/{z}/{x}/{y}.pbf${q}`;
        if (params.urlType === "style" /* MapOptionType.Style */)
            return `${this.baseUrl()}/v1/styles/${params.style ?? params.layerId}.json${q}`;
        if (params.urlType === "wmts" /* MapOptionType.Wmts */)
            return `${baseTileUrl}/WMTSCapabilities.xml${q}`;
        if (params.urlType === "attribution" /* MapOptionType.Attribution */)
            return `${baseTileUrl}/attribution.json${q}`;
        if (params.urlType === "tile-wmts" /* MapOptionType.TileWmts */) {
            return `${baseTileUrl}/{TileMatrix}/{TileCol}/{TileRow}.${imageFormat}${q}`;
        }
        throw new Error('Unknown url type: ' + String(params.urlType));
    },
    toConfigUrl(layerId, config = Config.map.config) {
        const query = toQueryString({ api: Config.ApiKey, config });
        return `${this.baseUrl()}/v1/config/${layerId}.json${query}`;
    },
    toConfigImageryUrl(layerId, imageryId, config = Config.map.config) {
        const query = toQueryString({ api: Config.ApiKey, config });
        return `${this.baseUrl()}/v1/config/${layerId}/${imageryId}.json${query}`;
    },
};
//# sourceMappingURL=url.js.map