/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{substitute as e}from"../../../intl/substitute.js";import{numericTypes as t}from"../../../layers/support/fieldUtils.js";import{TextContent as i}from"../../../popup/content.js";import n from"../../../popup/ExpressionInfo.js";import s from"../../../popup/FieldInfo.js";import r from"../../../popup/support/FieldInfoFormat.js";import{getFieldAndExpressionInfos as o,getContentFromFieldInfos as l}from"./utils.js";import{getStatisticInfos as u,getClusterField as a,getPredominantTypeExpression as f,clusterCountField as p}from"../../support/clusterUtils.js";import{isClusterCompatibleRenderer as m}from"../../../views/2d/layers/support/clusterUtils.js";function d(e){const{fieldName:i,label:n,type:o}=e,l=null!=o&&t.includes(o),u=new s({fieldName:i,label:n,visible:!0,format:l?new r({places:"integer"===o||"small-integer"===o?0:2,digitSeparator:!0}):void 0});return"date"===o&&(u.format=new r({dateFormat:"short-date-short-time"})),u}function c(e,t){const i=e?t.getField(e):null;return null!=i?i.alias||i.name:void 0}function y(t,i,n){const{attributeInfo:s,statisticType:r}=i,{field:o,normalizationField:l}=s;let u,a="";if("avg"===r?u=l?n.clusters.avgNormFieldLabel:n.clusters.avgFieldLabel:"type"===r&&(u=n.clusters.predominantFieldLabel),u){const i=s.valueExpression?s.valueExpressionTitle:c(o,t),n=l&&c(l,t);a=e(u,{fieldLabel:i||"",normFieldLabel:n||""})}return a}function I(t,i,n,s){const{attributeInfo:r,statisticType:o}=i,{field:l,normalizationField:u}=r;let a,f="";if("avg"===o?a=u?s.clusters.avgNormFieldSummary:s.clusters.avgFieldSummary:"type"===o&&(a=s.clusters.predominantFieldSummary),a){const i=r.valueExpression?r.valueExpressionTitle:c(l,t),s=u&&c(u,t);f=e(a,{fieldLabel:i||"",normFieldLabel:s||"",fieldValue:"{"+n+"}"})}return f}function b(t){return{fieldInfo:d({fieldName:p,label:t.clusters.numFeatures,type:"integer"}),fieldSummary:new i({text:e(t.clusters.countSummary,{count:`{${p}}`})})}}async function F(e,t,s){if(!m(t))return null;const r=u(e,t),o=s.clusters.predominantNoneValue,l="unique-value"===t.type?t.uniqueValueInfos??[]:[],{fieldInfo:p,fieldSummary:c}=b(s),F=[c],v=[p],x=[];for(const u of r){const{statisticType:t,attributeInfo:r}=u,p=a(r,t),m=y(e,u,s);if("avg"===t)F.push(new i({text:I(e,u,p,s)})),v.push(d({fieldName:p,label:m,type:"double"}));else if("type"===t){const t=`expression/${p}`;F.push(new i({text:I(e,u,t,s)})),x.push(new n({name:p,title:m,returnType:"string",expression:f(l,p,o)})),v.push(d({fieldName:t}))}}return{fieldInfos:v,expressionInfos:x,content:F}}async function v(e,t,i){if(!m(t))return null;const{fieldInfo:n,fieldSummary:s}=b(i),r=[s],u=[n],a=[],{fieldInfos:f,expressionInfos:p}=await o({renderer:t,layer:e,isFeatureReduction:!0}),d=await l(e,{fieldInfos:f,expressionInfos:p},!0);return r.push(...d),u.push(...f),a.push(...p),{fieldInfos:u,expressionInfos:a,content:r}}export{F as getContentAndInfos,v as getContentAndInfosForPieChart};
