/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{pt2px as e,px2pt as t}from"../../core/screenUtils.js";import{CIMSymbolHelper as i}from"../cim/CIMSymbolHelper.js";import{CIMSymbolRasterizer as o}from"../cim/CIMSymbolRasterizer.js";import{OverrideHelper as l}from"../cim/OverrideHelper.js";import{mapCIMSymbolToGeometryType as r}from"../cim/utils.js";import{renderSymbol as n}from"./renderUtils.js";const a=new o(null),s=t(22),m=t(120),h=t(50),c=1;async function y(e,t,i){const o=t?.size;let l=null!=o&&"object"==typeof o&&"width"in o?o.width:o,r=null!=o&&"object"==typeof o&&"height"in o?o.height:o;if(!l||!r)if("esriGeometryPolygon"===i)l=r=t.maxSize?Math.min(t.maxSize,s):s;else{const o=await g(e,t,i);o&&(l=o.width,r=o.height),"esriGeometryPolyline"===i&&(l=t.maxSize?Math.min(t.maxSize,h):h),l=null!=l&&isFinite(l)?Math.min(l,m):s,r=null!=r&&isFinite(r)?Math.max(Math.min(r,m),c):s}return"legend"===t.style&&"esriGeometryPolyline"===i&&(l=h),{width:l,height:r}}async function g(e,t={},o){const n=t.cimOptions||t;o??=n.geometryType||r(e?.data?.symbol);const{feature:s,fieldMap:m,viewParams:h}=n,c=await l.resolveSymbolOverrides(e.data,s,null,m,o,null,h);if(!c)return null;(e=e.clone()).data={type:"CIMSymbolReference",symbol:c},e.data.primitiveOverrides=void 0;const y=[];return i.fetchResources(c,a.resourceManager,y),i.fetchFonts(c,a.resourceManager,y),y.length>0&&await Promise.all(y),i.getEnvelope(c,null,a.resourceManager)}async function u(i,o={}){const{node:l,opacity:s,symbolConfig:m}=o,h=null!=m&&"object"==typeof m&&"isSquareFill"in m&&m.isSquareFill,c=o.cimOptions||o,g=c.geometryType||r(i?.data?.symbol),u=await y(i,o,g),{feature:d,fieldMap:f}=c,p=o?.geometry||h||"esriGeometryPolygon"!==g?"preview":"legend";let w=u;const b=u;if(o?.geometry&&("esriGeometryPolygon"===g||"esriGeometryPolyline"===g)){const i=200;if(e(u.width)<i||e(u.height)<i){const e=u.width>u.height?t(i)*u.height/u.width:t(i);w={width:u.width>u.height?t(i):t(i)*u.width/u.height,height:e}}}const M=await a.rasterizeCIMSymbolAsync(i,d,w,p,f,g,null,c.viewParams,c.allowScalingUp,o?.geometry?.toJSON());if(!M)return null;const{width:S,height:v}=M,j=document.createElement("canvas");j.width=S,j.height=v;j.getContext("2d").putImageData(M,0,0);const P=e(b.width),x=e(b.height),z=new Image(P,x);z.src=j.toDataURL(),z.ariaLabel=o.ariaLabel??null,z.alt=o.ariaLabel??"",null!=s&&(z.style.opacity=`${s}`);let C=z;if(o.cssEffectFilter){const e={shape:{type:"image",x:0,y:0,width:P,height:x,src:z.src},fill:null,stroke:null,offset:[0,0]};C=n([[e]],[P,x],o)}return l&&C&&l.appendChild(C),C}export{g as getCIMSymbolPreviewSize,u as previewCIMSymbol};
