var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import OlLayerBase from '@openlayers-elements/core/ol-layer-base';
import { property } from 'lit-element';
import WMTSCapabilities from 'ol/format/WMTSCapabilities';
import TileLayer from 'ol/layer/Tile';
import WMTS, { optionsFromCapabilities } from 'ol/source/WMTS';
import './projections';
import SwisstopoElement from './swisstopo-element';
const parser = new WMTSCapabilities();
/**
 * Layer which loads official Swiss maps from [WMTS capabilities document][wmts-list]
 *
 * [wmts-list]: http://api3.geo.admin.ch/services/sdiservices.html#supported-projections
 *
 * @demo https://openlayers-elements.netlify.com/demo/swiss-topo/
 * @appliesMixin SwisstopoElementMixin
 * @customElement
 */
export class SwisstopoWmts extends SwisstopoElement(OlLayerBase) {
    constructor() {
        super(...arguments);
        /**
         * One of projections supported by swisstopo maps:
         *
         * 1. EPSG:3857 (Mercator) (default)
         * 1. EPSG:2056
         * 1. EPSG:21718
         * 1. EPSG:4329
         *
         * @type {string}
         */
        this.projection = 'EPSG:3857';
    }
    async _attach(detail) {
        const { projection } = await detail.map;
        if (projection) {
            this.projection = projection;
        }
        return super._attach(detail);
    }
    async _createLayer() {
        const projectionSegments = this.projection.replace(/:/, '/');
        const response = await fetch(`https://wmts.geo.admin.ch/${projectionSegments}/1.0.0/WMTSCapabilities.xml`);
        const capabilities = parser.read(await response.text());
        const options = optionsFromCapabilities(capabilities, {
            layer: this.layerName,
            matrixSet: this.projection,
        });
        return new TileLayer({
            source: new WMTS(options),
        });
    }
}
__decorate([
    property({ type: String })
], SwisstopoWmts.prototype, "projection", void 0);
customElements.define('swisstopo-wmts', SwisstopoWmts);
//# sourceMappingURL=swisstopo-wmts.js.map