/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{__decorate as e}from"tslib";import r from"../../request.js";import{getOrCreateMapValue as t}from"../../core/MapUtils.js";import{throwIfAborted as s}from"../../core/promiseUtils.js";import{join as o}from"../../core/urlUtils.js";import i from"../../core/Version.js";import{property as a,subclass as p}from"../../core/accessorSupport/decorators.js";import l from"../../geometry/Extent.js";import n from"../../geometry/SpatialReference.js";import{parse as u}from"../support/arcgisLayerUrl.js";import{id as c,popupEnabled as y}from"../support/commonProperties.js";import{isHostedLayer as d}from"../../portal/support/portalItemUtils.js";import{reader as m}from"../../core/accessorSupport/decorators/reader.js";const h=h=>{const f=h;let b=class extends f{constructor(){super(...arguments),this.capabilities=void 0,this.copyright=null,this.fullExtent=null,this.legendEnabled=!0,this.spatialReference=null,this.version=void 0,this._allLayersAndTablesMap=null}readCapabilities(e,r){const t=r.capabilities&&r.capabilities.split(",").map(e=>e.toLowerCase().trim());if(!t)return{operations:{supportsExportMap:!1,supportsExportTiles:!1,supportsIdentify:!1,supportsQuery:!1,supportsTileMap:!1},exportMap:null,exportTiles:null};const s=this.type,o="tile"!==s&&!!r.supportsDynamicLayers,a=t.includes("query"),p=t.includes("map"),l=!!r.exportTilesAllowed,n=t.includes("tilemap"),u=t.includes("data"),c="tile"!==s&&(!r.tileInfo||o),y="tile"!==s&&(!r.tileInfo||o),d="tile"!==s,m="tile"!==s&&o&&r.currentVersion>=11.1,h=r.cimVersion?i.parse(r.cimVersion):null,f=h?.greaterEqual(1,4)??!1,b=h?.greaterEqual(2,0)??!1;return{operations:{supportsExportMap:p,supportsExportTiles:l,supportsIdentify:a,supportsQuery:u,supportsTileMap:n},exportMap:p?{supportsArcadeExpressionForLabeling:f,supportsCIMSymbols:b,supportsDynamicLayers:o,supportsSublayerOrderBy:m,supportsSublayerDefinitionExpression:y,supportsSublayerVisibility:c,supportsSublayersChanges:d}:null,exportTiles:l?{maxExportTilesCount:+r.maxExportTilesCount}:null}}readVersion(e,r){let t=r.currentVersion;return t||(t=r.hasOwnProperty("capabilities")||r.hasOwnProperty("tables")?10:r.hasOwnProperty("supportedImageFormatTypes")?9.31:9.3),t}async fetchRelatedService(e){const r=this.portalItem;if(!r||!d(r))return null;this._relatedFeatureServicePromise||(this._relatedFeatureServicePromise=r.fetchRelatedItems({relationshipType:"Service2Service",direction:"reverse"},e).then(e=>e.find(e=>"Feature Service"===e.type)??null,()=>null));const t=await this._relatedFeatureServicePromise;return s(e),t?{itemId:t.id,url:t.url}:null}async fetchSublayerInfo(e,t){const{source:s}=e;if(this?.portalItem&&"tile"===this.type&&"map-layer"===s?.type&&d(this.portalItem)&&e.originIdOf("url")<2){const r=await this.fetchRelatedService(t);r&&(e.url=o(r.url,s.mapLayerId.toString()),e.layerItemId=r.itemId)}const{url:i}=e;let a;if("data-layer"===s.type){a=(await r(i,{responseType:"json",query:{f:"json",...this.customParameters,token:this.apiKey},...t})).data}else{let r=e.id;"map-layer"===s?.type&&(r=s.mapLayerId);try{a=(await this.fetchAllLayersAndTables(t)).get(r)}catch{}if(i&&e.originIdOf("url")>2)try{const e=await this._fetchAllLayersAndTablesFromService(i),r=u(i)?.sublayer??s.mapLayerId,t=structuredClone(e.get(r));if(null!=t){const e=["drawingInfo","maxScale","minScale","name"];for(const r of e)delete t[r];a={...a,...t}}}catch{}}return a}async fetchAllLayersAndTables(e){return this._fetchAllLayersAndTablesFromService(this.parsedUrl?.path,e)}async _fetchAllLayersAndTablesFromService(e,i){await this.load(i),this._allLayersAndTablesMap||=new Map;const a=u(e),p=t(this._allLayersAndTablesMap,a?.url.path,()=>r(o(a?.url.path,"/layers"),{responseType:"json",query:{f:"json",...this.customParameters,token:this.apiKey}}).then(e=>{const r=new Map,{layers:t,tables:s}=e.data,o=[...t??[],...s??[]];for(const i of o)r.set(i.id,i);return{result:r}},e=>({error:e}))),l=await p;if(s(i),"result"in l)return l.result;throw l.error}};return e([a({readOnly:!0})],b.prototype,"capabilities",void 0),e([m("service","capabilities",["capabilities","cimVersion","currentVersion","exportTilesAllowed","maxExportTilesCount","supportsDynamicLayers","tileInfo"])],b.prototype,"readCapabilities",null),e([a({json:{read:{source:"copyrightText"}}})],b.prototype,"copyright",void 0),e([a({type:l})],b.prototype,"fullExtent",void 0),e([a(c)],b.prototype,"id",void 0),e([a({type:Boolean,json:{origins:{service:{read:{enabled:!1}}},read:{source:"showLegend"},write:{target:"showLegend"}}})],b.prototype,"legendEnabled",void 0),e([a(y)],b.prototype,"popupEnabled",void 0),e([a({type:n})],b.prototype,"spatialReference",void 0),e([a({readOnly:!0})],b.prototype,"version",void 0),e([m("service","version",["currentVersion","capabilities","tables","supportedImageFormatTypes"])],b.prototype,"readVersion",null),b=e([p("esri.layers.mixins.ArcGISMapService")],b),b};export{h as ArcGISMapService};
