// All material copyright Esri, All Rights Reserved, unless otherwise specified. // See https://js.arcgis.com/4.30/esri/copyright.txt for details. //>>built define("../chunks/tslib.es6 ../request ../core/arrayUtils ../core/Collection ../core/Error ../core/lang ../core/MultiOriginJSONSupport ../core/object ../core/promiseUtils ../core/reactiveUtils ../core/urlUtils ../core/accessorSupport/decorators/property ../core/has ../core/Logger ../core/accessorSupport/decorators/reader ../core/accessorSupport/decorators/subclass ../core/accessorSupport/decorators/writer ../geometry/Extent ./Layer ./WebTileLayer ./mixins/BlendLayer ./mixins/OperationalLayer ./mixins/PortalLayer ./mixins/RefreshableLayer ./mixins/ScaleRangeLayer ./support/imageBitmapUtils ./support/TileInfo ./support/TileInfoTilemapCache ./support/WMTSLayerInfo ./support/WMTSSublayer ./support/wmtsUtils".split(" "), function(f,r,z,A,u,v,e,B,w,k,q,g,R,S,m,C,D,E,F,G,H,I,J,K,L,x,M,N,O,n,l){function P(a,b){return a.map(d=>{const c=new n;c.read(d,b);return c})}var t;const y={"image/png":".png","image/png8":".png","image/png24":".png","image/png32":".png","image/jpg":".jpg","image/jpeg":".jpeg","image/gif":".gif","image/bmp":".bmp","image/tiff":".tif","image/jpgpng":"","image/jpegpng":"","image/unknown":""},Q=new Set("version service request layer style format tilematrixset tilematrix tilerow tilecol".split(" ")); e=t=class extends H.BlendLayer(K.RefreshableLayer(L.ScaleRangeLayer(I.OperationalLayer(J.PortalLayer(e.MultiOriginJSONMixin(F)))))){constructor(...a){super(...a);this.activeLayer=null;this.copyright="";this.fullExtent=this.customLayerParameters=this.customParameters=null;this.operationalLayerType="WebTiledLayer";this.resourceInfo=null;this.serviceMode="RESTful";this.sublayers=null;this.type="wmts";this.version="1.0.0";this.addHandles([k.watch(()=>this.activeLayer,(b,d)=>{d&&!this.sublayers?.includes(d)&& (d.layer=null,d.parent=null);b&&(b.layer=this,b.parent=this)},k.sync),k.on(()=>this.sublayers,"after-add",({item:b})=>{b.layer=this;b.parent=this},k.sync),k.on(()=>this.sublayers,"after-remove",({item:b})=>{b.layer=null;b.parent=null},k.sync),k.watch(()=>this.sublayers,(b,d)=>{if(d)for(const c of d)c.layer=null,c.parent=null;if(b)for(const c of b)c.layer=this,c.parent=this},k.sync)])}normalizeCtorArgs(a,b){return"string"===typeof a?{url:a,...b}:a}load(a){this.addResolvingPromise(this.loadFromPortal({supportedTypes:["WMTS"]}, a).catch(w.throwIfAbortError).then(()=>this._fetchService(a)).catch(b=>{w.throwIfAbortError(b);throw new u("wmtslayer:unsupported-service-data","Invalid response from the WMTS service.",{error:b});}));return Promise.resolve(this)}readActiveLayerFromService(a,b,d){this.activeLayer||(this.activeLayer=new n);(a=b.layers.find(c=>c.id===this.activeLayer.id))||(a=b.layers[0]);this.activeLayer.read(a,d);return this.activeLayer}readActiveLayerFromItemOrWebDoc(a,b){const {templateUrl:d,wmtsInfo:c}=b;a=d?this._getLowerCasedUrlParams(d): null;b=c?.layerIdentifier;let h=null;const p=c?.tileMatrixSet;p&&(Array.isArray(p)?p.length&&(h=p[0]):h=p);return new n({id:b,imageFormat:a?.format,styleId:a?.style,tileMatrixSetId:h})}writeActiveLayer(a,b,d,c){a=this.activeLayer;b.templateUrl=this.getUrlTemplate(a.id,a.tileMatrixSetId,a.imageFormat,a.styleId);d=B.getDeepValue("tileMatrixSet.tileInfo",a);b.tileInfo=d?d.toJSON(c):null;b.wmtsInfo={...b.wmtsInfo,layerIdentifier:a.id,tileMatrixSet:a.tileMatrixSetId}}readCustomParameters(a,b){return(a= b.wmtsInfo)?this._mergeParams(a.customParameters,a.url):null}get fullExtents(){return this.activeLayer.fullExtents}readServiceMode(a,b){return b.templateUrl.includes("?")?"KVP":"RESTful"}readSublayersFromService(a,b,d){return P(b.layers,d)}get supportedSpatialReferences(){return this.activeLayer.tileMatrixSets?.map(a=>a.tileInfo?.spatialReference).toArray().filter(z.isSome)??[]}get tilemapCache(){const a=this.activeLayer?.tileMatrixSet?.tileInfo;return a?new N(a):void 0}get title(){return this.activeLayer?.title?? "Layer"}set title(a){this._overrideIfSome("title",a)}get url(){return this._get("url")}set url(a){a&&"/"===a.substr(-1)?this._set("url",a.slice(0,-1)):this._set("url",a)}createWebTileLayer(a){const b=this.getUrlTemplate(this.activeLayer.id,this.activeLayer.tileMatrixSetId,this.activeLayer.imageFormat,this.activeLayer.styleId),d=this._getTileMatrixSetById(a.tileMatrixSetId)?.tileInfo,c=a.fullExtent;a=new O.WMTSLayerInfo({layerIdentifier:a.id,tileMatrixSet:a.tileMatrixSetId,url:this.url});this.customLayerParameters&& (a.customLayerParameters=this.customLayerParameters);this.customParameters&&(a.customParameters=this.customParameters);return new G({fullExtent:c,urlTemplate:b,tileInfo:d,wmtsInfo:a})}async fetchTile(a,b,d,c={}){({signal:c}=c);a=this.getTileUrl(a,b,d);({data:a}=await r(a,{responseType:"image",signal:c}));return a}async fetchImageBitmapTile(a,b,d,c={}){const {signal:h}=c;if(this.fetchTile!==t.prototype.fetchTile)return c=await this.fetchTile(a,b,d,c),x.createTileBitmap(c,a,b,d,h);c=this.getTileUrl(a, b,d);({data:c}=await r(c,{responseType:"blob",signal:h}));return x.createTileBitmap(c,a,b,d,h)}findSublayerById(a){return this.sublayers?.find(b=>b.id===a)}getTileUrl(a,b,d){var c=this._getTileMatrixSetById(this.activeLayer.tileMatrixSetId)?.tileInfo?.lods[a];a=c?c.levelValue||`${c.level}`:`${a}`;(c=this.resourceInfo?"":l.getTileUrlFromResourceUrls({dimensionMap:this.dimensionMap,layerMap:this.layerMap},this.activeLayer.id,this.activeLayer.tileMatrixSetId,this.activeLayer.imageFormat,this.activeLayer.styleId, a,b,d))||(c=this.getUrlTemplate(this.activeLayer.id,this.activeLayer.tileMatrixSetId,this.activeLayer.imageFormat,this.activeLayer.styleId).replaceAll(/\{level\}/gi,a).replaceAll(/\{row\}/gi,`${b}`).replaceAll(/\{col\}/gi,`${d}`));return c=this._appendCustomLayerParameters(c)}getUrlTemplate(a,b,d,c){if(!this.resourceInfo){var h=l.getTileUrlTemplateFromResourceUrls({dimensionMap:this.dimensionMap,layerMap:this.layerMap},a,b,c);if(h)return h}return"KVP"===this.serviceMode?this.url+"?SERVICE\x3dWMTS\x26VERSION\x3d"+ this.version+"\x26REQUEST\x3dGetTile\x26LAYER\x3d"+a+"\x26STYLE\x3d"+c+"\x26FORMAT\x3d"+d+"\x26TILEMATRIXSET\x3d"+b+"\x26TILEMATRIX\x3d{level}\x26TILEROW\x3d{row}\x26TILECOL\x3d{col}":"RESTful"===this.serviceMode?(h="",y[d.toLowerCase()]&&(h=y[d.toLowerCase()]),this.url+a+"/"+c+"/"+b+"/{level}/{row}/{col}"+h):""}async _fetchService(a){let b;if(this.resourceInfo)"KVP"===this.resourceInfo.serviceMode&&(this.url+=this.url.includes("?")?"":"?"),b={ssl:!1,data:this.resourceInfo};else try{b=await this._getCapabilities(this.serviceMode, a),l.validateCapabilities(b.data)}catch{const d="KVP"===this.serviceMode?"RESTful":"KVP";try{b=await this._getCapabilities(d,a),l.validateCapabilities(b.data),this.serviceMode=d}catch(c){throw new u("wmtslayer:unsupported-service-data","Services does not support RESTful or KVP service modes.",{error:c});}}b.data=this.resourceInfo?l.parseResourceInfo(b.data):l.parseCapabilities(b.data,{serviceMode:this.serviceMode,url:this.url});b.data&&this.read(b.data,{origin:"service"})}async _getCapabilities(a, b){a=this._getCapabilitiesUrl(a);return await r(a,{...b,responseType:"text"})}_getTileMatrixSetById(a){return this.findSublayerById(this.activeLayer.id)?.tileMatrixSets?.find(b=>b.id===a)}_appendCustomParameters(a){return this._appendParameters(a,this.customParameters)}_appendCustomLayerParameters(a){return this._appendParameters(a,{...v.clone(this.customParameters),...this.customLayerParameters})}_appendParameters(a,b){a=q.urlToObject(a);b=q.objectToQuery({...a.query,...b});return""===b?a.path:`${a.path}?${b}`}_getCapabilitiesUrl(a){let b= this.url=q.urlToObject(this.url).path;switch(a){case "KVP":b+=`?request=GetCapabilities&service=WMTS&version=${this.version}`;break;case "RESTful":a=`/${this.version}/WMTSCapabilities.xml`,b=b.replace(new RegExp(a,"i"),""),b+=a}return this._appendCustomParameters(b)}_getLowerCasedUrlParams(a){if(!a)return null;const b=q.urlToObject(a).query;if(!b)return null;const d={};Object.keys(b).forEach(c=>{d[c.toLowerCase()]=b[c]});return d}_mergeParams(a,b){const d=this._getLowerCasedUrlParams(b);d&&(b=Object.keys(d), b.length&&(a=a?v.clone(a):{},b.forEach(c=>{a.hasOwnProperty(c)||Q.has(c)||(a[c]=d[c])})));return a}};f.__decorate([g.property()],e.prototype,"dimensionMap",void 0);f.__decorate([g.property()],e.prototype,"layerMap",void 0);f.__decorate([g.property({type:n,json:{origins:{"web-document":{write:{ignoreOrigin:!0}}}}})],e.prototype,"activeLayer",void 0);f.__decorate([m.reader("service","activeLayer",["layers"])],e.prototype,"readActiveLayerFromService",null);f.__decorate([m.reader(["web-document","portal-item"], "activeLayer",["wmtsInfo"])],e.prototype,"readActiveLayerFromItemOrWebDoc",null);f.__decorate([D.writer(["web-document","portal-item"],"activeLayer",{templateUrl:{type:String},tileInfo:{type:M},"wmtsInfo.layerIdentifier":{type:String},"wmtsInfo.tileMatrixSet":{type:String}})],e.prototype,"writeActiveLayer",null);f.__decorate([g.property({type:String,value:"",json:{write:!0}})],e.prototype,"copyright",void 0);f.__decorate([g.property({type:["show","hide"]})],e.prototype,"listMode",void 0);f.__decorate([g.property({json:{read:!0, write:!0}})],e.prototype,"blendMode",void 0);f.__decorate([g.property({json:{origins:{"web-document":{read:{source:["wmtsInfo.customParameters","wmtsInfo.url"]},write:{target:"wmtsInfo.customParameters"}},"portal-item":{read:{source:["wmtsInfo.customParameters","wmtsInfo.url"]},write:{target:"wmtsInfo.customParameters"}}}}})],e.prototype,"customParameters",void 0);f.__decorate([m.reader(["portal-item","web-document"],"customParameters")],e.prototype,"readCustomParameters",null);f.__decorate([g.property({json:{origins:{"web-document":{read:{source:"wmtsInfo.customLayerParameters"}, write:{target:"wmtsInfo.customLayerParameters"}},"portal-item":{read:{source:"wmtsInfo.customLayerParameters"},write:{target:"wmtsInfo.customLayerParameters"}}}}})],e.prototype,"customLayerParameters",void 0);f.__decorate([g.property({type:E,json:{write:{ignoreOrigin:!0},origins:{"web-document":{read:{source:"fullExtent"}},"portal-item":{read:{source:"fullExtent"}}}}})],e.prototype,"fullExtent",void 0);f.__decorate([g.property({readOnly:!0})],e.prototype,"fullExtents",null);f.__decorate([g.property({type:["WebTiledLayer"]})], e.prototype,"operationalLayerType",void 0);f.__decorate([g.property()],e.prototype,"resourceInfo",void 0);f.__decorate([g.property()],e.prototype,"serviceMode",void 0);f.__decorate([m.reader(["portal-item","web-document"],"serviceMode",["templateUrl"])],e.prototype,"readServiceMode",null);f.__decorate([g.property({type:A.ofType(n)})],e.prototype,"sublayers",void 0);f.__decorate([m.reader("service","sublayers",["layers"])],e.prototype,"readSublayersFromService",null);f.__decorate([g.property({readOnly:!0})], e.prototype,"supportedSpatialReferences",null);f.__decorate([g.property({readOnly:!0})],e.prototype,"tilemapCache",null);f.__decorate([g.property({json:{read:{source:"title"}}})],e.prototype,"title",null);f.__decorate([g.property({json:{read:!1},readOnly:!0,value:"wmts"})],e.prototype,"type",void 0);f.__decorate([g.property({json:{origins:{service:{read:{source:"tileUrl"}},"web-document":{read:{source:"wmtsInfo.url"},write:{target:"wmtsInfo.url"}},"portal-item":{read:{source:"wmtsInfo.url"},write:{target:"wmtsInfo.url"}}}}})], e.prototype,"url",null);f.__decorate([g.property()],e.prototype,"version",void 0);return e=t=f.__decorate([C.subclass("esri.layers.WMTSLayer")],e)});