import{ArgumentError as e}from"../ArgumentError.js";import{isMapImageSublayer as i,isWMTSLayer as a}from"../utilities/_layers.js";import{checkArg as r}from"../utilities/checkArg.js";import{LayerDefinition as l}from"./LayerDefinition.js";import{PopupInfo as n}from"./PopupInfo.js";export var Layer;!function(t){async function o(e,a){if(r("sublayer",a).isNotMissing(),e&&(e.hasOwnProperty("defaultVisibility")&&(a.visible=e.defaultVisibility),e.hasOwnProperty("id")&&(a.id=e.id),e.hasOwnProperty("title")&&(a.title=e.title),i(a)&&(e.hasOwnProperty("maxScale")&&null!=e.maxScale&&(a.maxScale=e.maxScale),e.hasOwnProperty("minScale")&&null!=e.minScale&&(a.minScale=e.minScale),e.hasOwnProperty("name")&&(a.title=e.name),e.hasOwnProperty("popupInfo")&&null!=e.popupInfo&&(a.popupTemplate=n.toEsriApiPopupTemplate(e.popupInfo)),e.hasOwnProperty("showLegend")&&null!=e.showLegend&&(a.legendEnabled=e.showLegend,a.showLegend=e.showLegend),e.hasOwnProperty("layerDefinition")&&null!=e.layerDefinition&&await l.applyToEsriApiLayer(e.layerDefinition,a),e.hasOwnProperty("subLayerIds")&&null!=e.subLayerIds&&a.sublayers))){const i=e.subLayerIds.map((e=>+e)),r=a.sublayers.toArray().filter((e=>!i.includes(+e.id)));a.sublayers.removeMany(r)}}t.toEsriApiSublayer=async function(i,a){let r;if("ArcGISMapServiceLayer"!==a)throw new e(`Unable to create sublayers for layer type "${a}".`);return r=new((await import("@arcgis/core/layers/support/Sublayer")).default),await o(i,r),r},t.applyToEsriApiSublayer=o,t.applyToEsriApiLayer=async function(e,i){r("layer",i).isNotMissing(),e&&(e.hasOwnProperty("defaultVisibility")&&null!=e.defaultVisibility&&(i.visible=e.defaultVisibility),e.hasOwnProperty("id")&&(i.id=e.id),e.hasOwnProperty("maxScale")&&null!=e.maxScale&&(i.maxScale=e.maxScale),e.hasOwnProperty("minScale")&&null!=e.minScale&&(i.minScale=e.minScale),e.hasOwnProperty("title")&&(i.title=e.title),e.hasOwnProperty("name")&&(i.title=e.name),e.hasOwnProperty("popupInfo")&&null!=e.popupInfo&&(i.popupTemplate=n.toEsriApiPopupTemplate(e.popupInfo)),e.hasOwnProperty("showLegend")&&null!=e.showLegend&&(i.legendEnabled=e.showLegend,i.showLegend=e.showLegend),e.hasOwnProperty("layerDefinition")&&null!=e.layerDefinition&&await l.applyToEsriApiLayer(e.layerDefinition,i))},t.fromEsriApiSublayer=function(e,t=e?.layer){r("sublayer",e).isNotMissing().satisfies((e=>!a(e.layer)));const o={id:e.id,title:e.title??void 0};if(null!==e.visible&&void 0!==e.visible&&(o.defaultVisibility=e.visible),i(e)&&(o.minScale=e.minScale,o.maxScale=e.maxScale,o.layerDefinition=l.fromEsriApiLayer(e),e.popupTemplate&&(o.popupInfo=n.fromEsriApiPopupTemplate(e.popupTemplate)),null!==e.legendEnabled&&void 0!==e.legendEnabled&&(o.showLegend=e.legendEnabled)),e.sublayers?.length&&(o.subLayerIds=e.sublayers.map((e=>e.id)).toArray()),t?.sublayers){const i=[],a=(a,r)=>{if(a)for(const l of a.toArray()){if(l===e){o.parentLayerId=r;break}i.push(l)}};for(a(t.sublayers,-1);void 0===o.parentLayerId&&0!==i.length;){const e=i.pop();a(e.sublayers,e.id)}}else o.parentLayerId=-1;return o}}(Layer||(Layer={}));