var __extends=this&&this.__extends||function(){var o=function(t,e){return(o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i])})(t,e)};return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function i(){this.constructor=t}o(t,e),t.prototype=null===e?Object.create(e):(i.prototype=e.prototype,new i)}}();import EventType from"../events/EventType.js";import ImageTile from"../ImageTile.js";import ReprojTile from"../reproj/Tile.js";import TileCache from"../TileCache.js";import TileState from"../TileState.js";import UrlTile from"./UrlTile.js";import{ENABLE_RASTER_REPROJECTION}from"../reproj/common.js";import{IMAGE_SMOOTHING_DISABLED}from"./common.js";import{equivalent,get as getProjection}from"../proj.js";import{getKey,getKeyZXY}from"../tilecoord.js";import{getForProjection as getTileGridForProjection}from"../tilegrid.js";import{getUid}from"../util.js";var TileImage=function(n){function t(t){var e=n.call(this,{attributions:t.attributions,cacheSize:t.cacheSize,opaque:t.opaque,projection:t.projection,state:t.state,tileGrid:t.tileGrid,tileLoadFunction:t.tileLoadFunction||defaultTileLoadFunction,tilePixelRatio:t.tilePixelRatio,tileUrlFunction:t.tileUrlFunction,url:t.url,urls:t.urls,wrapX:t.wrapX,transition:t.transition,key:t.key,attributionsCollapsible:t.attributionsCollapsible,zDirection:t.zDirection})||this;return e.crossOrigin=void 0!==t.crossOrigin?t.crossOrigin:null,e.tileClass=void 0!==t.tileClass?t.tileClass:ImageTile,e.tileCacheForProjection={},e.tileGridForProjection={},e.reprojectionErrorThreshold_=t.reprojectionErrorThreshold,e.contextOptions_=!1===t.imageSmoothing?IMAGE_SMOOTHING_DISABLED:void 0,e.renderReprojectionEdges_=!1,e}return __extends(t,n),t.prototype.canExpireCache=function(){if(!ENABLE_RASTER_REPROJECTION)return n.prototype.canExpireCache.call(this);if(this.tileCache.canExpireCache())return!0;for(var t in this.tileCacheForProjection)if(this.tileCacheForProjection[t].canExpireCache())return!0;return!1},t.prototype.expireCache=function(t,e){if(ENABLE_RASTER_REPROJECTION){var i,o=this.getTileCacheForProjection(t);for(i in this.tileCache.expireCache(this.tileCache==o?e:{}),this.tileCacheForProjection){var r=this.tileCacheForProjection[i];r.expireCache(r==o?e:{})}}else n.prototype.expireCache.call(this,t,e)},t.prototype.getContextOptions=function(){return this.contextOptions_},t.prototype.getGutterForProjection=function(t){return ENABLE_RASTER_REPROJECTION&&this.getProjection()&&t&&!equivalent(this.getProjection(),t)?0:this.getGutter()},t.prototype.getGutter=function(){return 0},t.prototype.getKey=function(){return n.prototype.getKey.call(this)+(this.contextOptions_?"\n"+JSON.stringify(this.contextOptions_):"")},t.prototype.getOpaque=function(t){return!(ENABLE_RASTER_REPROJECTION&&this.getProjection()&&t&&!equivalent(this.getProjection(),t))&&n.prototype.getOpaque.call(this,t)},t.prototype.getTileGridForProjection=function(t){if(!ENABLE_RASTER_REPROJECTION)return n.prototype.getTileGridForProjection.call(this,t);var e=this.getProjection();return!this.tileGrid||e&&!equivalent(e,t)?((e=getUid(t))in this.tileGridForProjection||(this.tileGridForProjection[e]=getTileGridForProjection(t)),this.tileGridForProjection[e]):this.tileGrid},t.prototype.getTileCacheForProjection=function(t){if(!ENABLE_RASTER_REPROJECTION)return n.prototype.getTileCacheForProjection.call(this,t);var e=this.getProjection();return!e||equivalent(e,t)?this.tileCache:((e=getUid(t))in this.tileCacheForProjection||(this.tileCacheForProjection[e]=new TileCache(this.tileCache.highWaterMark)),this.tileCacheForProjection[e])},t.prototype.createTile_=function(t,e,i,o,r,n){t=[t,e,i],e=this.getTileCoordForTileUrlFunction(t,r),i=e?this.tileUrlFunction(e,o,r):void 0,e=new this.tileClass(t,void 0!==i?TileState.IDLE:TileState.EMPTY,void 0!==i?i:"",this.crossOrigin,this.tileLoadFunction,this.tileOptions);return e.key=n,e.addEventListener(EventType.CHANGE,this.handleTileChange.bind(this)),e},t.prototype.getTile=function(t,e,i,o,r){var n,l,c,s,a,h,p,u,T=this.getProjection();return ENABLE_RASTER_REPROJECTION&&T&&r&&!equivalent(T,r)?(n=this.getTileCacheForProjection(r),c=void 0,s=getKey(l=[t,e,i]),n.containsKey(s)&&(c=n.get(s)),a=this.getKey(),c&&c.key==a?c:(u=this.getTileGridForProjection(T),h=this.getTileGridForProjection(r),p=this.getTileCoordForTileUrlFunction(l,r),(u=new ReprojTile(T,u,r,h,l,p,this.getTilePixelRatio(o),this.getGutter(),function(t,e,i,o){return this.getTileInternal(t,e,i,o,T)}.bind(this),this.reprojectionErrorThreshold_,this.renderReprojectionEdges_,this.contextOptions_)).key=a,c?(u.interimTile=c,u.refreshInterimChain(),n.replace(s,u)):n.set(s,u),u)):this.getTileInternal(t,e,i,o,T||r)},t.prototype.getTileInternal=function(t,e,i,o,r){var n,l=null,c=getKeyZXY(t,e,i),s=this.getKey();return this.tileCache.containsKey(c)?(l=this.tileCache.get(c)).key!=s&&(n=l,l=this.createTile_(t,e,i,o,r,s),n.getState()==TileState.IDLE?l.interimTile=n.interimTile:l.interimTile=n,l.refreshInterimChain(),this.tileCache.replace(c,l)):(l=this.createTile_(t,e,i,o,r,s),this.tileCache.set(c,l)),l},t.prototype.setRenderReprojectionEdges=function(t){if(ENABLE_RASTER_REPROJECTION&&this.renderReprojectionEdges_!=t){for(var e in this.renderReprojectionEdges_=t,this.tileCacheForProjection)this.tileCacheForProjection[e].clear();this.changed()}},t.prototype.setTileGridForProjection=function(t,e){!ENABLE_RASTER_REPROJECTION||!(t=getProjection(t))||(t=getUid(t))in this.tileGridForProjection||(this.tileGridForProjection[t]=e)},t}(UrlTile);function defaultTileLoadFunction(t,e){t.getImage().src=e}export default TileImage;