var __extends=this&&this.__extends||function(){var i=function(e,t){return(i=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])})(e,t)};return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function r(){this.constructor=e}i(e,t),e.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}}();import EventTarget from"../events/Target.js";import EventType from"../events/EventType.js";import ImageTile from"../ImageTile.js";import TileState from"../TileState.js";import WebGLArrayBuffer from"./Buffer.js";import{ARRAY_BUFFER,STATIC_DRAW}from"../webgl.js";import{toSize}from"../size.js";function bindAndConfigure(e,t){e.bindTexture(e.TEXTURE_2D,t),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.LINEAR),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.LINEAR)}function uploadImageTexture(e,t,r){bindAndConfigure(e,t),e.texImage2D(e.TEXTURE_2D,0,e.RGBA,e.RGBA,e.UNSIGNED_BYTE,r)}function uploadDataTexture(e,t,r,i,n){var a,o,s=e.getGL();switch(bindAndConfigure(s,t),n){case 1:a=s.LUMINANCE;break;case 2:a=s.LUMINANCE_ALPHA;break;case 3:a=s.RGB;break;case 4:a=s.RGBA;break;default:throw new Error("Unsupported number of bands: "+n)}r instanceof Float32Array?(o=s.FLOAT,e.getExtension("OES_texture_float"),e.getExtension("OES_texture_float_linear")):o=s.UNSIGNED_BYTE,s.texImage2D(s.TEXTURE_2D,0,a,i[0],i[1],0,a,o,r)}var TileTexture=function(n){function e(e,t,r){var i=n.call(this)||this,t=(i.tile,i.textures=[],i.handleTileChange_=i.handleTileChange_.bind(i),i.size=toSize(t.getTileSize(e.tileCoord[0])),i.bandCount=NaN,i.helper_=r,new WebGLArrayBuffer(ARRAY_BUFFER,STATIC_DRAW));return t.fromArray([0,1,1,1,1,0,0,0]),r.flushBufferData(t),i.coords=t,i.setTile(e),i}return __extends(e,n),e.prototype.setTile=function(e){e!==this.tile&&(this.tile&&this.tile.removeEventListener(EventType.CHANGE,this.handleTileChange_),this.tile=e,this.textures.length=0,this.loaded=e.getState()===TileState.LOADED,this.loaded?this.uploadTile_():e.addEventListener(EventType.CHANGE,this.handleTileChange_))},e.prototype.uploadTile_=function(){var e=this.helper_,t=e.getGL(),r=this.tile;if(r instanceof ImageTile)return l=t.createTexture(),this.textures.push(l),this.bandCount=4,void uploadImageTexture(t,l,r.getImage());var i=r.getData(),r=i instanceof Float32Array,n=this.size[0]*this.size[1],a=(this.bandCount=i.byteLength/(r?4:1)/n,Math.ceil(this.bandCount/4));if(1===a)return l=t.createTexture(),this.textures.push(l),void uploadDataTexture(e,l,i,this.size,this.bandCount);for(var o=r?Float32Array:Uint8Array,s=new Array(a),u=0;u<a;++u){var l=t.createTexture(),h=(this.textures.push(l),u<a-1?4:this.bandCount%4);s[u]=new o(n*h)}for(var T=n*this.bandCount,E=0;E<T;++E){var f=E%this.bandCount,d=f%4,h=(u=Math.floor(f/4))<a-1?4:this.bandCount%4,f=Math.floor(E/this.bandCount);s[u][f*h+d]=i[E]}for(u=0;u<a;++u){h=u<a-1?4:this.bandCount%4;uploadDataTexture(e,l=this.textures[u],s[u],this.size,h)}},e.prototype.handleTileChange_=function(){this.tile.getState()===TileState.LOADED&&(this.loaded=!0,this.uploadTile_(),this.dispatchEvent(EventType.CHANGE))},e.prototype.disposeInternal=function(){var e=this.helper_.getGL();this.helper_.deleteBuffer(this.coords);for(var t=0;t<this.textures.length;++t)e.deleteTexture(this.textures[t]);this.tile.removeEventListener(EventType.CHANGE,this.handleTileChange_)},e}(EventTarget);export default TileTexture;