/*
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","../../../chunks/tslib.es6","../../../core/Accessor","../../../core/Handles","../../../core/maybe","../../../core/reactiveUtils","../../../core/accessorSupport/decorators/property","../../../core/arrayUtils","../../../core/has","../../../core/accessorSupport/ensureType","../../../core/accessorSupport/decorators/subclass","../../../geometry/projection","../../../geometry/support/aaBoundingRect","../../../geometry/support/spatialReferenceUtils","../../../layers/support/layerUtils","../../ViewingMode","./terrainUtils","./TilingScheme"],(function(e,t,i,n,o,s,r,c,l,a,h,p,d,g,S,u,y,m,_){"use strict";function T(e,t,i){return s.isSome(m.getTiledLayerInfo(e,t,i))}e.TilingSchemeLogic=function(e){function i(t){var i;return(i=e.call(this,t)||this)._handles=new o,i}t._inheritsLoose(i,e);var n=i.prototype;return n.initialize=function(){this._handles.add(this.layers.on("change",(()=>this._update()))),this._handles.add(r.watch((()=>this.extentHelper.layerViewsExtent),(()=>this._setAdHocTilingScheme()))),this._update(),this.tilingSchemeLocked||this._setAdHocTilingScheme()},n.destroy=function(){this._handles=s.destroyMaybe(this._handles),this._waitTask=null},n._update=function(){if(this._waitTask=null,this.tilingSchemeLocked)return;let e;if(this.layers.some((t=>!(!u.isTiledLayer(t)||t.isRejected())&&(!(t.isFulfilled()&&!T(t,this.viewSpatialReference,this.viewingMode))&&(e=t,!("vector-tile"===(null==t?void 0:t.type)||m.isProjectableRasterLayer(t)))))),e)if(e.isResolved()){const t=e,i=m.getTiledLayerInfo(t,this.viewSpatialReference,this.viewingMode);if(s.isSome(i)){const e=new _.TilingScheme(i.tileInfo);this._lockTilingScheme(e)}}else this._updateWhen(e)},n._updateWhen=function(e){const t=e.when().catch((()=>{})).then((()=>{t!==this._waitTask||this.destroyed||this._update()}));this._waitTask=t},n._lockTilingScheme=function(e){if(this.viewingMode===y.ViewingMode.Global){const t=e.levels.length-1;e.spatialReference.isWebMercator?e=_.TilingScheme.makeWebMercatorAuxiliarySphere(t):d.canProjectToWGS84ComparableLonLat(e.spatialReference)&&(e=_.TilingScheme.makeGCSWithTileSize(e.spatialReference,e.pixelSize,t))}this.tilingSchemeLocked=!0,this.tilingScheme=e,this.extentHelper.tilingScheme=this.tilingScheme,this._updateTiledLayerExtent(),this._handles.removeAll(),this._handles.add(r.watch((()=>this.extentHelper.tiledLayersExtent),(()=>this._updateTiledLayerExtent())))},n._updateTiledLayerExtent=function(){this._set("extent",this.extentHelper.tiledLayersExtent)},n._setAdHocTilingScheme=function(){if(this.viewingMode===y.ViewingMode.Global){const e=this.extentHelper.viewSpatialReference,t=d.canProjectToWGS84ComparableLonLat(e)||S.isMars(e)||S.isMoon(e);e.isWebMercator?this.tilingScheme=_.TilingScheme.WebMercatorAuxiliarySphere:t&&(this.tilingScheme=_.TilingScheme.makeGCSWithTileSize(e,256)),this._set("extent",this.extentHelper.layerViewsExtent)}else{const e=this.extentHelper.layerViewsExtent;s.isSome(e)&&!g.equals(e,this.extent)&&(this.tilingScheme=_.TilingScheme.fromExtent(e,this.extentHelper.viewSpatialReference),this._set("extent",e))}},t._createClass(i,[{key:"test",get:function(){return{lockTilingScheme:e=>this._lockTilingScheme(e),done:!this._waitTask}}}]),i}(n),i.__decorate([c.property()],e.TilingSchemeLogic.prototype,"tilingScheme",void 0),i.__decorate([c.property({readOnly:!0})],e.TilingSchemeLogic.prototype,"extent",void 0),i.__decorate([c.property({value:!1})],e.TilingSchemeLogic.prototype,"tilingSchemeLocked",void 0),i.__decorate([c.property({constructOnly:!0})],e.TilingSchemeLogic.prototype,"viewSpatialReference",void 0),i.__decorate([c.property({constructOnly:!0})],e.TilingSchemeLogic.prototype,"layers",void 0),i.__decorate([c.property({constructOnly:!0})],e.TilingSchemeLogic.prototype,"extentHelper",void 0),i.__decorate([c.property({constructOnly:!0})],e.TilingSchemeLogic.prototype,"viewingMode",void 0),e.TilingSchemeLogic=i.__decorate([p.subclass("esri.views.3d.terrain.TilingSchemeLogic")],e.TilingSchemeLogic),Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
