/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import i from"../../core/Error.js";import{isTimeOnlyField as e,numericTypes as s}from"../../layers/support/fieldUtils.js";import{verifyBasicFieldValidity as t,verifyFieldType as r,verifyFilterValidity as a}from"./support/utils.js";import{verifyBinningParams as o}from"../support/binningUtils.js";import{getNormalizationType as n,getFieldsList as l,isAnyDateField as f,dateTypes as p}from"../support/utils.js";import{defaultSupportedLayerTypes as d,binningCapableLayerTypes as m,createLayerAdapter as h,getLayerTypeLabels as u}from"../support/adapters/support/layerUtils.js";const w=[...p,"time-only",...s];async function c(p){if(!p?.layer||!(p.field||p.fields?.length||p.valueExpression||p.sqlExpression))throw new i("histogram:missing-parameters","'layer' and 'field', 'fields', 'valueExpression' or 'sqlExpression' parameters are required");if(p.valueExpression&&!p.sqlExpression&&!p.view)throw new i("histogram:missing-parameters","View is required when 'valueExpression' is specified");p.forBinning&&o(p,"histogram");const{layer:c,...g}=p,v=[...d,5],y=p.forBinning?m:v,x=h(c,y,p.forBinning);if(!x)throw new i("histogram:invalid-parameters","'layer' must be one of these types: "+u(y).join(", "));const E={layerAdapter:x,...g};E.normalizationType=n(E);const q=null!=E.signal?{signal:E.signal}:null;await x.load(q);const z=await l({field:E.field,normalizationField:E.normalizationField,valueExpression:E.valueExpression,fields:E.fields}),A=t(x,z,"histogram:invalid-parameters");if(A)throw A;const b=!E.classificationMethod||"equal-interval"===E.classificationMethod;if(E.fields?.length){const e=E.fields;if(e.length<2)throw new i("histogram:invalid-parameters","'fields' must contain at least 2 fields if specified.");if(E.normalizationType||E.valueExpression||E.field)throw new i("histogram:invalid-parameters","Normalization, 'field', 'valueExpression' are not allowed when 'fields' is specified.");const t=new Set;for(const i of e){const e=x.getField(i),a=r(x,e,"histogram:invalid-parameters",s);if(a)throw a;t.add(e.type)}if(t.size>1)throw new i("histogram:invalid-parameters","All fields must be of the same type when 'fields' is specified.");if(!b)throw new i("histogram:invalid-parameters","'classificationMethod' other than 'equal-interval' is not allowed when 'fields' is specified")}else{if(E.valueExpression||E.sqlExpression){if(E.normalizationType)throw new i("histogram:invalid-parameters","Normalization is not allowed when 'valueExpression' or 'sqlExpression' is specified");if(!b)throw new i("histogram:invalid-parameters","'classificationMethod' other than 'equal-interval' is not allowed when 'valueExpression' or 'sqlExpression' is specified")}else if(E.field){const s=x.getField(E.field),t=r(x,s,"histogram:invalid-parameters",w);if(t)throw t;if(f(s)||e(s)){if(E.normalizationType)throw new i("histogram:invalid-parameters","Normalization is not allowed for date fields");if(!b)throw new i("histogram:invalid-parameters","'classificationMethod' other than 'equal-interval' is not allowed for date fields")}}}E.filter&&!E.filter.spatialRelationship&&(E.filter.spatialRelationship="intersects");const j=a(E.filter,"histogram:invalid-parameters");if(j)throw j;if(null==E.useQueryAttributeBins&&(E.useQueryAttributeBins=!0),!E.useQueryAttributeBins&&E.returnAggregateIds)throw new i("histogram:invalid-parameters","'returnAggregateIds' cannot be true when 'useQueryAttributeBins' is false.");return E}async function g(i){const{layerAdapter:e,...s}=await c(i);return e.histogram(s)}export{g as default};
