/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import i from"../../core/Error.js";import{ensureClass as e}from"../../core/accessorSupport/ensureType.js";import a from"../../renderers/support/AuthoringInfo.js";import r from"../../renderers/support/AuthoringInfoVisualVariable.js";import s from"../../renderers/visualVariables/OpacityVariable.js";import l from"../../renderers/visualVariables/support/OpacityStop.js";import o from"../../renderers/visualVariables/support/VisualVariableLegendOptions.js";import{getSummaryStatistics as n,verifyBasicFieldValidity as t,createStopValues as p,getDefaultDataRange as u}from"./support/utils.js";import{verifyBinningParams as m}from"../support/binningUtils.js";import{getFieldsList as f,isAnyDateField as d}from"../support/utils.js";import{binningCapableLayerTypes as v,featureCapableLayerTypes as c,createLayerAdapter as y,getLayerTypeLabels as x}from"../support/adapters/support/layerUtils.js";async function V(e){if(!e?.layer||!(e.field||e.valueExpression||e.sqlExpression))throw new i("opacity-visual-variable:missing-parameters","'layer' and 'field', 'valueExpression' or 'sqlExpression' parameters are required");if(e.valueExpression&&!e.view)throw new i("opacity-visual-variable:missing-parameters","View is required when 'valueExpression' is specified");e.forBinning&&m(e,"opacity-visual-variable");const a={...e,layer:e.layer},r=e.forBinning?v:c,s=y(a.layer,r,e.forBinning);if(!s)throw new i("opacity-visual-variable:invalid-parameters","'layer' must be one of these types: "+x(r).join(", "));a.layer=s;const l=null!=a.signal?{signal:a.signal}:null;await s.load(l);const o=await f({field:a.field,normalizationField:a.normalizationField,valueExpression:a.valueExpression}),n=t(s,o,"opacity-visual-variable:invalid-parameters");if(n)throw n;return a}function w(i,n){const t=n.layer,m=n.field,f=m&&!("function"==typeof m)?t.getField(m):null,v=d(f),c=p(i),y=u(i,v,!0),x=y||[c[0],c[4]],V=new s({field:m??void 0,valueExpression:n.valueExpression,valueExpressionTitle:n.valueExpressionTitle,normalizationField:n.normalizationField,stops:[new l({value:x[0],opacity:.3}),new l({value:x[1],opacity:1})],legendOptions:e(o,n.legendOptions)}),w=new r({type:"opacity",minSliderValue:null!=n.minValue?n.minValue:i.min,maxSliderValue:null!=n.maxValue?n.maxValue:i.max}),g=new a({visualVariables:[w]});return Promise.resolve({visualVariable:V,statistics:i,defaultValuesUsed:!!y,authoringInfo:g})}async function g(i){const e=await V(i);let a=null;return a=e.statistics?e.statistics:await n({layer:e.layer,field:e.field,valueExpression:e.valueExpression,sqlExpression:e.sqlExpression,sqlWhere:e.sqlWhere,normalizationType:e.normalizationField?"field":void 0,normalizationField:e.normalizationField,minValue:e.minValue,maxValue:e.maxValue,filter:e.filter,view:e.view,signal:e.signal}),w(a,e)}export{g as createVisualVariable};
