/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["../../../../chunks/_rollupPluginBabelHelpers","../../../../core/maybe","../../../../core/PooledArray","./I3SNode","../support/I3SLayerView"],(function(e,i,n,o,t){"use strict";let s=function(){function n(e){this.layerView=e,this._lodGlobalDirty=!1}var s=n.prototype;return s.startNodeLoading=function(e,i,n,o){this._maxLodLevel=o.maxLodLevel,this._index=n,this._isNodeInScaleBounds=e,this._removeNodes=i},s.shouldLoadNode=function(e){if(i.isNone(e))return!1;const n=this._index.nodeTraversalState(e);return!!this._isChosenMaxLOD(n)||!!n.isChosen&&this._childrenRequireLoading(e)},s.setLodGlobalDirty=function(){this._lodGlobalDirty=!0},s.lodGlobalHandling=function(e){if(!this.requiresLODGlobalHandling)return!1;this._lodGlobalDirty=!1;const i=this.layerView.view.resourceController.memoryController.usedMemory,n=Math.max(0,Math.floor(10*(i-1)));d.clear(),this._lodGlobalHandling(this._index.rootNode,n,!1,this.layerView.nodeCrossfadingEnabled);const o=d.length;this._removeNodes(d,e);const t=d.length<o;return 0!==d.length&&(this._lodGlobalDirty=!0),d.clear(),t},s._lodGlobalHandling=function(e,n,s,r){if(i.isNone(e))return!1;const l=e.index,a=this._index,h=this.layerView,u=a.nodeTraversalState(e),c=this._isChosenMaxLOD(u),_=!e.resources.hasFeatureData;if(c&&_)return e.childrenLoaded>0&&this._removeChildrenRecursive(e),!0;const x=h.isNodeLoaded(l);if(r&&x&&c){const i=!s&&this.hasNoVisibleChildren(e);h.fadeNode(l,t.FadeDirection.FadeIn,!i)}const f=x&&(!h.isNodeFullyFadedIn||h.isNodeFullyFadedIn(l));if(x&&(h.updateNodeState(l,c?o.NodeState.Leaf:o.NodeState.Hole),c))return f&&this._removeChildrenRecursive(e),f;const L=e.childCount>0;let N=L;if(L)for(let o=0;o<e.childCount;o++){const e=a.getChildIndex(l,o),t=a.getNode(e);if(i.isSome(t)){!(!a.isGeometryVisible(e)||this._lodGlobalHandling(t,n,s||f,r))&&this._isNodeInScaleBounds(t)&&(N=!1)}else a.isNodeVisible(e)&&(N=!1)}const y=x&&!c&&(N||d.length<n);y&&d.push(l),!r||y||!x||s||N||h.fadeNode(l,t.FadeDirection.FadeIn,!1);const b=!e.resources.hasFeatureData;return N||f&&!y||b},s._removeChildrenRecursive=function(e){this._index.traverseChildren(e,(e=>((this.layerView.isNodeLoaded(e.index)||this.layerView.isNodeReloading(e.index))&&d.push(e.index),e.childrenLoaded>0)))},s.hasNoVisibleChildren=function(e){let i=!0;return this._index.traverseChildren(e,(e=>!(!i||!this._index.isNodeVisible(e.index))&&(this.layerView.isNodeLoaded(e.index)?(i=!1,!1):e.childrenLoaded>0))),i},s._childrenRequireLoading=function(e){let i=!1,n=!0;return this._index.traverseChildren(e,(e=>{if(!n||!this._index.isNodeVisible(e.index))return!1;const o=this._index.nodeTraversalState(e);return this._isChosenMaxLOD(o)&&this._index.isGeometryVisible(e.index)&&(i=!0),this.layerView.isNodeLoaded(e.index)?(n=!1,!1):e.childrenLoaded>0})),n&&i},s._isChosenMaxLOD=function(e){return e.isChosen&&(!e.nodeHasLOD||e.lodLevel===this._maxLodLevel)},e._createClass(n,[{key:"requiresLODGlobalHandling",get:function(){return null!=this._index&&!0===this._lodGlobalDirty}}]),n}();const d=new n({deallocator:null});return s}));
