/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import e from"../../core/Error.js";import{ensureClass as i}from"../../core/accessorSupport/ensureType.js";import a from"../../renderers/support/AuthoringInfo.js";import r from"../../renderers/support/AuthoringInfoVisualVariable.js";import l from"../../renderers/visualVariables/OpacityVariable.js";import s from"../../renderers/visualVariables/support/OpacityStop.js";import n from"../../renderers/visualVariables/support/VisualVariableLegendOptions.js";import{processRegenerateParams as t,getRendererToUpdate as o,isOpacityVV as u,getAuthoringInfoVisualVariable as p}from"./support/regenerateUtils.js";import{resolveDataSeriesInfo as m,getTimeSeriesSummaryStatistics as d}from"./support/timeSeriesUtils.js";import{verifyBasicFieldValidity as v,getSummaryStatistics as f,createStopValues as c,getDefaultDataRange as y}from"./support/utils.js";import{verifyBinningParams as w}from"../support/binningUtils.js";import{getFieldsList as g,isAnyDateField as x}from"../support/utils.js";import{binningCapableLayerTypes as V,featureCapableLayerTypes as b,createLayerAdapter as E,getLayerTypeLabels as h}from"../support/adapters/support/layerUtils.js";async function j(i){const a=m({...i});if(!a?.layer||!(a.field||a.valueExpression||a.sqlExpression))throw new e("opacity-visual-variable:missing-parameters","'layer' and 'field', 'valueExpression' or 'sqlExpression' parameters are required");if(a.valueExpression&&!a.view)throw new e("opacity-visual-variable:missing-parameters","View is required when 'valueExpression' is specified");a.forBinning&&w(a,"opacity-visual-variable");const r={...a,layer:a.layer},l=a.forBinning?V:b,s=E(r.layer,l,a.forBinning);if(!s)throw new e("opacity-visual-variable:invalid-parameters","'layer' must be one of these types: "+h(l).join(", "));r.layer=s;const n=null!=r.signal?{signal:r.signal}:null;await s.load(n);const t=await g({field:r.field,normalizationField:r.normalizationField,valueExpression:r.valueExpression}),o=v(s,t,"opacity-visual-variable:invalid-parameters");if(o)throw o;return r}function S(e,t){const o=t.layer,u=t.field,p=u&&!("function"==typeof u)?o.getField(u):null,m=x(p),d=c(e),v=y(e,m,!0),f=v||[d[0],d[4]],w=new l({field:u??void 0,valueExpression:t.valueExpression,valueExpressionTitle:t.valueExpressionTitle,normalizationField:t.normalizationField,stops:[new s({value:f[0],opacity:.3}),new s({value:f[1],opacity:1})],legendOptions:i(n,t.legendOptions)}),g=new r({type:"opacity",dataSeriesName:t.dataSeriesName,minSliderValue:null!=t.minValue?t.minValue:e.min,maxSliderValue:null!=t.maxValue?t.maxValue:e.max}),V=new a({visualVariables:[g]});return Promise.resolve({visualVariable:w,statistics:e,defaultValuesUsed:!!v,authoringInfo:V})}async function z(e){let i=null;return i=e.statistics?e.statistics:e.dataSeriesName?await d({layer:e.layer,view:e.view,dataSeriesName:e.dataSeriesName,filter:e.filter,signal:e.signal}):await f({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}),S(i,e)}async function F(i){const a="regenerate-opacity-visual-variable";await t(i,a);const r=await o(i);if(!("visualVariables"in r))throw new e(`${a}:invalid-parameters`,"Renderer must support visual variables to regenerate an opacity visual variable");const l=r.visualVariables?.find(u);if(!l)throw new e(`${a}:invalid-parameters`,"Renderer does not have an opacity visual variable");const s=p(r,"opacity"),n=l.legendOptions?{title:l.legendOptions.title??void 0,showLegend:l.legendOptions.showLegend??void 0}:null,{layer:m,forBinning:d,filter:v,view:f,signal:c}=i,y=await j({layer:m,field:l.field??null,normalizationField:l.normalizationField??null,valueExpression:l.valueExpression??null,valueExpressionTitle:l.valueExpressionTitle??null,dataSeriesName:s?.dataSeriesName??null,legendOptions:n,minValue:s?.minSliderValue??null,maxValue:s?.maxSliderValue??null,forBinning:d,filter:v,view:f,signal:c});return{...i,creatorParameters:y,renderer:r}}async function O(e){return z(await j(e))}async function q(e){const{creatorParameters:i}=await F(e);return await z(i)}export{O as createVisualVariable,q as regenerateVisualVariable};
