/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{isAborted as e}from"../../../core/promiseUtils.js";import t from"../../../geometry/Extent.js";import{toExtent as o}from"../../../geometry/support/aaBoundingRect.js";class r{constructor(){this.modifications=[]}async apply(t,o,r){if(0!==this.modifications.length&&(s??=Promise.all([import("../../../geometry/operators/containsOperator.js"),import("../../../geometry/operators/intersectsOperator.js"),import("../../../geometry/operators/intersectionOperator.js")]),a=await s,!e(r)))for(const e of this.modifications)e.apply(t,o)}}class n{constructor(e,t){this.type=e,this.polygon=t}apply(e,t){if("replace"!==this.type)return;const{polygon:r}=this,[n,s,l]=a,{extent:f,spatialReference:p}=t,m=o(f,p);if(s.accelerateGeometry(r),!s.execute(r,m))return;n.accelerateGeometry(r),l.accelerateGeometry(r);const{width:y,height:h,values:u}=e,g=r.hasZ?r.rings.reduce((e,t)=>t.reduce((e,t)=>Math.min(e,t[2]??1/0),e),1/0):1/0,d=g===1/0?0:g;if(n.execute(r,m))u.fill(d);else{const e=i(t,r,[y,h]);if(!e)return;if(e.rings.every(e=>0===e.length))return;const o=c(t,e,y,h);for(let t=h-1;t>=0;--t)for(let e=0;e<y;++e){const r=(t+1)*(y+2)+(e+1);let n=0;for(let e=-1;e<=1;++e){const t=r+e*(y+2);for(let e=-1;e<=1;++e)n=Math.max(n,o[t+e])}const s=t*y+e;u[s]=0===n?u[s]:d}}}}let s,a;function i(e,o,r){const[n,s,i,c]=e.extent,{spatialReference:l}=e,[f,p]=r,m=2,y=(i-n)/(f-1),h=(c-s)/(p-1),u=new t({xmin:n-m*y,ymin:s-m*h,xmax:i+m*y,ymax:c+m*h,spatialReference:l}),g=a[2].execute(o,u);return"polygon"===g?.type?g:null}function c(e,t,o,r){const n=new OffscreenCanvas(o+2,r+2).getContext("2d",{willReadFrequently:!0});if(!n)throw new Error("failed to create canvas");const[s,a,i,c]=e.extent,f=i-s,p=c-a;n.fillStyle="black",n.clearRect(0,0,o,r);const m=new Path2D;for(const g of t.rings)if(g.length>0){const e=new Path2D;let t=!0;for(const[n,a]of g){const i=(n-s)/f*(o-1)+1+l,m=(c-a)/p*(r-1)+1+l;t?(t=!1,e.moveTo(i,m)):e.lineTo(i,m)}e.closePath(),m.addPath(e)}n.fillStyle="white",n.fill(m,"evenodd");const y=n.getImageData(0,0,o+2,r+2),h=(o+2)*(r+2),u=new Uint8Array(h);for(let l=0;l<h;++l)u[l]=y.data[4*l+0]>0?1:0;return u}const l=.5;export{n as ElevationLayerView3DModification,r as ElevationLayerView3DModifications};
