/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{isPolyline as e,isPolygon as t}from"../../geometry/support/jsonTypeUtils.js";import{isCurvedGeometry as i}from"../../geometry/support/curves/curveUtils.js";import r from"./CIMResourceManager.js";import{CanvasDrawHelper as s,Transformation as h}from"./CIMSymbolDrawHelper.js";import{CIMSymbolHelper as n}from"./CIMSymbolHelper.js";import{OverrideHelper as o}from"./OverrideHelper.js";import{scale as a,translate as l}from"./rasterizingUtils.js";import{mapCIMSymbolToGeometryType as m}from"./utils.js";import{densifyForPreview as c}from"../support/densifyForPreview.js";const g=96/72;class y{constructor(e){this._spatialReference=e,this._imageDataCanvas=null,this._cimResourceManager=new r}get _canvas(){return this._imageDataCanvas||(this._imageDataCanvas=document.createElement("canvas")),this._imageDataCanvas}get resourceManager(){return this._cimResourceManager}async rasterizeCIMSymbolAsync(i,r,s,h,l,c,g,y,d,u){if(!i)return null;const{data:p}=i;if(!p||"CIMSymbolReference"!==p.type||!p.symbol)return null;const{symbol:f}=p;c||(c=m(f));const x=await o.resolveSymbolOverrides(p,r,this._spatialReference,l,c,g,y),b=this._cimResourceManager,M=[];n.fetchResources(x,b,M),n.fetchFonts(x,b,M),M.length>0&&await Promise.all(M);const{width:v,height:C}=s;let R=w(c,v,C,h,u);const I=n.getEnvelope(x,R,b);if(!I)return null;I.x===1/0&&(I.x=v+2),I.y===1/0&&(I.y=-C/2),I.width===-1/0&&(I.width=v),I.height===-1/0&&(I.height=C);let S=1,_=0,P=0;switch(f.type){case"CIMPointSymbol":case"CIMTextSymbol":{let e=1;I.width>v&&(e=v/I.width);let t=1;I.height>C&&(t=C/I.height),"preview"===h&&(I.width<v&&(e=v/I.width),I.height<C&&(t=C/I.height)),S=Math.min(e,t),_=I.x+I.width/2,P=I.y+I.height/2}break;case"CIMLineSymbol":if(u){P=I.y+I.height/2,_=I.x+I.width/2;const e=I.width-v,t=I.height-C;R={paths:a(R.paths,{xmin:-1*I.width/2+e,xmax:I.width/2-e,ymin:-1*I.height/2+t,ymax:I.height/2-t,width:I.width-2*e,height:I.height-2*t})}}else{(d||I.height>C)&&(S=C/I.height),P=I.y+I.height/2;const i=I.x*S+v/2,r=(I.x+I.width)*S+v/2,s=e(R)?R.paths:t(R)?R.rings:null;if(null===s)throw new Error("Bad geometry, can't rasterise symbol!");s[0][0][0]-=i/S,s[0][2][0]-=(r-v)/S}break;case"CIMPolygonSymbol":if(u){P=I.y+I.height/2,_=I.x+I.width/2;const e=I.width-v,t=I.height-C;R={paths:a(R.rings,{xmin:-1*I.width/2+e,xmax:I.width/2-e,ymin:-1*I.height/2+t,ymax:I.height/2-t,width:I.width-2*e,height:I.height-2*t})}}else{_=I.x+I.width/2,P=I.y+I.height/2;const e=I.x*S+v/2,t=(I.x+I.width)*S+v/2,i=I.y*S+C/2,r=(I.y+I.height)*S+C/2,{rings:s}=R;e<0&&(s[0][0][0]-=e,s[0][3][0]-=e,s[0][4][0]-=e),i<0&&(s[0][0][1]+=i,s[0][1][1]+=i,s[0][4][1]+=i),t>v&&(s[0][1][0]-=t-v,s[0][2][0]-=t-v),r>C&&(s[0][2][1]+=r-C,s[0][3][1]+=r-C)}}const j={type:"cim",data:{type:"CIMSymbolReference",symbol:x}};return this.rasterize(j,v,C,_,P,S,c,1,R)}rasterize(e,t,i,r,n,o,a,l=0,c=null,y=window.devicePixelRatio||1){const{data:d}=e;if(!d||"CIMSymbolReference"!==d.type||!d.symbol)return null;const{symbol:u}=d,p=this._canvas,f=y*g;p.width=t*f,p.height=i*f,a||(a=m(u)),c||(c=w(a,t,i,"legend")),p.width+=2*l,p.height+=2*l;const x=p.getContext("2d",{willReadFrequently:!0}),b=h.createIdentity();b.translate(-r,-n),b.scale(o*f,-o*f),b.translate(t*f/2+l,i*f/2+l),x.clearRect(0,0,p.width,p.height);return new s(x,this._cimResourceManager,b,!0).drawSymbol(u,c),x.getImageData(0,0,p.width,p.height)}}function d(e,t,r,s){const h=i(e)?c(e,r,s):e;if("esriGeometryPolygon"===t){return{rings:l(a(h.rings,{xmin:0,ymin:0,width:r,height:s}),-1*r/2,-1*s/2)}}if("esriGeometryPolyline"===t){return{paths:l(a(h.paths,{xmin:0,ymin:0,width:r,height:s}),-1*r/2,-1*s/2)}}return null}function w(e,t,i,r,s){const h=1,n=-t/2+h,o=t/2-h,a=i/2-h,l=-i/2+h;if(s&&("esriGeometryPolygon"===e||"esriGeometryPolyline"===e)){const r=d(s,e,t,i);if(r)return r}switch(e){case"esriGeometryPoint":return{x:0,y:0};case"esriGeometryPolyline":return{paths:[[[n,0,-4],[0,0,0],[o,0,2]]],hasM:!0};default:return"legend"===r?{rings:[[[n,a],[o,0],[o,l],[n,l],[n,a]]]}:{rings:[[[n,a],[o,a],[o,l],[n,l],[n,a]]]}}}export{y as CIMSymbolRasterizer};
