// All material copyright ESRI, All Rights Reserved, unless otherwise specified.
// See https://js.arcgis.com/4.9/esri/copyright.txt for details.
//>>built
define("require exports ../../../../core/tsSupport/assignHelper ../../../../core/PooledArray ../../../../core/urlUtils ./I3SUtil ../../support/orientedBoundingBox".split(" "),function(n,y,p,m,q,g,r){var t="version level sharedResource attributeData geometryData textureData lodSelection".split(" ");n=function(){function c(b,a,h,d,e,k,c,f,v,w,g,l){var u=this;this.rootId=h;this.progressiveLoadPenalty=d;this.nodeIndex=e;this.streamDataSupplier=k;this.viewportQueries=c;this.logger=f;this._isLoaded=v;this._isSelected=
w;this._enable=g;this._needsUpdate=l;this._prefetching=this._loading=this._dirty=!0;this._loadingNodes=new Set;this._indexMissing=1;this._nodesMissing=0;this._maxUnloadedPrio=Number.NEGATIVE_INFINITY;this._nodeTraversalState={};this._version=0;this._featureEstimate={estimate:0,leafsReached:!1};this._unloadedMemoryEstimate=0;this._missing=new m;this._prefetch=new m;this._updates=new x;this.ignoreServiceOBB=!1;this._maxLodLevel=this.viewportQueries.maxLodLevel;this.rootUrl=q.makeAbsolute(a,b);this.traverseVisible(function(a,
b){return u.nodeTraversalState(b)})}c.prototype.requestReload=function(){this._prefetching=this._loading=this._dirty=!0;++this._version};c.prototype.update=function(b){var a=this;if(this._dirty){this._nodesMissing=this._indexMissing=0;this._maxUnloadedPrio=Number.NEGATIVE_INFINITY;this._missing.clear();this._prefetch.clear();this._updates.reset(b);this._prefetching=this._loading=!1;var h=!0,d=new l,e=new l;this.traverseVisible(function(c,f,k){a._updateFeatureEstimate(f,e);if(f){if(0===a._missing.length&&
f.children)for(c=0,k=f.children;c<k.length;c++){var g=k[c];a.nodeIndex[g.id]||(a._prefetching=!0,a._loadingNodes.has(g.id)||a._prefetch.push(p({},g,{baseUrl:f.baseUrl})))}!f.failed&&f.featureData&&0!==f.featureData.length&&(a._isLoaded(f)?(d.known+=f.memory,++d.knownNodes,a._isSelected(f)?f.children&&(h=!1):(d.unremoved+=f.memory,h=!1),a._needsUpdate(f)&&a._updates.update.push(f)):(f.memory&&(d.known+=f.memory,++d.knownNodes),a._isSelected(f)&&(++a._nodesMissing,f.children&&(h=!1),f.memory?(d.missing+=
f.memory,d.known+=f.memory,++d.knownNodes):++d.missingNodes,0<=b.indexOf(f.id)?a._updates.cancel=a._updates.cancel.filter(function(a){return a!==f.id}):a._enable(f)||a._updates.add.push(f))))}else++a._indexMissing,a._loading=!0,a._prefetching=!1,a._loadingNodes.has(c.id)||a._missing.push(p({},c,{baseUrl:k}))});this._unloadedMemoryEstimate=d.missing-d.unremoved;3<d.knownNodes&&(this._unloadedMemoryEstimate+=d.missingNodes*d.known/d.knownNodes);this._unloadedMemoryEstimate=.8*Math.max(0,this._unloadedMemoryEstimate);
this._featureEstimate.estimate=e.known-e.missing;this._featureEstimate.leafsReached=h;if(0<e.knownNodes){var c=e.missing-e.unremoved+e.known/e.knownNodes*e.missingNodes;0<this._featureEstimate.estimate+c&&(this._featureEstimate.estimate+=c)}this._dirty=0<this._indexMissing||0<this._nodesMissing||0<this._updates.add.length||0<this._updates.update.length||0<this._updates.cancel.length}};c.prototype.checkFeatureTarget=function(b,a){var h=this;a=this.viewportQueries.updateScreenSpaceErrorBias(a);++this._version;
var d=new l;this.traverseVisible(function(a,b){return h._updateFeatureEstimate(b,d)});var e=d.known-d.missing;if(0<d.knownNodes){var c=d.missing-d.unremoved+d.known/d.knownNodes*d.missingNodes;0<e+c&&(e+=c)}this.viewportQueries.updateScreenSpaceErrorBias(a);++this._version;return e<=b};c.prototype._updateFeatureEstimate=function(b,a){b&&!b.failed&&b.featureData&&0!==b.featureData.length&&(this._isLoaded(b)?(a.known+=b.numFeatures,++a.knownNodes,this._isSelected(b)||(a.unremoved+=b.numFeatures)):this._isSelected(b)&&
(b.numFeatures?(a.missing+=b.numFeatures,a.known+=b.numFeatures,++a.knownNodes):++a.missingNodes))};c.prototype.load=function(b){var a=this;g.buildTopNodeMap(this._missing,b,function(b){return a.entryPriority(b)});if(0>=this._missing.length)return this._loading;this._maxUnloadedPrio=this.entryPriority(this._missing.front());this._missing.forEach(function(b){return a._loadNode(b)});this._missing.clear();return this._loading};c.prototype.prefetch=function(b){var a=this;g.buildTopNodeMap(this._prefetch,
b,function(b){return a.entryPriority(b)});this._prefetch.forEach(function(b){return a._loadNode(b)});this._prefetch.clear();return this._prefetching};c.prototype.isLoading=function(){return this._loading};c.prototype.isPrefetching=function(){return this._prefetching};c.prototype.getIndexLoading=function(){return this._loadingNodes.size};c.prototype.getIndexMissing=function(){return this._indexMissing};c.prototype.getUnloadedMemoryEstimate=function(){return this._unloadedMemoryEstimate};c.prototype.getNodesMissing=
function(){return this._nodesMissing};c.prototype.getUpdates=function(b){var a=this;g.buildTopNodeMap(this._updates.add,b,function(b){return a.entryPriority(b)},this._maxUnloadedPrio);g.buildTopNodeMap(this._updates.update,Number.POSITIVE_INFINITY,function(b){return a.entryPriority(b)});return this._updates};c.prototype.getFeatureEstimate=function(){return this._featureEstimate};c.prototype.nodeTraversalState=function(b){if(!b)return null;var a=this._nodeTraversalState[b.id];if(a&&a.version===this._version)return a;
var c=this.viewportQueries.getLodLevel(b),d=this.viewportQueries.hasLOD(b),e=!0;if(d)if(b.parentNode){e=this.nodeIndex[b.parentNode.id];if(null==e)return null;e=(e=this._nodeTraversalState[e.id])&&c>e.lodLevel}else e=0<c;if(a)return a.lodLevel=c,a.isChosen=e,a.version=this._version,a;this._nodeTraversalState[b.id]={nodeHasLOD:d,lodLevel:c,isChosen:e,version:this._version};return this._nodeTraversalState[b.id]};c.prototype._loadNode=function(b){var a=this;this._loadingNodes.add(b.id);var c=q.makeAbsolute(b.href,
b.baseUrl);this.streamDataSupplier.request(c,"json").then(function(b,c){var d={id:c.id,mbs:c.mbs,parentNode:c.parentNode,children:c.children,featureData:c.featureData};t.forEach(function(a){d[a]=c.hasOwnProperty(a)?c[a]:null});c.hasOwnProperty("obb")&&!a.ignoreServiceOBB&&(d.obb=r.clone(c.obb),d.hasServiceOBB=!0,a.viewportQueries.updateNode(d.id));a.nodeIndex[d.id]=d;d.baseUrl=b;d.featureData&&1===d.featureData.length&&d.featureData[0].featureRange&&(d.numFeatures=d.featureData[0].featureRange[1]-
d.featureData[0].featureRange[0]+1);a.nodeTraversalState(d);a._loadingNodes.delete(d.id);a._dirty=!0},function(d){a.loadErrorCallback(c);a._loadingNodes.delete(b.id);a._dirty=!0})};c.prototype.entryPriority=function(b){if(b.id===this.rootId)return 0;var a=0,c=this.nodeIndex[b.id];if(null!=c&&null!=c.parentNode){var d=this._nodeTraversalState[c.parentNode.id];null!=d&&(a=d.lodLevel)}for(d=this.progressiveLoadPenalty;c;c=c.parentNode?this.nodeIndex[c.parentNode.id]:null)if(this._isLoaded(c)){d=0;break}b=
this.viewportQueries.distToPOI(b);return-b-a*(b+this.progressiveLoadPenalty)+d};c.prototype.traverseVisible=function(b){var a=this.nodeIndex[this.rootId];a?this._traverse({id:this.rootId,mbs:a.mbs,href:this.rootUrl},a,b,""):b({id:this.rootId,mbs:null,href:this.rootUrl},null,"")};c.prototype._traverse=function(b,a,c,d){if(!a.children||0===a.children.length)this.viewportQueries.isGeometryVisible(a)&&c(b,a,d);else if(this.viewportQueries.isNodeVisible(a)&&(c(b,a,d),b=this.nodeTraversalState(a),!b.nodeHasLOD||
b.lodLevel!==this._maxLodLevel))for(b=0,d=a.children;b<d.length;b++){var e=d[b],g=this.nodeIndex[e.id];g?this._traverse(e,g,c,a.baseUrl):this.viewportQueries.isNodeVisible(e)&&c(e,null,a.baseUrl)}};c.prototype.loadErrorCallback=function(b){this.logger.warn("Error loading node: "+b)};c.prototype.updateStats=function(b){this._nodesMissing&&(b.nodes+=" + "+Math.round(this._nodesMissing));this._indexMissing&&(b.index+=" + "+this._indexMissing);if(this._featureEstimate.estimate){var a=this._featureEstimate.estimate-
b.features;0<a?b.features+=" + "+a:0>a&&(b.features+=" - "+-a)}};return c}();var x=function(){function c(){this.update=new m;this.add=new m;this.cancel=[]}c.prototype.reset=function(b){this.add.clear();this.update.clear();this.cancel=b};return c}(),l=function(){return function(){this.unremoved=this.missingNodes=this.missing=this.knownNodes=this.known=0}}();return n});