/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../chunks/_rollupPluginBabelHelpers","../../../core/maybe","./interfaces","./TerrainConst","./terrainUtils","./tileUtils"],(function(e,t,i,s,l,n,a){"use strict";let r=function(){function e(){}var r=e.prototype;return r.init=function(e,t,i,s){this.tile=e,this._layerIdx=t,this._layerClass=i,this._suspended=s,this._tileLayerInfo=e.getLayerInfo(t,i),this._tileRequested=null;const l=this._findAncestorWithData();this._setUpsampleTile(l)},r.startLoading=function(){return this._requestNext()},r.dispose=function(){this._tileRequested&&(this._tileRequested.unrequestLayerData(this._layerIdx,this._layerClass,this),this._tileRequested=null),this.tile=null,this._tileLayerInfo=null},r.setSuspension=function(e){e!==this._suspended&&(this._suspended=e,e?this._tileRequested&&(this._tileRequested.unrequestLayerData(this._layerIdx,this._layerClass,this),this._tileRequested=null):this._tileLayerInfo.data||this.update())},r.update=function(){const e=this._findAncestorWithData();return this._setUpsampleTile(e),this._requestNext()},r.dataArrived=function(e){this._setUpsampleTile(e),this._tileRequested=null,e===this.tile?this.tile.updateRenderData(this._layerClass,s.TextureUpdate.FADING):this._requestNext()},r.dataMissing=function(){this._tileRequested=null,this._tileLayerInfo.data=null,this._requestNext()},r._agentDone=function(){this.tile.agentDone(this._layerIdx,this._layerClass),this.dispose()},r._requestNext=function(){if(this._suspended)return!0;const e=this._findNextDownload();if(this._tileRequested){if(e===this._tileRequested)return!0;this._tileRequested.unrequestLayerData(this._layerIdx,this._layerClass,this),this._tileRequested=null}return i.isSome(e)?e.requestLayerData(this._layerIdx,this._layerClass,this)&&(this._tileRequested=e):this._agentDone(),!!this._tileRequested},r._findNextDownload=function(){const e=this._layerIdx,t=this._layerClass,s=this.tile.surface.layerViewByIndex(e,t),r=n.getLayerWithExtentRange(s),{minLevel:u,maxLevel:o}=s.dataLevelRange,h=this._desiredMinLevelDelta,d=this._progressiveLevelModulo+h,_=this._scaleRangeEnabled?a.fallsWithinLayer:()=>!0;let f=this.tile;const y=f.level;let c;const p=this._tileLayerInfo.upsampleInfo,L=i.isSome(p)?p.tile.level:-1,I=!!i.isSome(p)&&L-y>=h,v=i.get(r,"tilemapCache");for(;f&&_(f,r,!1)&&f.level>=u;){const s=f.level,n=y-s,a=f.layerInfo[t][e];if(a.data&&n>=h){(!I||s>L)&&this._setUpsampleTile(f),a.dataInvalidated&&(c=f);break}if((i.isNone(v)||"unavailable"!==v.getAvailability(f.lij[0],f.lij[1],f.lij[2]))&&s<=o&&!a.data&&!a.dataMissing&&((i.isNone(c)||f.level===u||s%l.PROGRESSIVE_LOADING_MODULO==0||y-c.level<h)&&(c=f),n>=d))break;f=f.parent}return i.isSome(c)&&y-c.level<h&&this._tileLayerInfo.upsampleInfo&&(c=null),c},r._findAncestorWithData=function(){const e=this.tile.vlevel,t=this._desiredMinLevelDelta;let i;for(let s=this.tile;s;s=s.parent)if(s.hasLayerData(this._layerIdx,this._layerClass)){if(e-s.level>=t)return s;i=s}return i},r._setUpsampleTile=function(e){this._tileLayerInfo.setUpsampleInfo(this.tile,e),this.tile.updateRenderData(this._layerClass,s.TextureUpdate.FADING)},t._createClass(e,[{key:"updating",get:function(){return!!this._tileRequested}},{key:"test",get:function(){return{findNextDownload:()=>this._findNextDownload(),tileLayerInfo:this._tileLayerInfo}}}]),e}(),u=function(e){function i(){return e.apply(this,arguments)||this}return t._inheritsLoose(i,e),i.prototype.dispose=function(){},t._createClass(i,[{key:"_desiredMinLevelDelta",get:function(){throw o}},{key:"_progressiveLevelModulo",get:function(){throw o}}]),i}(r);const o=new Error("Abstract method called on TileAgent"),h=new u;e.TILE_AGENT_DONE=h,e.TileAgent=r,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
