/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import e from"../../../../core/Error.js";import has from"../../../../core/has.js";import r from"../../../../core/Logger.js";import{createMD5Hash as s}from"../../../../core/MD5.js";import i from"../../../../layers/support/AggregateField.js";import n from"../../../../layers/support/ExpressionInfo.js";import t from"../../../../renderers/SimpleRenderer.js";import a from"../../../../renderers/support/AuthoringInfo.js";import o from"../../../../symbols/SimpleMarkerSymbol.js";const l=()=>r.getLogger("esri.views.2d.layers.support.clusterUtils");has.add("esri-cluster-arcade-enabled",!0);const u=has("esri-cluster-arcade-enabled"),p=new Set(["simple-line","simple-fill","picture-fill"]);function d(e,r){let s=r.clone();if(!m(s))return s;if(r.symbols.some(e=>p.has(e.type))&&(s=new t({symbol:new o})),s.authoringInfo||(s.authoringInfo=new a),s.authoringInfo.isAutoGenerated=!0,"visualVariables"in s){const r=(s.visualVariables||[]).filter(e=>"$view.scale"!==e.valueExpression);r.forEach(r=>{"rotation"===r.type?r.field?r.field=v(e,r.field,"avg_angle","number"):r.valueExpression&&(r.field=c(e,r.valueExpression,"avg_angle","number"),r.valueExpression=null):r.normalizationField?(r.field=v(e,r.field,"avg_norm","number",r.normalizationField),r.normalizationField=null):r.field?r.field=v(e,r.field,"avg","number"):r.valueExpression&&(r.field=c(e,r.valueExpression,"avg","number"),r.valueExpression=null)}),s.visualVariables=r}switch(s.type){case"simple":break;case"pie-chart":for(const r of s.attributes)r.field?r.field=v(e,r.field,"sum","number"):r.valueExpression&&(r.field=c(e,r.valueExpression,"sum","number"),r.valueExpression=null);break;case"unique-value":s.field?s.field=v(e,s.field,"mode","string"):s.valueExpression&&(s.field=c(e,s.valueExpression,"mode","string"),s.valueExpression=null);break;case"class-breaks":s.normalizationField?(s.field=v(e,s.field,"avg_norm","number",s.normalizationField),s.normalizationField=null):s.field?s.field=v(e,s.field,"avg","number"):s.valueExpression&&(s.field=c(e,s.valueExpression,"avg","number"),s.valueExpression=null)}return s}const m=r=>{const s=s=>l().error(new e("Unsupported-renderer",s,{renderer:r}));if(!r)return!1;switch(r.type){case"unique-value":if(r.field2||r.field3)return s("FeatureReductionCluster does not support multi-field UniqueValueRenderers"),!1;break;case"class-breaks":if(r.normalizationField){const e=r.normalizationType;if("field"!==e)return s(`FeatureReductionCluster does not support a normalizationType of ${e}`),!1}break;case"simple":case"pie-chart":break;default:return s(`FeatureReductionCluster does not support renderers of type ${r.type}`),!1}if(!u){if("valueExpression"in r&&r.valueExpression)return s("FeatureReductionCluster does not currently support renderer.valueExpression. Support will be added in a future release"),!1;if(("visualVariables"in r&&r.visualVariables||[]).some(e=>!(!("valueExpression"in e)||!e.valueExpression)))return s("FeatureReductionCluster does not currently support visualVariables with a valueExpression. Support will be added in a future release"),!1}return!0};function f(e,r,i){switch(e){case"sum":return`cluster_sum_${r}`;case"avg":case"avg_angle":return`cluster_avg_${r}`;case"mode":return`cluster_type_${r}`;case"avg_norm":{const e=i,n="field",t=r.toLowerCase()+",norm:"+n+","+e.toLowerCase();return"cluster_avg_"+s(t)}}}function c(e,r,t,a){const o=s(r),l="mode"===t?`cluster_type_${o}`:"sum"===t?`cluster_sum_${o}`:`cluster_avg_${o}`;return e.some(e=>e.name===l)||e.push(new i({name:l,isAutoGenerated:!0,onStatisticExpression:new n({expression:r,returnType:a}),statisticType:t})),l}function v(e,r,s,t,a){if("cluster_count"===r||e.some(e=>e.name===r))return r;const o=f(s,r,a);return e.some(e=>e.name===o)||("avg_norm"===s?e.push(new i({name:o,isAutoGenerated:!0,onStatisticExpression:new n({expression:`$feature.${r} / $feature.${a}`,returnType:t}),statisticType:"avg"})):e.push(new i({name:o,isAutoGenerated:!0,onStatisticField:r,statisticType:s}))),o}export{d as createInferredClusterRenderer,m as isClusterCompatibleRenderer};
