/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["../../../../chunks/_rollupPluginBabelHelpers","../../../../core/Error","../../../../core/events","../../../../core/Logger","../../../../core/mathUtils","../../../../core/maybe","../../../../core/reactiveUtils","../DisplayObject","../../../webgl/BufferObject","../../../webgl/enums","../../../webgl/Texture","../../../webgl/VertexArrayObject","../../../webgl/capabilities/isWebGL2Context"],(function(e,t,r,i,n,s,o,a,l,c,u,h,d){"use strict";return function(a){function m(e){var n;return(n=a.call(this)||this).elementView=e,n.isWrapAround=!1,n._vertices=new Float32Array(20),n._handles=[],n._handles.push(o.watch((()=>n.elementView.element.opacity),(e=>n.opacity=e),o.initial),o.watch((()=>[n.elementView.coords]),(()=>{n.requestRender()}),o.initial),o.when((()=>n.elementView.element.loaded),(()=>{const e=n.elementView.element;n.ready(),"video"===e.type&&s.isSome(e.content)&&n._handles.push(r.on(e.content,"play",(()=>n.requestRender())))}),o.initial)),e.element.load().catch((r=>{i.getLogger("esri.views.2d.layers.MediaLayerView2D").error(new t("element-load-error","Element cannot be displayed",{element:e,error:r}))})),n}e._inheritsLoose(m,a);var w=m.prototype;return w.destroy=function(){var e;this._handles.forEach((e=>e.remove())),null==(e=this.texture)||e.dispose(),this.texture=null},w.beforeRender=function(e){const{context:t}=e,r=this.elementView.element.content;if(s.isSome(r)){const e=r instanceof HTMLImageElement,i=r instanceof HTMLVideoElement,s=e?r.naturalWidth:i?r.videoWidth:r.width,o=e?r.naturalHeight:i?r.videoHeight:r.height;this.texture?i&&!r.paused&&(this.texture.setData(r),this.requestRender(),(d(t.gl)||n.isPowerOfTwo(s)&&n.isPowerOfTwo(o))&&this.texture.generateMipmap()):(this.texture=new u.Texture(t,{pixelFormat:c.PixelFormat.RGBA,dataType:c.PixelType.UNSIGNED_BYTE,samplingMode:c.TextureSamplingMode.LINEAR,wrapMode:c.TextureWrapMode.CLAMP_TO_EDGE,width:s,height:o},r),(d(t.gl)||n.isPowerOfTwo(s)&&n.isPowerOfTwo(o))&&this.texture.generateMipmap(),i&&!r.paused&&this.requestRender())}a.prototype.beforeRender.call(this,e)},w._createTransforms=function(){return null},w.updateDrawCoords=function(e,t){const r=this.elementView.coords;if(s.isNone(r))return;const[i,n,o,a]=r.rings[0],l=this._vertices,{x:c,y:u}=e,h=0!==t;h?l.set([n[0]-c,n[1]-u,i[0]-c,i[1]-u,o[0]-c,o[1]-u,a[0]-c,a[1]-u,a[0]-c,a[1]-u,n[0]+t-c,n[1]-u,n[0]+t-c,n[1]-u,i[0]+t-c,i[1]-u,o[0]+t-c,o[1]-u,a[0]+t-c,a[1]-u]):l.set([n[0]-c,n[1]-u,i[0]-c,i[1]-u,o[0]-c,o[1]-u,a[0]-c,a[1]-u]),this.isWrapAround=h},w.getVAO=function(e,t,r){if(s.isNone(this.elementView.coords))return null;const i=this._vertices;if(this._vao)this._geometryVbo.setData(i);else{this._geometryVbo=l.BufferObject.createVertex(e,c.Usage.DYNAMIC_DRAW,i);const n=l.BufferObject.createVertex(e,c.Usage.STATIC_DRAW,new Uint16Array([0,0,0,1,1,0,1,1,1,1,0,0,0,0,0,1,1,0,1,1]));this._vao=new h.VertexArrayObject(e,r,t,{geometry:this._geometryVbo,tex:n})}return this._vao},e._createClass(m,[{key:"dvsMat3",get:function(){return this.parent.dvsMat3}}]),m}(a.DisplayObject)}));
