import{__decorate as e}from"tslib";import{applyJsonToLayer as t,jsonToLayer as r,layerToJson as a}from"../json/LayerJson.js";import{serializable as i}from"../support/Serializable.js";import{notifyChange as s,watch as o}from"../support/observableUtils.js";import{isWMTSLayer as l}from"../utilities/_layers.js";import{getLogger as n}from"../utilities/log.js";import{LayerExtension as y,LayerExtensionLayerListSettings as m}from"./_LayerExtension.js";const p=n("WMTSLayerExtension");let h=class extends y{get supportsSublayers(){return!1}get layer(){return super.layer}get _layerSupportsHideChildren(){return!1}toJSON(e="all"){const t=super.toJSON(e);if(t)return t.layer.$ref&&(t.layer.wmtsInfo={}),t}_getSerializableProperties(){const e=super._getSerializableProperties();return{...e,layer:{...this._toPropertyDef(e.layer),default:{layerType:"WebTiledLayer",wmtsInfo:{}}}}}_createLayerListSettings(e){return new m(e,this,!1)}async _onInitialize(){try{await super._onInitialize()}catch(e){if("RESTful"!==this.layer.serviceMode)throw e;{p.warn(`WMTS Layer '${this.id}' failed to load with the 'RESTful' service mode. Now attempting to load the layer with the 'KVP' service mode...`);const e=this.layer;await this._switchLayer(await r(a(e))),this.mapExtension?.map?.layers.remove(e),this.layer.serviceMode="KVP",this.mapExtension?.map?.layers.add(this.layer),await super._onInitialize()}}finally{this._ensureActiveLayerDefaults()}this._handles.add(o(this.layer,"activeLayer",(e=>{this._ensureActiveLayerDefaults(),this.layer.title=e.title,s(this,"title")}),{sync:!0}))}_isSupportedLayer(e){return l(e)}async _applyPortalItemProperties(e){if("WMTS"!==e.type)throw new Error(`Cannot create WMTS layer from Portal item that has type: ${e.type}`);const r={layerType:"WebTiledLayer",...await e.fetchData()};return t(r,this.layer)}_ensureActiveLayerDefaults(){const e=this.layer?.activeLayer;e&&(e.imageFormats=e.imageFormats?.length?e.imageFormats:["image/png"],e.imageFormat=e.imageFormat||e.imageFormats[0],e.styleId=e.styleId??"")}};h=e([i],h);export{h as WMTSLayerExtension};