/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["../../../../core/floatRGBA","../../../../core/promiseUtils"],(function(t,e){"use strict";const i=1e20;return function(){function s(t){this.size=t;const e=document.createElement("canvas");e.width=e.height=t,this._context=e.getContext("2d"),this._gridOuter=new Float64Array(t*t),this._gridInner=new Float64Array(t*t),this._f=new Float64Array(t),this._d=new Float64Array(t),this._z=new Float64Array(t+1),this._v=new Int16Array(t)}var r=s.prototype;return r.dispose=function(){this._context=this._gridOuter=this._gridInner=this._f=this._d=this._z=this._v=null,this._svg&&(document.body.removeChild(this._svg),this._svg=null)},r.draw=function(s,r,n=31){this._initSVG();const h=this.createSVGString(s);return new Promise(((s,o)=>{const a=new Image;a.src="data:image/svg+xml; charset=utf8, "+encodeURIComponent(h),a.onload=()=>{a.onload=null,this._context.clearRect(0,0,this.size,this.size),this._context.drawImage(a,0,0,this.size,this.size);const e=this._context.getImageData(0,0,this.size,this.size),r=new Uint8Array(this.size*this.size*4);for(let t=0;t<this.size*this.size;t++){const s=e.data[4*t+3]/255;this._gridOuter[t]=1===s?0:0===s?i:Math.max(0,.5-s)**2,this._gridInner[t]=1===s?i:0===s?0:Math.max(0,s-.5)**2}this._edt(this._gridOuter,this.size,this.size),this._edt(this._gridInner,this.size,this.size);for(let i=0;i<this.size*this.size;i++){const e=.5-(this._gridOuter[i]-this._gridInner[i])/(2*n);t.packFloatRGBA(e,r,4*i)}s(r)};const d=r&&r.signal;d&&e.onAbort(d,(()=>o(e.createAbortError())))}))},r._initSVG=function(){if(!this._svg){const t=document.createElementNS("http://www.w3.org/2000/svg","svg");t.setAttribute("style","position: absolute;"),t.setAttribute("width","0"),t.setAttribute("height","0"),t.setAttribute("aria-hidden","true"),t.setAttribute("role","presentation"),document.body.appendChild(t),this._svg=t}},r.createSVGString=function(t){this._initSVG();const e=document.createElementNS("http://www.w3.org/2000/svg","path");e.setAttribute("d",t),this._svg.appendChild(e);const i=e.getBBox(),s=i.width/i.height,r=this.size/2;let n,h,o,a;if(s>1){h=n=r/i.width;const t=r*(1/s);o=this.size/4,a=r-t/2}else{n=h=r/i.height;o=r-r*s/2,a=this.size/4}const d=-i.x*n+o,c=-i.y*h+a;e.setAttribute("style",`transform: matrix(${n}, 0, 0, ${h}, ${d}, ${c})`);const l=`<svg style="fill:red;" height="${this.size}" width="${this.size}" xmlns="http://www.w3.org/2000/svg">${this._svg.innerHTML}</svg>`;return this._svg.removeChild(e),l},r._edt=function(t,e,i){const s=this._f,r=this._d,n=this._v,h=this._z;for(let o=0;o<e;o++){for(let r=0;r<i;r++)s[r]=t[r*e+o];this._edt1d(s,r,n,h,i);for(let s=0;s<i;s++)t[s*e+o]=r[s]}for(let o=0;o<i;o++){for(let i=0;i<e;i++)s[i]=t[o*e+i];this._edt1d(s,r,n,h,e);for(let i=0;i<e;i++)t[o*e+i]=Math.sqrt(r[i])}},r._edt1d=function(t,e,s,r,n){s[0]=0,r[0]=-i,r[1]=+i;for(let h=1,o=0;h<n;h++){let e=(t[h]+h*h-(t[s[o]]+s[o]*s[o]))/(2*h-2*s[o]);for(;e<=r[o];)o--,e=(t[h]+h*h-(t[s[o]]+s[o]*s[o]))/(2*h-2*s[o]);o++,s[o]=h,r[o]=e,r[o+1]=+i}for(let i=0,h=0;i<n;i++){for(;r[h+1]<i;)h++;e[i]=(i-s[h])*(i-s[h])+t[s[h]]}},s}()}));
