import e from"@arcgis/core/core/Collection";import r from"@arcgis/core/portal/Portal";import a from"@arcgis/core/portal/PortalItem";import{isMapImageLayer as i}from"../utilities/_layers.js";import{checkArg as s}from"../utilities/checkArg.js";import{compareAsync as o}from"../utilities/collection.js";import{deepClone as l}from"../utilities/object.js";import{Layer as n}from"./Layer.js";import{ERR_LAYER_TYPE_MISMATCH as t,applyLayerBaseToEsriApiLayer as y,applyBasemapLayerToEsriApiLayer as p,applyOperationalLayerToEsriApiLayer as c,fromEsriApiLayerToLayerBase as d,fromEsriApiLayerToOperationalLayer as f}from"./_layerHelper.js";export var MapServiceLayer;function u(e){if(e.layerDefinition){if(e.layerDefinition.source)return!0;const{drawingInfo:r}=e.layerDefinition;if(r&&(r.renderer||r.labelingInfo||void 0!==r.showLabels&&null!==r.showLabels))return!0}return!1}function m(e){const{layerDefinition:r}=e;if(r){r.source&&(delete r.source,console.warn("The map service does not support dynamic layers. Ignoring property: layer.layerDefinition.source"));const{drawingInfo:e}=r;e&&(e.renderer&&(delete e.renderer,console.warn("The map service does not support dynamic layers. Ignoring property: layer.layerDefinition.drawingInfo.renderer")),e.labelingInfo&&(delete e.labelingInfo,console.warn("The map service does not support dynamic layers. Ignoring property: layer.layerDefinition.drawingInfo.labelingInfo")),void 0!==e.showLabels&&null!==e.showLabels&&(delete e.showLabels,console.warn("The map service does not support dynamic layers. Ignoring property: layer.layerDefinition.showLabels.labelingInfo")))}}async function b(r,a){const i=a.capabilities?.exportMap?.supportsDynamicLayers;if(r.layers)if(i&&r.layers.some(u)){const i=[...r.layers].reverse(),s=new Map;await o(a.allSublayers,i,{equals:(e,r)=>e.id===+(r.id??NaN),onMatch:async(e,r)=>{await n.applyToEsriApiSublayer(r,e)},onMissing:e=>{a.sublayers?.remove(e)},onNew:async e=>{const r=await n.toEsriApiSublayer(e,"ArcGISMapServiceLayer");r.layer=a,s.set(r.id,r)}}),i.forEach(((r,o)=>{const l=+(r.id??NaN),n=s.get(l);if(n)if(null!=r.parentLayerId&&-1!==r.parentLayerId){const a=s.get(r.parentLayerId);a&&(a.sublayers=a.sublayers??new e,a.sublayers.add(n))}else a.sublayers?.add(n);else{const e=a.allSublayers.find((e=>e.id===l));if(e){let s,l=a.allSublayers.find((e=>e.id===r.parentLayerId));if(l){s=i.filter((e=>e.parentLayerId===l.id)).map((e=>i.indexOf(e))).indexOf(o)}else s=o,l=a;l.sublayers?.reorder(e,s)}}}))}else if(a.sublayers)for(const e of r.layers){if(void 0===e.id||null===e.id)continue;const r=a.allSublayers.find((r=>r.id===e.id));r&&(u(e)&&m(e),await n.applyToEsriApiSublayer(e,r))}if(r.visibleLayers&&a.allSublayers)for(const e of a.allSublayers.toArray())e.visible=r.visibleLayers.includes(e.id)}!function(e){async function o(e,o,n){s("layerJson",e).isNotMissing(),s("layerJson.layerType",e.layerType).satisfies((e=>!e||"ArcGISMapServiceLayer"===e),t),s("layer",o).isNotMissing().satisfies(i,t);const d=l(e);if(y(d,o),p(d,o),c(d,o),d.hasOwnProperty("url")&&(o.url=d.url),d.hasOwnProperty("itemId")&&(o.portalItem?o.portalItem.id=d.itemId:o.portalItem=new a({id:d.itemId,portal:n||r.getDefault()})),e.customParameters?.hasOwnProperty("gdbVersion")&&(o.gdbVersion=e.customParameters.gdbVersion),o.loaded)await b(d,o);else{const e=o.load;let r=!1;o.load=async a=>(await e(a),r||(r=!0,await b(d,o)),o)}}e.esriMapImageLayerDefaults={legendEnabled:!0,visible:!0,opacity:1},e.toEsriApiLayer=async function(e){s("layerJson",e).isNotMissing(),s("layerJson.layerType",e.layerType).satisfies((e=>!e||"ArcGISMapServiceLayer"===e),t);const r=new((await import("@arcgis/core/layers/MapImageLayer")).default);return await o(e,r),r},e.applyToEsriApiLayer=o,e.fromEsriApiMapImageLayer=function(r){s("layer",r).isNotMissing();const a={...d(r,e.esriMapImageLayerDefaults),...f(r),layerType:"ArcGISMapServiceLayer",url:r.url??void 0};if(r.portalItem&&(a.itemId=r.portalItem.id??void 0),r.gdbVersion&&(a.customParameters={...a.customParameters,gdbVersion:r.gdbVersion}),r.sublayers){a.layers=[],a.visibleLayers=[];const e=i=>{i.visible&&a.visibleLayers.push(i.id),a.layers.push(n.fromEsriApiSublayer(i,r)),i.sublayers&&i.sublayers.toArray().reverse().forEach(e)};r.sublayers.toArray().reverse().forEach(e)}return a}}(MapServiceLayer||(MapServiceLayer={}));