var e;import{__decorate as i}from"tslib";import s from"@arcgis/core/core/Collection";import{EntityBase as t}from"../Entity.js";import{InvalidOperationError as a}from"../InvalidOperationError.js";import{ItemType as n}from"../ItemType.js";import{basemapToJson as r}from"../json/BasemapJson.js";import{serializable as o}from"../support/Serializable.js";import{watch as l}from"../support/observableUtils.js";import{ObservableMap as p}from"../utilities/ObservableMap.js";import{createFlattenedLayerXList as c,createRef as d,getBackingLayer as m,isIterable as y,isLayerRefEqual as f,isSublayerRefEqual as b,toMapExtension as u}from"../utilities/_layer-preset.js";import{basemapsAreEquivalent as x,createStandardBasemap as h}from"../utilities/basemaps.js";import{map as L}from"../utilities/iterable.js";import{getLogger as v}from"../utilities/log.js";import{LayerPresetSetting as E}from"./LayerPresetSettings.js";const S=v("layer-preset");let g=e=class extends t{icon;iconUrl;title;description;layers;basemap;basemapOptions;_itemType=n.LAYER_PRESET;static createFrom(i){const s=new e,t=u(i);t.basemapExtension?.basemap&&(s.basemap=r(t.basemapExtension.basemap));for(const e of t.basemapOptions.toArray())e.included&&e.basemap&&(s.basemapOptions=s.basemapOptions||[],s.basemapOptions.push(r(e.basemap)));const a=c(t);for(const e of a.toArray()){const i=w(e);if(i){const t=e.allSublayerExtensions;if(t.length>0){i.sublayers=new p;for(const e of t.toArray().filter((e=>!!e.sublayer))){const s=w(e);s&&i.sublayers.set(d(e.sublayer),s)}}s.layers=s.layers??new Map,s.layers.set(d(e.layer),i)}}return s}async applyTo(e){if(y(e)){for(const i of e)await this.applyTo(i);return}const i=u(e);if(this.basemap){const e="string"==typeof this.basemap?await h(this.basemap):this.basemap,s=i.basemapOptions.find((i=>x(i.basemap,e)));s&&(i.basemapExtension=s,s.included=!0)}if(this.basemapOptions){const e=new s;for(const s of await Promise.all(this.basemapOptions.map((async e=>"string"==typeof e?h(e):e)))){const t=i.basemapOptions.find((e=>x(e.basemap,s)));t&&(t.included=!0,e.add(t))}for(const s of i.basemapOptions.toArray())e.includes(s)||i.basemapExtension===s||(s.included=!1)}if(this.layers){const e=c(i);e.forEach((e=>e._resolveLayer()));for(const i of this.layers.keys()){const t=d(i),a=e.find((i=>{const s=d(i);return"string"==typeof s.id&&f(s,t,e.toArray())}));if(a){const t=this.layers.get(i);O(t,a),e.remove(a);const n=()=>{const e=new s(a.allSublayerExtensions.toArray());for(const i of t.sublayers.keys()){const s=d(i),a=e.find((e=>b(d(e),s)));a&&(O(t.sublayers.get(i),a),e.remove(a))}};a.layer.loaded?n():l(a.layer,"loaded",(()=>{try{n()}catch(e){S.error({message:`Error applying LayerPreset to sublayers of LayerExtension with ID '${a.id}' and title '${a.title}'.`,error:e})}}),{once:!0})}}}}_getSerializableProperties(){return{...super._getSerializableProperties(),title:"initial",iconUrl:"initial",description:"initial",layers:{serializeModes:["initial"],serialize:e=>{if(this.layers)return Array.from(L(this.layers.entries(),(i=>{let s={layer:i[0]};const t=i[1].toJSON(e);return s={...s,...t,sublayers:t?.sublayers},s})))},deserialize:e=>{this.layers=new Map;for(const i of e){if(!i.layer)throw new a("Cannot define a LayerPresetSetting that does not have layer reference.");this.layers.set(i.layer,new E(i))}},isDefault:e=>!e},icon:"initial",basemap:"initial",basemapOptions:{serializeModes:["initial"]}}}};g=e=i([o],g);export{g as LayerPreset};function w(e){const i=new E,s=m(e);if(i.visible=s.visible,i.opacity=s.opacity,"definitionExpression"in s&&(i.definitionExpression=s.definitionExpression),e.layerListSettings.included)return i.expanded=e.layerListSettings.expanded,i}function O(e,i){const s=m(i);void 0!==e.definitionExpression&&null!==e.definitionExpression&&"definitionExpression"in s&&(s.definitionExpression=e.definitionExpression),void 0!==e.visible&&null!==e.visible&&(s.visible=e.visible),void 0!==e.opacity&&null!==e.opacity&&(s.opacity=e.opacity),void 0!==e.maxScale&&null!==e.maxScale&&(s.maxScale=e.maxScale),void 0!==e.minScale&&null!==e.minScale&&(s.minScale=e.minScale),void 0!==e.includedInLayerList&&null!==e.includedInLayerList&&(i.layerListSettings.included=e.includedInLayerList),void 0!==e.expanded&&null!==e.expanded&&(i.layerListSettings.expanded=e.expanded),void 0!==e.childrenIncludedInLayerList&&null!==e.childrenIncludedInLayerList&&(i.layerListSettings.childrenIncluded=e.childrenIncludedInLayerList)}