import e from"@arcgis/core/geometry/Extent";import r from"@arcgis/core/geometry/SpatialReference";import{geographicToWebMercator as a,webMercatorToGeographic as t}from"@arcgis/core/geometry/support/webMercatorUtils";import s from"@arcgis/core/portal/Portal";import i from"@arcgis/core/portal/PortalItem";import{watch as o}from"../support/observableUtils.js";import{isWMSLayer as l}from"../utilities/_layers.js";import{checkArg as n}from"../utilities/checkArg.js";import{parallelEach as m}from"../utilities/promise.js";import{SpatialReference as p}from"./SpatialReference.js";import{WMSSublayer as y}from"./WMSSublayer.js";import{applyLayerBaseToEsriApiLayer as f,applyBasemapLayerToEsriApiLayer as c,applyOperationalLayerToEsriApiLayer as u,fromEsriApiLayerToLayerBase as h,fromEsriApiLayerToOperationalLayer as g}from"./_layerHelper.js";export var WMSLayer;!function(x){async function P(t,h,g){if(n("layerJson",t).isNotMissing(),n("layer",h).isNotMissing().satisfies(l),n("layerJson.layerType",t.layerType).satisfies((e=>!e||"WMS"===e)),f(t,h),c(t,h),u(t,h),t.hasOwnProperty("copyright")&&(h.copyright=t.copyright),t.hasOwnProperty("customLayerParameters")&&(h.customLayerParameters={...t.customLayerParameters}),t.hasOwnProperty("customParameters")&&(h.customParameters={...t.customParameters}),t.hasOwnProperty("featureInfoFormat")&&(h.featureInfoFormat=t.featureInfoFormat),t.hasOwnProperty("featureInfoUrl")&&(h.featureInfoUrl=t.featureInfoUrl),h.imageFormat=t.hasOwnProperty("format")?t.format:"image/png",t.hasOwnProperty("itemId")&&(h.portalItem?h.portalItem.id=t.itemId:h.portalItem=new i({id:t.itemId,portal:g||s.getDefault()})),t.hasOwnProperty("layers")&&t.layers?.length){const e=async()=>{await m(t.layers,(async e=>{const r=h.sublayers.find((r=>r.name===e.name));r&&await y.applyToEsriApiWMSSublayer(e,r)})),h.sublayers.removeMany(h.sublayers.filter((e=>!t.layers.some((r=>r.name===e.name)))))};h.sublayers?await e():o(h,"sublayers",e,{once:!0})}if(t.hasOwnProperty("visibleLayers")&&null!=t.visibleLayers){const e=()=>h.sublayers.forEach((e=>{e.visible=t.visibleLayers.includes(e.name)}));h.sublayers?e():o(h,"sublayers",e,{once:!0})}if(t.hasOwnProperty("mapUrl")&&null!=t.mapUrl&&(h.mapUrl=t.mapUrl),t.hasOwnProperty("maxHeight")&&null!=t.maxHeight&&(h.imageMaxHeight=t.maxHeight),t.hasOwnProperty("maxWidth")&&null!=t.maxWidth&&(h.imageMaxWidth=t.maxWidth),t.hasOwnProperty("spatialReferences")&&t.spatialReferences?.length&&(h.spatialReferences=t.spatialReferences.slice(),h.spatialReference||(h.spatialReference=p.toEsriApiSpatialReference({wkid:t.spatialReferences[0]}))),t.hasOwnProperty("extent")&&null!=t.extent){const s=new e({xmin:t.extent[0][0],ymin:t.extent[0][1],xmax:t.extent[1][0],ymax:t.extent[1][1],spatialReference:r.WGS84});(h.spatialReference?.isWGS84||h.spatialReference?.isWebMercator)&&(h.fullExtent=h.spatialReference.isWGS84?s:a(s))}t.hasOwnProperty("url")&&null!=t.url&&(h.url=t.url),t.hasOwnProperty("version")&&null!=t.version&&(h.version=t.version)}x.toEsriApiLayer=async function(e){n("layerJson",e).isNotMissing(),n("layerJson.layerType",e.layerType).satisfies((e=>!e||"WMS"===e));const r=new((await import("@arcgis/core/layers/WMSLayer")).default);return await P(e,r),r},x.applyToEsriApiLayer=P,x.fromEsriApiLayer=function(e){n("layer",e).isNotMissing();const r={...h(e),...g(e),layerType:"WMS",visibleLayers:[]};if(e.copyright&&(r.copyright=e.copyright),e.customLayerParameters&&(r.customLayerParameters={...e.customLayerParameters}),e.customParameters&&(r.customParameters={...e.customParameters}),e.fullExtent&&(e.spatialReference.isWGS84||e.spatialReference.isWebMercator)){const a=e.spatialReference.isWGS84?e.fullExtent:t(e.fullExtent,!1);r.extent=[[a.xmin,a.ymin],[a.xmax,a.ymax]]}return e.featureInfoFormat&&(r.featureInfoFormat=e.featureInfoFormat),e.featureInfoUrl&&(r.featureInfoUrl=e.featureInfoUrl),e.imageFormat&&(r.format=e.imageFormat),e.portalItem&&(r.itemId=e.portalItem.id??void 0),e.sublayers&&(r.layers=[],e.sublayers.forEach((e=>{const a=y.fromEsriApiWMSSublayer(e);r.layers.push(a),e.visible&&r.visibleLayers?.push(e.name)}))),e.imageMaxHeight&&(r.maxHeight=e.imageMaxHeight),e.imageMaxWidth&&(r.maxWidth=e.imageMaxWidth),e.mapUrl&&(r.mapUrl=e.mapUrl),e.spatialReferences&&(r.spatialReferences=e.spatialReferences.slice()),e.url&&(r.url=e.url),e.version&&(r.version=e.version),r}}(WMSLayer||(WMSLayer={}));