/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/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;await n.normalizeSymbolAndFetchResources(x,b);const{width:M,height:v}=s;let C=w(c,M,v,h,u);const R=n.getEnvelope(x,C,b);if(!R)return null;R.x===1/0&&(R.x=M+2),R.y===1/0&&(R.y=-v/2),R.width===-1/0&&(R.width=M),R.height===-1/0&&(R.height=v);let I=1,S=0,_=0;switch(f.type){case"CIMPointSymbol":case"CIMTextSymbol":{let e=1;R.width>M&&(e=M/R.width);let t=1;R.height>v&&(t=v/R.height),"preview"===h&&(R.width<M&&(e=M/R.width),R.height<v&&(t=v/R.height)),I=Math.min(e,t),S=R.x+R.width/2,_=R.y+R.height/2}break;case"CIMLineSymbol":if(u){_=R.y+R.height/2,S=R.x+R.width/2;const e=R.width-M,t=R.height-v;C={paths:a(C.paths,{xmin:-1*R.width/2+e,xmax:R.width/2-e,ymin:-1*R.height/2+t,ymax:R.height/2-t,width:R.width-2*e,height:R.height-2*t})}}else{(d||R.height>v)&&(I=v/R.height),_=R.y+R.height/2;const i=R.x*I+M/2,r=(R.x+R.width)*I+M/2,s=e(C)?C.paths:t(C)?C.rings:null;if(null===s)throw new Error("Bad geometry, can't rasterise symbol!");s[0][0][0]-=i/I,s[0][2][0]-=(r-M)/I}break;case"CIMPolygonSymbol":if(u){_=R.y+R.height/2,S=R.x+R.width/2;const e=R.width-M,t=R.height-v;C={paths:a(C.rings,{xmin:-1*R.width/2+e,xmax:R.width/2-e,ymin:-1*R.height/2+t,ymax:R.height/2-t,width:R.width-2*e,height:R.height-2*t})}}else{S=R.x+R.width/2,_=R.y+R.height/2;const e=R.x*I+M/2,t=(R.x+R.width)*I+M/2,i=R.y*I+v/2,r=(R.y+R.height)*I+v/2,{rings:s}=C;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>M&&(s[0][1][0]-=t-M,s[0][2][0]-=t-M),r>v&&(s[0][2][1]+=r-v,s[0][3][1]+=r-v)}}const j={type:"cim",data:{type:"CIMSymbolReference",symbol:x}};return this.rasterize(j,M,v,S,_,I,c,1,C)}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};
