/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import e from"../../core/Collection.js";import"../../core/has.js";import{LayerLoadContext as r}from"./LayerLoadContext.js";import{layersLoaderMap as t}from"./layersLoader.js";import{getLayerModuleForContextAndType as a}from"./layersMapping.js";import o from"../../portal/PortalItem.js";import{isMapNotesLayer as n,isMarkupLayer as s,isRouteLayer as i}from"../../portal/support/featureCollectionUtils.js";import{selectLayerClassPath as l}from"../../portal/support/portalLayers.js";import{loadStyleRenderer as c}from"../../renderers/support/styleUtils.js";async function y(e,r,t){if(!r)return;const a=r.map(e=>u(e,t)),o=await Promise.allSettled(a);for(const n of o)"rejected"===n.status||n.value&&e.add(n.value)}async function u(e,r){return p(await f(e,r),e,r)}async function p(e,r,t){const a=new e;return a.read(r,t.context),"group"===a.type&&("GroupLayer"===r.layerType?await w(a,r,t):m(r)?L(a,r,t.context):d(r)&&await I(a,r,t.context)),await c(a,t.context),a}async function f(e,c){const y=c.context,u=(e.layerType||e.type)??c.defaultLayerType;switch(u){case"Feature Collection":case"ArcGISFeatureLayer":if(n(e)||s(e))return t.MapNotesLayer();if(i(e))return t.RouteLayer();if(m(e)){const a=y?.portal;if(e.itemId){const n=new o({id:e.itemId,portal:a});await n.load();const s=(await l(n,new r)).className||"UnknownLayer";return t[s]()}return t.UnknownLayer()}return d(e)?t.GroupLayer():t.FeatureLayer();case"WebTiledLayer":return e.wmtsInfo?.url&&e.wmtsInfo.layerIdentifier?t.WMTSLayer():t.WebTileLayer();case"WFS":return"2.0.0"!==e.wfsInfo?.version?t.UnsupportedLayer():t.WFSLayer();default:{const e=a(y,u);return(0,t[e])()}}}function d(e){if("ArcGISFeatureLayer"!==e.layerType||m(e))return!1;return(e.featureCollection?.layers?.length??0)>1}function m(e){return"Feature Collection"===e.type}async function w(r,t,a){const o=new e,n=y(o,Array.isArray(t.layers)?t.layers:[],a);try{try{if(await n,"group"===r.type)return r.layers.addMany(o),r}catch(s){r.destroy();for(const e of o)e.destroy();throw s}}catch(s){throw s}}function L(e,r,t){r.itemId&&(e.portalItem=new o({id:r.itemId,portal:t?.portal}),e.when(()=>{const a=a=>{const o=a.layerId;h(a,e,r,o,t);const n=r.featureCollection?.layers?.[o];n&&a.read(n,t)};e.layers?.forEach(a),e.tables?.forEach(a)}))}async function I(e,r,a){const o=t.FeatureLayer,n=await o(),s=r.featureCollection,i=s?.showLegend,l=s?.layers?.map((t,o)=>{const s=new n;s.read(t,a);const l={...a,ignoreDefaults:!0};return h(s,e,r,o,l),null!=i&&s.read({showLegend:i},l),s});e.layers.addMany(l??[])}function h(e,r,t,a,o){e.read({id:`${r.id}-sublayer-${a}`,visibility:t.visibleLayers?.includes(a)??!0},o)}export{w as populateGroupLayer,y as populateOperationalLayers};
