/*
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/Logger","../../../../core/maybe","../../../../chunks/earcut","../../../../chunks/vec2","../../../../chunks/vec2f64","../../../../layers/graphics/featureConversionUtils","../../../../layers/graphics/OptimizedGeometry","./number","../../../webgl/BufferObject","../../../webgl/enums"],(function(e,t,r,n,o,i,c,s,a,f,h,u){"use strict";const y=r.getLogger("esri.views.2d.engine.webgl.Mesh2D"),m=e=>{switch(e.BYTES_PER_ELEMENT){case 1:return u.DataType.UNSIGNED_BYTE;case 2:return u.DataType.UNSIGNED_SHORT;case 4:return u.DataType.UNSIGNED_INT;default:throw new t("Cannot get DataType of array")}},g=(e,t,r,n)=>{let o=0;for(let i=1;i<r;i++){const r=e[2*(t+i-1)],n=e[2*(t+i-1)+1];o+=(e[2*(t+i)]-r)*(e[2*(t+i)+1]+n)}return n?o>0:o<0},l=({coords:e,lengths:t},r)=>{const n=[];for(let i=0,c=0;i<t.length;c+=t[i],i+=1){const s=c,a=[];for(;i<t.length-1&&g(e,c+t[i],t[i+1],r);i+=1,c+=t[i])a.push(c+t[i]-s);const f=e.slice(2*s,2*(c+t[i])),h=o.earcut(f,a,2);for(const e of h)n.push(e+s)}return n};return function(){function r(e,t,r,n=!1){this._cache={},this.vertices=e,this.indices=t,this.primitiveType=r,this.isMapSpace=n}r.fromRect=function({x:e,y:t,width:n,height:o}){const i=e,c=t,s=i+n,a=c+o;return r.fromScreenExtent({xmin:i,ymin:c,xmax:s,ymax:a})},r.fromPath=function(e){const t=s.convertFromNestedArray(new a,e.path,!1,!1),n=t.coords,o=new Uint32Array(l(t,!0)),i=new Uint32Array(n.length/2);for(let r=0;r<i.length;r++)i[r]=f.i1616to32(Math.floor(n[2*r]),Math.floor(n[2*r+1]));return new r({geometry:i},o,u.PrimitiveType.TRIANGLES)},r.fromGeometry=function(e,n){const o=n.geometry.type;switch(o){case"polygon":return r.fromPolygon(e,n.geometry);case"extent":return r.fromMapExtent(e,n.geometry);default:return y.error(new t("mapview-bad-type",`Unable to create a mesh from type ${o}`,n)),r.fromRect({x:0,y:0,width:1,height:1})}},r.fromPolygon=function(e,t){const n=s.convertFromPolygon(new a,t,!1,!1),o=n.coords,h=new Uint32Array(l(n,!1)),y=new Uint32Array(o.length/2),m=c.create(),g=c.create();for(let r=0;r<y.length;r++)i.set(m,o[2*r],o[2*r+1]),e.toScreen(g,m),y[r]=f.i1616to32(Math.floor(g[0]),Math.floor(g[1]));return new r({geometry:y},h,u.PrimitiveType.TRIANGLES,!0)},r.fromScreenExtent=function({xmin:e,xmax:t,ymin:n,ymax:o}){return new r({geometry:new Uint32Array([f.i1616to32(e,n),f.i1616to32(t,n),f.i1616to32(e,o),f.i1616to32(e,o),f.i1616to32(t,n),f.i1616to32(t,o)])},new Uint32Array([0,1,2,3,4,5]),u.PrimitiveType.TRIANGLES)},r.fromMapExtent=function(e,t){const[n,o]=e.toScreen([0,0],[t.xmin,t.ymin]),[i,c]=e.toScreen([0,0],[t.xmax,t.ymax]);return new r({geometry:new Uint32Array([f.i1616to32(n,o),f.i1616to32(i,o),f.i1616to32(n,c),f.i1616to32(n,c),f.i1616to32(i,o),f.i1616to32(i,c)])},new Uint32Array([0,1,2,3,4,5]),u.PrimitiveType.TRIANGLES)};var o=r.prototype;return o.destroy=function(){n.isSome(this._cache.indexBuffer)&&this._cache.indexBuffer.dispose();for(const e in this._cache.vertexBuffers)n.isSome(this._cache.vertexBuffers[e])&&this._cache.vertexBuffers[e].dispose()},o.getIndexBuffer=function(e,t=u.Usage.STATIC_DRAW){return this._cache.indexBuffer||(this._cache.indexBuffer=h.BufferObject.createIndex(e,t,this.indices)),this._cache.indexBuffer},o.getVertexBuffers=function(e,t=u.Usage.STATIC_DRAW){return this._cache.vertexBuffers||(this._cache.vertexBuffers=Object.keys(this.vertices).reduce(((r,n)=>({...r,[n]:h.BufferObject.createVertex(e,t,this.vertices[n])})),{})),this._cache.vertexBuffers},e._createClass(r,[{key:"elementType",get:function(){return m(this.indices)}}]),r}()}));
