/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{__decorate as e}from"tslib";import r from"../core/Collection.js";import{referenceSetter as t}from"../core/collectionUtils.js";import s from"../core/Logger.js";import{isPromiseLike as o}from"../core/promiseUtils.js";import{property as a,subclass as i}from"../core/accessorSupport/decorators.js";import l from"../layers/Layer.js";function n(e,r,t){let s,o;if(e)for(let a=0,i=e.length;a<i;a++){if(s=e.at(a),s?.[r]===t)return s;if("group"===s?.type&&(o=n(s.layers,r,t),o))return o}}const d=d=>{const y=d;let h=class extends y{constructor(...e){super(...e),this.layers=new r;const t=e=>{e.parent&&e.removeFromParent()},o=e=>{e.parent=this,this.layerAdded(e),"elevation"!==e.type&&"base-elevation"!==e.type||s.getLogger(this).error(`Layer 'title:${e.title}, id:${e.id}' of type '${e.type}' is not supported as an operational layer and will therefore be ignored.`)},a=e=>{e.parent=null,this.layerRemoved(e)};this.addHandles([this.layers.on("before-add",e=>{if(e.item===this)return e.preventDefault(),void s.getLogger(this).error("#add()","Cannot add layer to itself.");t(e.item)}),this.layers.on("after-add",e=>o(e.item)),this.layers.on("after-remove",e=>a(e.item))])}destroy(){const e=this.layers.toArray();for(const r of e)r.destroy();this.layers.destroy()}removeChildLayer(e){this.layers.remove(e),super.removeChildLayer?.(e)}get layers(){return this._get("layers")}set layers(e){this._set("layers",t(e,this._get("layers")))}add(e,r){const t=this.layers;if(r=t.getNextIndex(r),e instanceof l){const s=e;s.parent===this?this.reorder(s,r):t.add(s,r)}else o(e)?e.then(e=>{this.destroyed||this.add(e,r)}):s.getLogger(this).error("#add()","The item being added is not a Layer or a Promise that resolves to a Layer.")}addMany(e,r){const t=this.layers;let s=t.getNextIndex(r);e.slice().forEach(e=>{e.parent!==this?(t.add(e,s),s+=1):this.reorder(e,s)})}findLayerById(e){return n(this.layers,"id",e)}findLayerByUid(e){return n(this.layers,"uid",e)}remove(e){return this.layers.remove(e)}removeMany(e){return this.layers.removeMany(e)}removeAll(){return this.layers.removeAll()}reorder(e,r){return this.layers.reorder(e,r)}layerAdded(e){}layerRemoved(e){}};return e([a()],h.prototype,"layers",null),h=e([i("esri.support.LayersMixin")],h),h};export{d as LayersMixin};
