/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{__decorate as e}from"tslib";import s from"../../core/Accessor.js";import t from"../../core/Collection.js";import{referenceSetter as r}from"../../core/collectionUtils.js";import{isAbortError as o}from"../../core/promiseUtils.js";import{property as i,subclass as a}from"../../core/accessorSupport/decorators.js";import{findFullModelSublayer as l}from"./support/buildingLayerUtils.js";import{LayerTreeNode as d}from"./support/LayerTreeNode.js";import{createLoadLayersFunction as n}from"./support/layerUtils.js";let y=class extends s{constructor(e){super(e),this.root=new d,this.state="disabled",this._loadLayers=n(),this.layers=new t}initialize(){this.addHandles(this.layers.on("change",()=>this._onLayersChange())),this._onLayersChange()}destroy(){this._set("state","disabled"),this.root.destroy()}set layers(e){const s=this._get("layers");this._set("layers",r(e,s))}_updateLayerTree(){this.root.destroy(),this._set("root",new d);const e=new Map,s=this.layers.length>1?"modelName":"id";return this.layers.forEach(t=>{const r=l(t);this._addNodesForSublayers(r??t,this.root,e,s)}),this}_addNodeForLayer(e,s,t,r){const o=String(e[r]).toLowerCase();if(null==o||e.isEmpty)return;const i=`${s.id}/${o}`;let a=t.get(i);a||(a=new d({id:o,level:s.level+1}),t.set(i,a)),a.layers.push(e),s.children.push(a),this._addNodesForSublayers(e,a,t,r)}_addNodesForSublayers(e,s,t,r){("building-scene"===e.type||"building-group"===e.type&&!e.isEmpty)&&e.sublayers.forEach(e=>this._addNodeForLayer(e,s,t,r))}async _onLayersChange(){if(this._set("state","loading"),0!==this.layers.length)try{await this._loadLayers(this.layers),this._updateLayerTree(),this._set("state","ready")}catch(e){o(e)||this._set("state","failed")}}};e([i({readOnly:!0})],y.prototype,"root",void 0),e([i({type:t,nonNullable:!0})],y.prototype,"layers",null),e([i({readOnly:!0,nonNullable:!0})],y.prototype,"state",void 0),y=e([a("esri.widgets.BuildingExplorer.BuildingDisciplinesViewModel")],y);const h=y;export{h as default};
