/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{packFloatRGBA as t}from"../../../../core/floatRGBA.js";import{onAbort as e,createAbortError as s}from"../../../../core/promiseUtils.js";import{destroyHiddenSvg as i,createHiddenSvg as r,createSvgElement as n}from"../svgUtils.js";const o=1e20;class a{constructor(t,e=2){this._textureSize=t,this._rasterizationScale=e,this._canvasSize=this._textureSize*this._rasterizationScale,this._svg=null;const{_canvasSize:s}=this,i=document.createElement("canvas");i.width=i.height=s,this._context=i.getContext("2d",{willReadFrequently:!1}),this._gridOuter=new Float64Array(s*s),this._gridInner=new Float64Array(s*s),this._f=new Float64Array(s),this._d=new Float64Array(s),this._z=new Float64Array(s+1),this._v=new Int16Array(s)}dispose(){this._context=this._gridOuter=this._gridInner=this._f=this._d=this._z=this._v=null,this._svg=i(this._svg)}draw(i,r,n){const{_canvasSize:a,_textureSize:h,_rasterizationScale:_}=this,l=h/4;this._initSVG();const c=this.createSVGString(i,r);return new Promise((i,r)=>{const d=new Image;d.src="data:image/svg+xml; charset=utf8, "+encodeURIComponent(c),d.onload=()=>{d.onload=null,this._context.clearRect(0,0,a,a),this._context.drawImage(d,0,0,a,a);const e=this._context.getImageData(0,0,a,a),s=new Uint8Array(h*h*4);for(let t=0;t<a*a;t++){const s=e.data[4*t+3]/255;this._gridOuter[t]=1===s?0:0===s?o:Math.max(0,.5-s)**2,this._gridInner[t]=1===s?o:0===s?0:Math.max(0,s-.5)**2}this._edt(this._gridOuter,a,a),this._edt(this._gridInner,a,a);for(let i=0;i<h*h;i++){let e=0;for(let t=0;t<_;t++){const s=Math.floor(i/h)*_+t;for(let t=0;t<_;t++){const r=s*a+(i%h*_+t);e+=this._gridOuter[r]-this._gridInner[r]}}e/=_*_,e/=_;t(.5-e/(2*l),s,4*i)}i(s)};const g=n?.signal;g&&e(g,()=>r(s()))})}_initSVG(){return this._svg||(this._svg=r()),this._svg}createSVGString(t,e){const s=this._initSVG(),i=n("path");i.setAttribute("d",t),s.appendChild(i);const r=i.getBBox(),o=r.width/r.height,a=this._canvasSize/2;let h,_,l;if(o>1){h=a/r.width;const t=a*(1/o);_=this._canvasSize/4,l=a-t/2}else{h=a/r.height;_=a-a*o/2,l=this._canvasSize/4}const c=-r.x*h+_,d=-r.y*h+l;i.setAttribute("style",`transform: matrix(${h}, 0, 0, ${h}, ${c}, ${d})`),i.setAttribute("stroke-width",""+.5/h);const g=`<svg style="fill:${e?"red":"none"}; stroke:${e?"none":"red"}" height="${this._canvasSize}" width="${this._canvasSize}" xmlns="http://www.w3.org/2000/svg">${s.innerHTML}</svg>`;return s.removeChild(i),g}_edt(t,e,s){const i=this._f,r=this._d,n=this._v,o=this._z;for(let a=0;a<e;a++){for(let r=0;r<s;r++)i[r]=t[r*e+a];this._edt1d(i,r,n,o,s);for(let i=0;i<s;i++)t[i*e+a]=r[i]}for(let a=0;a<s;a++){for(let s=0;s<e;s++)i[s]=t[a*e+s];this._edt1d(i,r,n,o,e);for(let s=0;s<e;s++)t[a*e+s]=Math.sqrt(r[s])}}_edt1d(t,e,s,i,r){s[0]=0,i[0]=-o,i[1]=+o;for(let n=1,a=0;n<r;n++){let e=(t[n]+n*n-(t[s[a]]+s[a]*s[a]))/(2*n-2*s[a]);for(;e<=i[a];)a--,e=(t[n]+n*n-(t[s[a]]+s[a]*s[a]))/(2*n-2*s[a]);a++,s[a]=n,i[a]=e,i[a+1]=+o}for(let n=0,o=0;n<r;n++){for(;i[o+1]<n;)o++;e[n]=(n-s[o])*(n-s[o])+t[s[o]]}}}export{a as default};
