/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import e from"../request.js";import{fromJSON as t}from"../renderers/support/jsonUtils.js";import{parseUrl as n}from"./utils.js";import{executeQuery as i}from"./query/executeQuery.js";import s from"./support/Query.js";import a from"./support/StatisticDefinition.js";async function l(t,i,s,a){const l=n(t),{source:o,checkValueRange:u}=i,{classificationDefinition:c}=s,f={...s.toJSON(),f:"json"};let m=null;if(m="class-breaks-definition"===c?.type?c.classificationField:c?.attributeField,o){const e={source:o?.toJSON()};f.layer=JSON.stringify(e)}f.classificationDef&&(f.classificationDef=JSON.stringify(f.classificationDef));let p={query:f};a&&(p={...a,...p});const y={url:l.path,field:m,checkValueRange:u},V=l.path+"/generateRenderer";return e(V,p).then(e=>r(y,e))}function r(e,t){const{field:n,checkValueRange:l,url:r}=e,u=t?.data;if(!u)return;if(!l){const e=o(u);return Promise.resolve(e)}const c=new a({statisticType:"min",onStatisticField:n}),f=new a({statisticType:"max",onStatisticField:n}),m=new s({outStatistics:[c,f]});return i(r,m).then(e=>{const t=e.features[0].attributes;let n=null,i=null;for(const s in t)s.toLowerCase().startsWith("min")?n=t[s]:i=t[s];return o(u,n,i)})}function o(e,n,i){if("classBreaks"===e.type){const s=t(e);return{classBreaks:s.classBreakInfos.map((e,t)=>(0===t&&null!=n&&(e.minValue=n),t===s.classBreakInfos.length-1&&null!=i&&(e.maxValue=i),{minValue:e.minValue,maxValue:e.maxValue,label:e.label})),normalizationTotal:s.normalizationTotal}}const{uniqueValueInfos:s}=e;return{uniqueValues:s?.map((e,t)=>(0===t&&null!=n&&(e.value=n),t===s.length-1&&null!=i&&(e.value=i),{count:e.count,value:e.value,label:e.label}))??[]}}export{l as generateRenderer};
