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