/*
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","../../../../../../core/screenUtils","../../../../../../layers/graphics/featureConversionUtils","../../../../../../symbols/cim/placements/CIMMarkerPlacementHelper","../../definitions","../../enums","../../number","../../materialKey/MaterialKey","./shapingUtils"],(function(e,t,i,r,n,s,o,a,h,l){"use strict";const x=8;return c=>function(c){function d(...e){var t;return(t=c.call(this,...e)||this)._isCIM=!1,t._vertexBoundsScale=1,t.geometryType=o.WGLGeometryType.TEXT,t._aux=a.i8888to32(0,0,t._referenceSize,t._bitset),t}e._inheritsLoose(d,c);var f=d.prototype;return f.bindTextInfo=function(e,i){e&&e.length?this._shapingInfo=t.applySome(e,(e=>l.shapeGlyphs(e,i,{scale:this._scale,angle:this._angle,xOffset:this._xOffset,yOffset:this._yOffset,hAlign:this._xAlignD,vAlign:this._yAlignD,maxLineWidth:Math.max(32,Math.min(this._lineWidth,512)),lineHeight:s.MAGIC_LABEL_LINE_HEIGHT*Math.max(.25,Math.min(this._lineHeight,4)),decoration:this._decoration,isCIM:this._isCIM}))):this._shapingInfo=null},f._write=function(e,t,i,r){const n=t.getDisplayId();this._writeGeometry(e,t,n,i,r)},f._writeGeometry=function(e,i,n,s,o){const a=this._shapingInfo;if(t.isNone(a))return;if(t.isSome(this._textPlacement)){const t=null!=o?o:i.readLegacyGeometryForDisplay();return this._writePlacedText(e,n,t,a,s)}const h=o?r.deltaDecodeGeometry(r.convertFromGeometry(o),2):"esriGeometryPolygon"===i.geometryType?i.readCentroid():i.readGeometryForDisplay();if(!t.isNone(h)){if(h.isPoint){const[t,i]=h.coords;if(!e.hasAggregates&&e.hasPixelBufferEnabled&&(t<0||t>=512||i<0||i>=512))return;return this._writeGlyphs(e,n,{x:t,y:i},a)}h.forEachVertex(((t,i)=>this._writeGlyphs(e,n,{x:t,y:i},a)))}},f._writePlacedText=function(e,r,s,o,a){const h=t.unwrap(this._textPlacement),l=n.CIMMarkerPlacementHelper.getPlacement(s,h,i.pt2px(1),a.geometryEngine);if(!l)return;let x=l.next();for(;null!=x;){const t=-x.getAngle();o.setRotation(t);const i=x.tx,n=-x.ty;i<0||i>=512||n<0||n>=512?x=l.next():(this._writeGlyphs(e,r,{x:i,y:n},o),o.setRotation(-t),x=l.next())}},f._writeGlyphs=function(e,t,i,r){const n=h.MaterialKeyBase.load(this._materialKey),s=a.i1616to32(Math.round(x*i.x),Math.round(x*i.y)),o=this._vertexBoundsScale,l=r.bounds,c=2*Math.max(l.width,l.height);for(const a of r.glyphs)n.textureBinding=a.textureBinding,e.recordStart(t,n.data,this.geometryType,!0),e.vertexBounds(i.x+l.x+this._xOffset,i.y+l.y-this._yOffset,c*o,c*o),this._writeVertices(e,t,s,a),e.recordEnd()},f._writeGlyph=function(e,t,i,r,n){const s=h.MaterialKeyBase.load(this._materialKey),o=a.i1616to32(Math.round(x*i),Math.round(x*r));s.textureBinding=n.textureBinding,e.recordStart(t,s.data,this.geometryType,!0);const l=n.bounds,c=this._vertexBoundsScale;e.vertexBounds(i+l.x*c,r+l.y*c,l.width*c,l.height*c),this._writeVertices(e,t,o,n),e.recordEnd()},f._writeVertices=function(e,t,i,r){const n=e.vertexCount();this._writeVertexCommon(e,t,i,r),e.vertexWrite(r.offsets.upperLeft),e.vertexWrite(r.texcoords.upperLeft),e.vertexEnd(),this._writeVertexCommon(e,t,i,r),e.vertexWrite(r.offsets.upperRight),e.vertexWrite(r.texcoords.upperRight),e.vertexEnd(),this._writeVertexCommon(e,t,i,r),e.vertexWrite(r.offsets.lowerLeft),e.vertexWrite(r.texcoords.lowerLeft),e.vertexEnd(),this._writeVertexCommon(e,t,i,r),e.vertexWrite(r.offsets.lowerRight),e.vertexWrite(r.texcoords.lowerRight),e.vertexEnd(),e.indexWrite(n+0),e.indexWrite(n+1),e.indexWrite(n+2),e.indexWrite(n+1),e.indexWrite(n+3),e.indexWrite(n+2)},f._writeVertexCommon=function(e,t,i,r){const n=this._color,s=this._haloColor,o=a.i8888to32(0,0,this._referenceSize,this._bitset),h=a.i8888to32(0,0,this._size,this._haloSize);e.vertexWrite(i),e.vertexWrite(t),e.vertexWrite(n),e.vertexWrite(s),e.vertexWrite(h),e.vertexWrite(o),e.vertexWrite(this._minMaxZoom)},d}(c)}));
