/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import e from"../../core/Logger.js";const t=()=>e.getLogger("esri.rest.support.generateRendererUtils");function l(e,t){return Number(e.toFixed(t))}function n(e){const t=o(e),l=[],n=t.uniqueValues.length;for(let a=0;a<n;a++){const e=t.uniqueValues[a],n=t.valueFrequency[a],u=e.toString();l.push({value:e,count:n,label:u})}return{uniqueValues:l}}function a(e,t){const{normalizationTotal:l}=e;return{classBreaks:u(e,t),normalizationTotal:l}}function u(e,t){const n=e.definition,{classificationMethod:a,normalizationType:u,definedInterval:i}=n,c=n.breakCount??1,b=[];let V=e.values;if(0===V.length)return[];V=V.sort((e,t)=>e-t);const[p,g]=t??[V.at(0),V.at(-1)];if("equal-interval"===a)if(V.length>=c){const e=(g-p)/c;let t=p;for(let n=1;n<c;n++){const a=l(p+n*e,6);b.push({minValue:t,maxValue:a,label:s(t,a,u)}),t=a}b.push({minValue:t,maxValue:g,label:s(t,g,u)})}else V.forEach(e=>{b.push({minValue:e,maxValue:e,label:s(e,e,u)})});else if("natural-breaks"===a){const t=o(V),n=e.valueFrequency||t.valueFrequency,a=r(t.uniqueValues,n,c);let i=p;for(let e=1;e<c;e++)if(t.uniqueValues.length>e){const n=l(t.uniqueValues[a[e]],6);b.push({minValue:i,maxValue:n,label:s(i,n,u)}),i=n}b.push({minValue:i,maxValue:g,label:s(i,g,u)})}else if("quantile"===a)if(V.length>=c&&p!==g){let e=p,t=Math.ceil(V.length/c),l=0;for(let n=1;n<c;n++){let a=t+l-1;a>V.length&&(a=V.length-1),a<0&&(a=0),b.push({minValue:e,maxValue:V[a],label:s(e,V[a],u)}),e=V[a],l+=t,t=Math.ceil((V.length-l)/(c-n))}b.push({minValue:e,maxValue:g,label:s(e,g,u)})}else{let e=-1;for(let t=0;t<V.length;t++){const l=V[t];l!==e&&(e=l,b.push({minValue:e,maxValue:l,label:s(e,l,u)}),e=l)}}else if("standard-deviation"===a){const e=h(V),t=m(V,e);if(0===t)b.push({minValue:V[0],maxValue:V[0],label:s(V[0],V[0],u)});else{const n=f(p,g,c,e,t)*t;let a=0,o=p;for(let t=c;t>=1;t--){const r=l(e-(t-.5)*n,6);b.push({minValue:o,maxValue:r,label:s(o,r,u)}),o=r,a++}let r=l(e+.5*n,6);b.push({minValue:o,maxValue:r,label:s(o,r,u)}),o=r,a++;for(let t=1;t<=c;t++)r=a===2*c?g:l(e+(t+.5)*n,6),b.push({minValue:o,maxValue:r,label:s(o,r,u)}),o=r,a++}}else if("defined-interval"===a){if(!i)return b;const[e,n]=t??[V.at(0),V.at(-1)],a=Math.ceil((n-e)/i);let o=e;for(let t=1;t<a;t++){const n=l(e+t*i,6);b.push({minValue:o,maxValue:n,label:s(o,n,u)}),o=n}b.push({minValue:o,maxValue:n,label:s(o,n,u)})}return b}function s(e,t,l){let n=null;return n=e===t?l&&"percent-of-total"===l?e+"%":e.toString():l&&"percent-of-total"===l?e+"% - "+t+"%":e+" - "+t,n}function o(e){const t=[],l=[];let n=Number.MIN_VALUE,a=1,u=-1;for(let s=0;s<e.length;s++){const o=e[s];o===n?(a++,l[u]=a):null!==o&&(t.push(o),n=o,a=1,l.push(a),u++)}return{uniqueValues:t,valueFrequency:l}}function r(e,t,l){const n=e.length,a=[];l>n&&(l=n);for(let s=0;s<l;s++)a.push(Math.round(s*n/l-1));a.push(n-1);let u=i(a,e,t,l);return c(u.mean,u.sdcm,a,e,t,l)&&(u=i(a,e,t,l)),a}function i(e,t,l,n){let a=[],u=[],s=[],o=0;const r=[],i=[];for(let m=0;m<n;m++){const n=b(m,e,t,l);r.push(n.sbMean),i.push(n.sbSdcm),o+=i[m]}let c,f=o,h=!0;for(;h||o<f;){h=!1,a=[];for(let t=0;t<n;t++)a.push(e[t]);for(let l=0;l<n;l++)for(let a=e[l]+1;a<=e[l+1];a++)if(c=t[a],l>0&&a!==e[l+1]&&Math.abs(c-r[l])>Math.abs(c-r[l-1]))e[l]=a;else if(l<n-1&&e[l]!==a-1&&Math.abs(c-r[l])>Math.abs(c-r[l+1])){e[l+1]=a-1;break}f=o,o=0,u=[],s=[];for(let a=0;a<n;a++){u.push(r[a]),s.push(i[a]);const n=b(a,e,t,l);r[a]=n.sbMean,i[a]=n.sbSdcm,o+=i[a]}}if(o>f){for(let t=0;t<n;t++)e[t]=a[t],r[t]=u[t],i[t]=s[t];o=f}return{mean:r,sdcm:i}}function c(e,t,l,n,a,u){let s=0,o=0,r=0,i=0,c=!0;for(let f=0;f<2&&c;f++){0===f&&(c=!1);for(let f=0;f<u-1;f++)for(;l[f+1]+1!==l[f+2];){l[f+1]=l[f+1]+1;const u=b(f,l,n,a);r=u.sbMean,s=u.sbSdcm;const h=b(f+1,l,n,a);if(i=h.sbMean,o=h.sbSdcm,!(s+o<t[f]+t[f+1])){l[f+1]=l[f+1]-1;break}t[f]=s,t[f+1]=o,e[f]=r,e[f+1]=i,c=!0}for(let f=u-1;f>0;f--)for(;l[f]!==l[f-1]+1;){l[f]=l[f]-1;const u=b(f-1,l,n,a);r=u.sbMean,s=u.sbSdcm;const h=b(f,l,n,a);if(i=h.sbMean,o=h.sbSdcm,!(s+o<t[f-1]+t[f])){l[f]=l[f]+1;break}t[f-1]=s,t[f]=o,e[f-1]=r,e[f]=i,c=!0}}return c}function f(e,t,l,n,a){let u=Math.max(n-e,t-n)/a/l;return u=u>=1?1:u>=.5?.5:.25,u}function h(e){let t=0;for(let l=0;l<e.length;l++)t+=e[l];return t/=e.length,t}function m(e,t){let l=0;for(let n=0;n<e.length;n++){const a=e[n];l+=(a-t)*(a-t)}l/=e.length;return Math.sqrt(l)}function b(e,l,n,a){let u=0,s=0;for(let t=l[e]+1;t<=l[e+1];t++){const e=a[t];u+=n[t]*e,s+=e}s<=0&&t().warn("Exception in Natural Breaks calculation");const o=u/s;let r=0;for(let t=l[e]+1;t<=l[e+1];t++)r+=a[t]*(n[t]-o)**2;return{sbMean:o,sbSdcm:r}}export{a as createGenerateRendererClassBreaks,n as createGenerateRendererUniqueValues};
