import{ValueTypes,expressionToGlsl,getStringNumberEquivalent,uniformNameForVariable}from"../style/expressions.js";var ShaderBuilder=function(){function e(){this.uniforms=[],this.attributes=[],this.varyings=[],this.sizeExpression="vec2(1.0)",this.rotationExpression="0.0",this.offsetExpression="vec2(0.0)",this.colorExpression="vec4(1.0)",this.texCoordExpression="vec4(0.0, 0.0, 1.0, 1.0)",this.discardExpression="false",this.rotateWithView=!1}return e.prototype.addUniform=function(e){return this.uniforms.push(e),this},e.prototype.addAttribute=function(e){return this.attributes.push(e),this},e.prototype.addVarying=function(e,t,o){return this.varyings.push({name:e,type:t,expression:o}),this},e.prototype.setSizeExpression=function(e){return this.sizeExpression=e,this},e.prototype.setRotationExpression=function(e){return this.rotationExpression=e,this},e.prototype.setSymbolOffsetExpression=function(e){return this.offsetExpression=e,this},e.prototype.setColorExpression=function(e){return this.colorExpression=e,this},e.prototype.setTextureCoordinateExpression=function(e){return this.texCoordExpression=e,this},e.prototype.setFragmentDiscardExpression=function(e){return this.discardExpression=e,this},e.prototype.setSymbolRotateWithView=function(e){return this.rotateWithView=e,this},e.prototype.getSizeExpression=function(){return this.sizeExpression},e.prototype.getOffsetExpression=function(){return this.offsetExpression},e.prototype.getColorExpression=function(){return this.colorExpression},e.prototype.getTextureCoordinateExpression=function(){return this.texCoordExpression},e.prototype.getFragmentDiscardExpression=function(){return this.discardExpression},e.prototype.getSymbolVertexShader=function(e){var t=this.rotateWithView?"u_offsetScaleMatrix * u_offsetRotateMatrix":"u_offsetScaleMatrix",o=this.attributes,n=this.varyings;return e&&(o=o.concat("vec4 a_hitColor"),n=n.concat({name:"v_hitColor",type:"vec4",expression:"a_hitColor"})),"precision mediump float;\nuniform mat4 u_projectionMatrix;\nuniform mat4 u_offsetScaleMatrix;\nuniform mat4 u_offsetRotateMatrix;\nuniform float u_time;\nuniform float u_zoom;\nuniform float u_resolution;\n".concat(this.uniforms.map(function(e){return"uniform "+e+";"}).join("\n"),"\nattribute vec2 a_position;\nattribute float a_index;\n").concat(o.map(function(e){return"attribute "+e+";"}).join("\n"),"\nvarying vec2 v_texCoord;\nvarying vec2 v_quadCoord;\n").concat(n.map(function(e){return"varying "+e.type+" "+e.name+";"}).join("\n"),"\nvoid main(void) {\n  mat4 offsetMatrix = ").concat(t,";\n  vec2 halfSize = ").concat(this.sizeExpression," * 0.5;\n  vec2 offset = ").concat(this.offsetExpression,";\n  float angle = ").concat(this.rotationExpression,";\n  float offsetX;\n  float offsetY;\n  if (a_index == 0.0) {\n    offsetX = (offset.x - halfSize.x) * cos(angle) + (offset.y - halfSize.y) * sin(angle);\n    offsetY = (offset.y - halfSize.y) * cos(angle) - (offset.x - halfSize.x) * sin(angle);\n  } else if (a_index == 1.0) {\n    offsetX = (offset.x + halfSize.x) * cos(angle) + (offset.y - halfSize.y) * sin(angle);\n    offsetY = (offset.y - halfSize.y) * cos(angle) - (offset.x + halfSize.x) * sin(angle);\n  } else if (a_index == 2.0) {\n    offsetX = (offset.x + halfSize.x) * cos(angle) + (offset.y + halfSize.y) * sin(angle);\n    offsetY = (offset.y + halfSize.y) * cos(angle) - (offset.x + halfSize.x) * sin(angle);\n  } else {\n    offsetX = (offset.x - halfSize.x) * cos(angle) + (offset.y + halfSize.y) * sin(angle);\n    offsetY = (offset.y + halfSize.y) * cos(angle) - (offset.x - halfSize.x) * sin(angle);\n  }\n  vec4 offsets = offsetMatrix * vec4(offsetX, offsetY, 0.0, 0.0);\n  gl_Position = u_projectionMatrix * vec4(a_position, 0.0, 1.0) + offsets;\n  vec4 texCoord = ").concat(this.texCoordExpression,";\n  float u = a_index == 0.0 || a_index == 3.0 ? texCoord.s : texCoord.p;\n  float v = a_index == 2.0 || a_index == 3.0 ? texCoord.t : texCoord.q;\n  v_texCoord = vec2(u, v);\n  u = a_index == 0.0 || a_index == 3.0 ? 0.0 : 1.0;\n  v = a_index == 2.0 || a_index == 3.0 ? 0.0 : 1.0;\n  v_quadCoord = vec2(u, v);\n").concat(n.map(function(e){return"  "+e.name+" = "+e.expression+";"}).join("\n"),"\n}")},e.prototype.getSymbolFragmentShader=function(e){var t=e?"  if (gl_FragColor.a < 0.1) { discard; } gl_FragColor = v_hitColor;":"",o=this.varyings;return e&&(o=o.concat({name:"v_hitColor",type:"vec4",expression:"a_hitColor"})),"precision mediump float;\nuniform float u_time;\nuniform float u_zoom;\nuniform float u_resolution;\n".concat(this.uniforms.map(function(e){return"uniform "+e+";"}).join("\n"),"\nvarying vec2 v_texCoord;\nvarying vec2 v_quadCoord;\n").concat(o.map(function(e){return"varying "+e.type+" "+e.name+";"}).join("\n"),"\nvoid main(void) {\n  if (").concat(this.discardExpression,") { discard; }\n  gl_FragColor = ").concat(this.colorExpression,";\n  gl_FragColor.rgb *= gl_FragColor.a;\n").concat(t,"\n}")},e}();function parseLiteralStyle(o){var e=o.symbol,t=void 0!==e.size?e.size:1,n=e.color||"white",i=e.textureCoord||[0,0,1,1],s=e.offset||[0,0],r=void 0!==e.opacity?e.opacity:1,a=void 0!==e.rotation?e.rotation:0,f={inFragmentShader:!1,variables:[],attributes:[],stringLiteralsMap:{},functions:{}},c=expressionToGlsl(f,t,ValueTypes.NUMBER_ARRAY|ValueTypes.NUMBER),s=expressionToGlsl(f,s,ValueTypes.NUMBER_ARRAY),i=expressionToGlsl(f,i,ValueTypes.NUMBER_ARRAY),a=expressionToGlsl(f,a,ValueTypes.NUMBER),l={inFragmentShader:!0,variables:f.variables,attributes:[],stringLiteralsMap:f.stringLiteralsMap,functions:{}},n=expressionToGlsl(l,n,ValueTypes.COLOR),r=expressionToGlsl(l,r,ValueTypes.NUMBER),u="1.0",p="vec2(".concat(expressionToGlsl(l,t,ValueTypes.NUMBER_ARRAY|ValueTypes.NUMBER),").x");switch(e.symbolType){case"square":case"image":break;case"circle":u="(1.0-smoothstep(1.-4./".concat(p,",1.,dot(v_quadCoord-.5,v_quadCoord-.5)*4.))");break;case"triangle":var x="(v_quadCoord*2.-1.)",h="(atan(".concat(x,".x,").concat(x,".y))"),u="(1.0-smoothstep(.5-3./".concat(p,",.5,cos(floor(.5+").concat(h,"/2.094395102)*2.094395102-").concat(h,")*length(").concat(x,")))");break;default:throw new Error("Unexpected symbol type: "+e.symbolType)}var d=(new ShaderBuilder).setSizeExpression("vec2(".concat(c,")")).setRotationExpression(a).setSymbolOffsetExpression(s).setTextureCoordinateExpression(i).setSymbolRotateWithView(!!e.rotateWithView).setColorExpression("vec4(".concat(n,".rgb, ").concat(n,".a * ").concat(r," * ").concat(u,")")),y=(o.filter&&(t=expressionToGlsl(l,o.filter,ValueTypes.BOOLEAN),d.setFragmentDiscardExpression("!".concat(t))),{});return l.variables.forEach(function(t){var e=uniformNameForVariable(t);d.addUniform("float ".concat(e)),y[e]=function(){if(!o.variables||void 0===o.variables[t])throw new Error("The following variable is missing from the style: ".concat(t));var e=o.variables[t];return void 0!==(e="string"==typeof e?getStringNumberEquivalent(f,e):e)?e:-9999999}}),"image"===e.symbolType&&e.src&&((c=new Image).crossOrigin=void 0===e.crossOrigin?"anonymous":e.crossOrigin,c.src=e.src,d.addUniform("sampler2D u_texture").setColorExpression(d.getColorExpression()+" * texture2D(u_texture, v_texCoord)"),y.u_texture=c),l.attributes.forEach(function(e){-1===f.attributes.indexOf(e)&&f.attributes.push(e),d.addVarying("v_".concat(e),"float","a_".concat(e))}),f.attributes.forEach(function(e){d.addAttribute("float a_".concat(e))}),{builder:d,attributes:f.attributes.map(function(o){return{name:o,callback:function(e,t){t=t[o];return void 0!==(t="string"==typeof t?getStringNumberEquivalent(f,t):t)?t:-9999999}}}),uniforms:y}}export{ShaderBuilder,parseLiteralStyle};