/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{loadArcade as e}from"../../../support/loadArcade.js";function t(e,t,n){return null!=e&&isFinite(e)&&((t??!0)||0!==e)&&((n??!0)||e>=0)}async function n(n){const{features:i,attributes:s,includeZeros:o,includeNegatives:l,view:c}=n;let a=0,r=0,u=1/0,f=-1/0,d=null;const p=new Map;for(let t=0;t<s.length;t++){const{valueExpression:n}=s[t];if(n){if(!d){const{arcadeUtils:t}=await e();d=t}p.set(t,d.createFunction(n))}}const g=c?d?.getViewInfo({viewingMode:"2d"===c.type?"map":c.viewingMode,scale:c.scale,spatialReference:c.spatialReference}):null;for(const e of i){const n=e.geometry,i=e.attributes;if(n){const x=n.extent;if(x){const n=x.width*x.height;if(n>0){let x=0;const m=d?.createExecContext(e,g,c?.timeZone);for(let e=0;e<s.length;e++){const{field:n,valueExpression:c}=s[e];let a=null;if(n)a=i[n];else if(c){const t=p.get(e);a=d.executeFunction(t,m)}t(a,o,l)&&(x+=a||0)}if(t(x,o,l)){const e=x/n;++a,r+=e,e<u&&(u=e),e>f&&(f=e)}}}}}return{minDensity:u!==1/0?u:null,maxDensity:f!==-1/0?f:null,avgDensity:a?r/a:null}}export{n as default};
