/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import e from"../../core/Error.js";import{fetchMessageBundle as t}from"../../intl/messages.js";import r from"../heuristics/clusterMinSize.js";import{getAggregateFieldInfo as s,createLabelExpressionForAggregateField as n,createLabelClass as i,createNumericLabelExpression as a}from"./support/utils.js";import{getStatisticInfos as o,getClusterField as l,clusterCountField as c,getClusterFieldHash as u,getPredominantTypeExpression as m}from"../support/clusterUtils.js";import{isClusterCompatibleRenderer as f}from"../../views/2d/layers/support/clusterUtils.js";async function p(t){const{layer:r,renderer:s,view:n}=t;await Promise.all([r.load(),n.when()]);const i=s||r.renderer;if(!f(i))throw new e("clusters-label-schemes:invalid-parameters","'renderer' is not valid");return{layer:r,renderer:i,view:n}}function d(e,t,r){const s="unique-value"===e.type?e.uniqueValueInfos??[]:[];return m(s,t,r)}async function w(e){const{renderer:t,view:s,statInfo:n}=e,o=n?.statisticType,m=n?l(n.attributeInfo,o):c,f="type"===o?d(t,m,e.noneValueLabel):a(m),p=i(f);return{name:n?`clusters-${o}-${u(m,o)}`:"clusters-count",labelingInfo:[p],clusterMinSize:await r(p.symbol,s),fieldName:m}}function y(e){const t=new Map;for(const r of e)"size"===r.attributeInfo.vvType?t.set(r.statisticHash,r):t.has(r.statisticHash)||t.set(r.statisticHash,r);return[...t.values()]}async function v(a){const[{renderer:l,layer:c,view:u},m]=await Promise.all([p(a),t("esri/smartMapping/t9n/smartMapping")]),f=m.clusters.predominantNoneValue;if(!l)return null;let d=null;const v=[];if(a.field){const t=s(a.field,c);if(!t)throw new e("clusters-label-schemes:invalid-parameters","'field' is not a valid aggregate field");const{name:o}=t,m=n(c,l,t,f),p=i(m);return d={name:`clusters-${o}`,labelingInfo:[p],clusterMinSize:await r(p.symbol,u),fieldName:o},{primaryScheme:d,secondarySchemes:v}}const h=y(o(c,l,!1));for(const e of h){const t=await w({renderer:l,view:u,statInfo:e,noneValueLabel:f});"size"===e.attributeInfo.vvType?d=t:v.push(t)}const b=await w({renderer:l,view:u});return d?v.unshift(b):d=b,{primaryScheme:d,secondarySchemes:v}}export{v as getLabelSchemes};
