/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../../core/Error","../../../../core/has","../../../../core/Logger","../../../../core/maybe","../../../../core/MD5","../../../../renderers/visualVariables/SizeVariable","../../../../renderers/visualVariables/support/SizeStop","../../engine/LevelDependentSizeVariable"],(function(e,i,t,r,s,a,n,l,o){"use strict";const u=r.getLogger("esri.views.2d.layers.support.clusterUtils");t.add("esri-cluster-arcade-enabled",!0);const c=t("esri-cluster-arcade-enabled"),d=(e,i,t,r)=>{const a=i.clone();if(!m(a))return a;if(t.fields)for(const s of t.fields)b(e,s);if("visualVariables"in a){const i=(a.visualVariables||[]).filter((e=>"$view.scale"!==e.valueExpression)),n=p(i);i.forEach((i=>{"rotation"===i.type?i.field?i.field=x(e,i.field,"avg_angle"):i.valueExpression&&(i.field=g(e,i.valueExpression,"avg_angle"),i.valueExpression=null):i.normalizationField?(i.field=x(e,i.field,"norm",i.normalizationField),i.normalizationField=null):i.field?i.field=x(e,i.field,"avg"):(i.field=g(e,i.valueExpression,"avg"),i.valueExpression=null)})),s.isNone(n)&&!f(i)&&(i.push(v(t,r)),a.dynamicClusterSize=!0),a.visualVariables=i}switch(a.type){case"simple":break;case"unique-value":a.field?a.field=x(e,a.field,"mode"):a.valueExpression&&(a.field=g(e,a.valueExpression,"mode"),a.valueExpression=null);break;case"class-breaks":a.normalizationField?(a.field=x(e,a.field,"norm",a.normalizationField),a.normalizationField=null):a.field?a.field=x(e,a.field,"avg"):(a.field=g(e,a.valueExpression,"avg"),a.valueExpression=null)}return a},p=e=>{for(const i of e)if("size"===i.type)return i;return null},f=e=>{for(const i of e)if("cluster_count"===i.field)return!0;return!1},v=(e,i)=>{const t=[new l({value:0,size:0}),new l({value:1})];if(s.isNone(i))return new n({field:"cluster_count",stops:[...t,new l({value:2,size:0})]});const r=Object.keys(i).reduce(((r,s)=>({...r,[s]:[...t,new l({value:Math.max(2,i[s].minValue),size:e.clusterMinSize}),new l({value:Math.max(3,i[s].maxValue),size:e.clusterMaxSize})]})),{});return new o.LevelDependentSizeVariable({field:"cluster_count",levels:r})},m=e=>{const t=t=>u.error(new i("Unsupported-renderer",t,{renderer:e}));if("unique-value"===e.type){if(e.field2||e.field3)return t("FeatureReductionCluster does not support multi-field UniqueValueRenderers"),!1}else if("class-breaks"===e.type){if(e.normalizationField){const i=e.normalizationType;if("field"!==i)return t(`FeatureReductionCluster does not support a normalizationType of ${i}`),!1}}else if("simple"!==e.type)return t(`FeatureReductionCluster does not support renderers of type ${e.type}`),!1;if(!c){if("valueExpression"in e&&e.valueExpression)return t("FeatureReductionCluster does not currently support renderer.valueExpression. Support will be added in a future release"),!1;if(("visualVariables"in e&&e.visualVariables||[]).some((e=>!(!("valueExpression"in e)||!e.valueExpression))))return t("FeatureReductionCluster does not currently support visualVariables with a valueExpression. Support will be added in a future release"),!1}return!0};function S(e,i,t){switch(e){case"avg":case"avg_angle":return`cluster_avg_${i}`;case"mode":return`cluster_type_${i}`;case"norm":{const e=t,r="field",s=i.toLowerCase()+",norm:"+r+","+e.toLowerCase();return"cluster_avg_"+a.createMD5Hash(s)}}}function b(e,t){const{name:r,outStatistic:s}=t,{onStatisticField:n,onStatisticValueExpression:l,statisticType:o}=s;if(l){const i=a.createMD5Hash(l.toLowerCase());e.push({name:r,outStatistic:{onStatisticField:i,onStatisticValueExpression:l,statisticType:o}})}else n?e.push({name:r,outStatistic:{onStatisticField:n,statisticType:o}}):u.error(new i("mapview-unsupported-field","Unable to handle field",{field:t}))}function g(e,i,t){const r=a.createMD5Hash(i),s="mode"===t?`cluster_type_${r}`:`cluster_avg_${r}`;return e.some((e=>e.name===s))||e.push({name:s,outStatistic:{onStatisticField:r,onStatisticValueExpression:i,statisticType:t}}),s}function x(e,i,t,r){if("cluster_count"===i||e.some((e=>e.name===i)))return i;const s=S(t,i,r);return e.some((e=>e.name===s))||("norm"===t?e.push({name:s,outStatistic:{onStatisticField:i,onStatisticNormalizationField:r,statisticType:t}}):e.push({name:s,outStatistic:{onStatisticField:i,statisticType:t}})),s}e.convertAggregateFieldFromPublic=b,e.createClusterCountSizeVariable=v,e.createClusterRenderer=d,e.findSizeVV=p,e.hasClusterCountVV=f,e.isClusterCompatibleRenderer=m,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
