/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{id as t}from"../../kernel.js";import e from"../../request.js";import{toHSV as i,toRGB as r}from"../../core/colorUtils.js";import n from"../../core/Error.js";import has from"../../core/has.js";import"../../libs/maquette/projection.js";import{createProjector as o}from"../../libs/maquette/projector.js";import{renderSVG as a}from"./svgUtils.js";const c=o();function s(t,e){c.append(t,e),c.detach(e)}function l(t,e,i){const r=Math.ceil(e[0]),n=Math.ceil(e[1]);if(!t.some(t=>!!t.length))return null;const o=i?.node||document.createElement("div");null!=i.opacity&&(o.style.opacity=i.opacity.toString()),null!=i.cssEffectFilter&&(o.style.filter=i.cssEffectFilter);return s(o,()=>a(t,r,n,i)),o}function h(t,e){t=Math.ceil(t),e=Math.ceil(e);const i=document.createElement("canvas");i.width=t,i.height=e,i.style.width=t+"px",i.style.height=e+"px";const r=i.getContext("2d");return r.clearRect(0,0,t,e),r}function m(t,i,r){return t?e(t,{responseType:"image"}).then(t=>{const e=t.data,n=e.width,o=e.height,a=n/o;let c=i;if(r){const t=Math.max(n,o);c=Math.min(c,t)}return{image:e,width:a<=1?Math.ceil(c*a):c,height:a<=1?c:Math.ceil(c/a)}}):Promise.reject(new n("renderUtils: imageDataSize","href not provided."))}function g(t,e){return!(!t||"ignore"===e)&&("multiply"!==e||255!==t.r||255!==t.g||255!==t.b||1!==t.a)}function u(t,e,n,o,a){switch(a){case"multiply":t[e]*=n[0],t[e+1]*=n[1],t[e+2]*=n[2],t[e+3]*=n[3];break;default:{const a=i({r:t[e],g:t[e+1],b:t[e+2]});a.h=o.h,a.s=o.s,a.v=a.v/100*o.v;const c=r(a);t[e]=c.r,t[e+1]=c.g,t[e+2]=c.b,t[e+3]*=n[3];break}}}function d(e,r,n,o,a){return m(e,r,a).then(a=>{const c=a.width??r,s=a.height??r;if(a.image&&g(n,o)){let t=a.image.width,r=a.image.height;has("edge")&&/\.svg$/i.test(e)&&(t-=1,r-=1);const l=h(c,s);l.drawImage(a.image,0,0,t,r,0,0,c,s);const m=l.getImageData(0,0,c,s),g=[n.r/255,n.g/255,n.b/255,n.a],d=i(n);for(let e=0;e<m.data.length;e+=4)u(m.data,e,g,d,o);l.putImageData(m,0,0),e=l.canvas.toDataURL("image/png")}else{const i=t?.findCredential(e);if(i?.token){const t=e.includes("?")?"&":"?";e=`${e}${t}token=${i.token}`}}return{url:e,width:c,height:s}}).catch(()=>({url:e,width:r,height:r}))}export{s as renderOnce,l as renderSymbol,d as tintImageWithColor};
