/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../chunks/_rollupPluginBabelHelpers","../../../geometry","../../../core/Error","../../../core/maybe","../../../core/screenUtils","../../../geometry/SpatialReference","../../../geometry/support/quantizationUtils","../../../geometry/support/spatialReferenceUtils","../../../layers/support/fieldUtils","../../../renderers/support/heatmapUtils","../../../statistics/utils","../../../support/arcadeOnDemand","../../../geometry/Point"],(function(e,t,n,r,i,l,o,s,a,u,c,f,d,p){"use strict";let m=null;function h(e,t,n){return e.x<0?e.x+=t:e.x>n&&(e.x-=t),e}function y(e,t,n,r){const l=a.isWrappable(n)?a.getInfo(n):null,o=l?Math.round((l.valid[1]-l.valid[0])/t.scale[0]):null;return e.map((e=>{const n=new p(i.unwrap(e.geometry));return s.quantizePoint(t,n,n,n.hasZ,n.hasM),e.geometry=l?h(n,o,r[0]):n,e}))}function g(e,t=18,n,r,i,o){const s=new Float64Array(i*o);t=Math.round(l.pt2px(t));let a=Number.POSITIVE_INFINITY,u=Number.NEGATIVE_INFINITY,f=0,d=0,p=0,m=0;const h=c.createValueFunction(r,n);for(const{geometry:l,attributes:g}of e){const{x:e,y:n}=l,r=Math.max(0,e-t),y=Math.max(0,n-t),I=Math.min(o,n+t),F=Math.min(i,e+t),$=+h(g);for(let l=y;l<I;l++)for(let o=r;o<F;o++){const r=l*i+o,h=c.evaluateDensityKernel(o-e,l-n,t),y=s[r];f=s[r]+=h*$;const g=f-y;d+=g,p+=g*g,f<a&&(a=f),f>u&&(u=f),m++}}if(!m)return{mean:0,stddev:0,min:0,max:0,mid:0,count:0};const y=(u-a)/2;return{mean:d/m,stdDev:Math.sqrt((p-d*d/m)/m),min:a,max:u,mid:y,count:m}}function I(e,t){return F.apply(this,arguments)}function F(){return(F=t._asyncToGenerator((function*(e,t){if(!t)return[];const n=e.field,r=e.valueExpression,i=e.normalizationType,l=e.normalizationField,s=e.normalizationTotal,a=[],u=e.viewInfoParams;let c=null,p=null;if(r){if(!m){const{arcadeUtils:e}=yield d.loadArcade();m=e}c=m.createFunction(r),p=u&&m.getViewInfo({viewingMode:u.viewingMode,scale:u.scale,spatialReference:new o(u.spatialReference)})}const h=e.fieldInfos,y=!(t[0]&&"declaredClass"in t[0]&&"esri.Graphic"===t[0].declaredClass)&&h?{fields:h}:null;return t.forEach((e=>{const t=e.attributes;let o;if(r){const t=y?{...e,layer:y}:e,n=m.createExecContext(t,p);o=m.executeFunction(c,n)}else t&&(o=t[n]);if(i&&isFinite(o)){const e=t&&parseFloat(t[l]);o=f.getNormalizedValue(o,i,e,s)}a.push(o)})),a}))).apply(this,arguments)}function $(e){const t=e.field,n=e.normalizationType,r=e.normalizationField;let i;return"field"===n?i="(NOT "+r+" = 0)":"log"!==n&&"natural-log"!==n&&"square-root"!==n||(i=`(${t} > 0)`),i}function x(e,t){return new r(e,t)}function v(e,t,n){const r=null!=t?e+" >= "+t:"",i=null!=n?e+" <= "+n:"";let l="";return l=r&&i?N(r,i):r||i,l?"("+l+")":""}function b(e,t,n,r){let i=null;return t?t.name!==e.objectIdField&&r.includes(t.type)||(i=x(n,"'field' should be one of these types: "+r.join(","))):i=x(n,"'field' is not defined in the layer schema"),i}function E(e,t,n){let r;return t?t.name!==e.objectIdField&&u.isNumericField(t)||(r=x(n,"'field' should be one of these numeric types: "+u.numericTypes.join(","))):r=x(n,"'field' is not defined in the layer schema"),r}function N(e,t){let n=i.isSome(e)?e:"";return i.isSome(t)&&t&&(n=n?"("+n+") AND ("+t+")":t),n}function j(e,t,n){const r=M({layer:e,fields:t});if(r.length)return x(n,"Unknown fields: "+r.join(", ")+". You can only use fields defined in the layer schema");const i=T({layer:e,fields:t});return i.length?x(n,"Unsupported fields: "+i.join(", ")+". You can only use fields that can be fetched i.e. AdapterFieldUsageInfo.supportsStatistics must be true"):void 0}function M(e){const t=e.layer;return e.fields.filter((e=>!t.getField(e)))}function T(e){const t=e.layer;return e.fields.filter((e=>{const n=t.getFieldUsageInfo(e);return!n||!n.supportsStatistics}))}function S(e,t,n){const r=[],i=[],l=[],o=[],s=[];e.forEach(((e,t)=>{const a=e.field?"field":"expression",u=e.field||e.valueExpression;e.field?(s.push(u),i.push(`var ${a}${t} = Number($feature["${u}"]);`)):(r.push(`function getValueForExpr${t}() {\n  ${u} \n}`),i.push(`var ${a}${t} = Number(getValueForExpr${t}());`)),n||l.push(`${a}${t} = IIf(${a}${t} < 0, 0, ${a}${t});`),o.push(`${a}${t}`)}));let a="return sum;";const u=r.length?null:s.reduce(((e,t)=>`${e} + ${t}`));let c=null;t||n?t?n||(a="return IIf(sum >= 0, sum, null);",u&&(c=`(( ${u} ) >= 0)`)):(a="return IIf(sum != 0, sum, null);",u&&(c=`(( ${u} ) <> 0)`)):(a="return IIf(sum > 0, sum, null);",u&&(c=`(( ${u} ) > 0)`));return{valueExpression:[r.length?r.join("\n"):"",i.join("\n"),l.join("\n"),`var sum = ${o.join(" + ")};`,a].filter(Boolean).join("\n\n"),sqlExpression:u,sqlWhere:c}}e.calculateHeatmapStats=g,e.createError=x,e.getDataValues=I,e.getRangeExpr=v,e.getSQLFilterForNormalization=$,e.getSumOfAttributesExpr=S,e.mergeWhereClauses=N,e.quantizeFeatures=y,e.verifyBasicFieldValidity=j,e.verifyFieldType=b,e.verifyNumericField=E,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
