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