/*
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/maybe","../../../../../chunks/vec4f32","../DefaultVertexAttributeLayouts","./WGLBrush","../shaders/BackgroundPrograms","../shaders/TileInfoPrograms","../../../../webgl/BufferObject","../../../../webgl/enums","../../../../webgl/ProgramTemplate","../../../../webgl/Texture","../../../../webgl/VertexArrayObject"],(function(e,t,r,i,n,o,s,a,l,u,c,_){"use strict";const f=300,g=32;let d=function(n){function d(){var e;return(e=n.apply(this,arguments)||this)._color=r.fromValues(1,0,0,1),e}e._inheritsLoose(d,n);var h=d.prototype;return h.dispose=function(){this._outlineProgram&&(this._outlineProgram.dispose(),this._outlineProgram=null),this._tileInfoProgram&&(this._tileInfoProgram.dispose(),this._tileInfoProgram=null),this._outlineVertexArrayObject&&(this._outlineVertexArrayObject.dispose(),this._outlineVertexArrayObject=null),this._tileInfoVertexArrayObject&&(this._tileInfoVertexArrayObject.dispose(),this._tileInfoVertexArrayObject=null),this._canvas=null},h.prepareState=function({context:e}){e.setBlendingEnabled(!0),e.setBlendFunctionSeparate(l.BlendFactor.ONE,l.BlendFactor.ONE_MINUS_SRC_ALPHA,l.BlendFactor.ONE,l.BlendFactor.ONE_MINUS_SRC_ALPHA),e.setColorMask(!0,!0,!0,!0),e.setStencilWriteMask(0),e.setStencilTestEnabled(!1)},h.draw=function(e,r){const{context:i,requestRender:n}=e;if(!r.isReady)return;if(this._loadWGLResources(i),t.isSome(n)&&(!this._outlineProgram.isCompiled||!this._tileInfoProgram.isCompiled))return void n();i.bindVAO(this._outlineVertexArrayObject),i.useProgram(this._outlineProgram),this._outlineProgram.setUniformMatrix3fv("u_dvsMat3",r.transforms.dvs),this._outlineProgram.setUniform2f("u_coord_range",r.rangeX,r.rangeY),this._outlineProgram.setUniform1f("u_depth",0),this._outlineProgram.setUniform4fv("u_color",this._color),i.drawArrays(l.PrimitiveType.LINE_STRIP,0,4);const o=this._getTexture(i,r);o?(i.bindVAO(this._tileInfoVertexArrayObject),i.useProgram(this._tileInfoProgram),i.bindTexture(o,0),this._tileInfoProgram.setUniformMatrix3fv("u_dvsMat3",r.transforms.dvs),this._tileInfoProgram.setUniform1f("u_depth",0),this._tileInfoProgram.setUniform2f("u_coord_ratio",r.rangeX/r.width,r.rangeY/r.height),this._tileInfoProgram.setUniform2f("u_delta",8,8),this._tileInfoProgram.setUniform2f("u_dimensions",o.descriptor.width,o.descriptor.height),i.drawArrays(l.PrimitiveType.TRIANGLE_STRIP,0,4),i.bindVAO()):i.bindVAO()},h._loadWGLResources=function(e){if(this._outlineProgram&&this._tileInfoProgram)return;const t=u.createProgram(e,o.background),r=u.createProgram(e,s.tileInfo),n=new Int8Array([0,0,1,0,1,1,0,1]),c=a.BufferObject.createVertex(e,l.Usage.STATIC_DRAW,n),f=new _.VertexArrayObject(e,o.background.attributes,i.Pos2b,{geometry:c}),g=new Int8Array([0,0,1,0,0,1,1,1]),d=a.BufferObject.createVertex(e,l.Usage.STATIC_DRAW,g),h=new _.VertexArrayObject(e,s.tileInfo.attributes,i.Pos2b,{geometry:d});this._outlineProgram=t,this._tileInfoProgram=r,this._outlineVertexArrayObject=f,this._tileInfoVertexArrayObject=h},h._getTexture=function(e,t){if(t.texture&&t.triangleCountReportedInDebug===t.triangleCount)return t.texture;t.triangleCountReportedInDebug=t.triangleCount,this._canvas||(this._canvas=document.createElement("canvas"),this._canvas.setAttribute("id","canvas2d"),this._canvas.setAttribute("width",`${f}`),this._canvas.setAttribute("height",`${g}`),this._canvas.setAttribute("style","display:none"));const r=t.triangleCount;let i=t.key.id;t.triangleCount>0&&(i+=`, ${r}`);const n=this._canvas,o=n.getContext("2d");return o.font="24px sans-serif",o.textAlign="left",o.textBaseline="top",o.clearRect(0,0,f,g),r>1e5?(o.fillStyle="red",o.fillRect(0,0,f,g),o.fillStyle="black"):(o.clearRect(0,0,f,g),o.fillStyle="blue"),o.fillText(i,0,0),t.texture=new c.Texture(e,{target:l.TextureType.TEXTURE_2D,pixelFormat:l.PixelFormat.RGBA,dataType:l.PixelType.UNSIGNED_BYTE,samplingMode:l.TextureSamplingMode.NEAREST,wrapMode:l.TextureWrapMode.CLAMP_TO_EDGE},n),t.texture},d}(n);return d}));
