import{watch as e}from"../support/observableUtils.js";import{isSubtypeGroupLayer as r}from"../utilities/_layers.js";import{checkArg as s}from"../utilities/checkArg.js";import{LayerDefinition as a}from"./LayerDefinition.js";import{SubtypeSublayer as o}from"./SubtypeSublayer.js";import{ERR_LAYER_TYPE_MISMATCH as t,applyLayerBaseToEsriApiLayer as i,applyOperationalLayerToEsriApiLayer as y,fromEsriApiLayerToLayerBase as p,fromEsriApiLayerToOperationalLayer as u}from"./_layerHelper.js";export var SubtypeGroupLayer;!function(l){async function n(p,u){s("layerJson",p).isNotMissing();const l=p.layerType||p.type;if(s("layerJson.layerType",l).satisfies((e=>!e||"SubtypeGroupLayer"===e),t),s("layer",u).isNotMissing().satisfies(r,t),i(p,u),y(p,u),p.hasOwnProperty("url")&&(u.url=p.url),p.hasOwnProperty("customParameters")&&null!=p.customParameters&&(u.customParameters={...p.customParameters},p.customParameters.hasOwnProperty("gdbVersion")&&(u.gdbVersion=p.customParameters.gdbVersion)),p.hasOwnProperty("layerDefinition")&&null!=p.layerDefinition&&await a.applyToEsriApiLayer(p.layerDefinition,u),p.hasOwnProperty("layers")&&null!=p.layers){const r=async()=>{p.layers.length>0&&p.layers.some((e=>u.sublayers.map((e=>e.subtypeCode)).includes(+(e.subtypeCode??NaN))))&&(u.sublayers=u.sublayers.filter((e=>p.layers.some((r=>+(r.subtypeCode??NaN)===e.subtypeCode))))),await Promise.all(u.sublayers.map((e=>{const r=p.layers.find((r=>+(r.subtypeCode??NaN)===e.subtypeCode));if(r)return o.applyToEsriApiLayer(r,e)})))};u.loaded?await r():e(u,"loaded",r,{once:!0})}}l.toEsriApiLayer=async function(e){s("layerJson",e).isNotMissing();const r=e.layerType||e.type;s("layerJson.layerType",r).satisfies((e=>!e||"SubtypeGroupLayer"===e),t);const a=new((await import("@arcgis/core/layers/SubtypeGroupLayer")).default);return await n(e,a),a},l.applyToEsriApiLayer=n,l.fromEsriApiSubtypeGroupLayer=function(e){s("subtypeGroupLayer",e).isNotMissing();const r={...p(e),...u(e),layerType:"SubtypeGroupLayer",url:e.url??void 0,customParameters:{...e.customParameters,gdbVersion:e.gdbVersion},layerDefinition:a.fromEsriApiLayer(e),layers:[]};return e.sublayers.forEach((e=>{r.layers.push(o.fromEsriApiSubtypeSublayer(e))})),r}}(SubtypeGroupLayer||(SubtypeGroupLayer={}));