/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import"../../../../../../core/has.js";import{pt2px as e}from"../../../../../../core/screenUtils.js";import{getMetersPerUnitForSR as a}from"../../../../../../core/units.js";import{meterIn as i}from"../../../../../../renderers/support/lengthUtils.js";import{nanMagicNumber as l}from"../../../../engine/webgl/definitions.js";import{padStops as s,premultiplyColor as t}from"./schemaUtils.js";import{simplifyVisualVariables as r}from"../../support/rendererUtils.js";const u=1.25,n=128,o=128;function p(e){if(!e.stops?.length)return null;const a=e.stops.sort((e,a)=>e.value-a.value),i=s(a,8),l=i.map(({value:e})=>e),r=i.map(({color:e})=>t(e));return{values:l,colors:r}}function c(e){if(!e.stops?.length)return null;const a=e.stops.sort((e,a)=>e.value-a.value),i=s(a,8);return{opacityValues:i.map(({value:e})=>e),opacities:i.map(({opacity:e})=>e)}}function v(e){return{rotationType:"geographic"===e.rotationType?0:1}}function V(a){if(!a.stops?.length)return null;if(a.stops.some(e=>e.useMaxValue||e.useMinValue))return(i,t)=>{const r=i.statisticsByLevel.get(t.key.level),u=a.stops.map(i=>({value:i.useMaxValue?r?.get(a.field)?.maxValue??0:i.useMinValue?r?.get(a.field)?.minValue??0:i.value,size:i.size?e(i.size):l})).sort((e,a)=>e.value-a.value),n=s(u,8);return{values:n.map(({value:e})=>e),sizes:n.map(({size:e})=>e)}};const i=a.stops.sort((e,a)=>e.value-a.value),t=s(i,8);return{values:t.map(({value:e})=>e),sizes:t.map(({size:a})=>e(a))}}function f(e){return l=>{const{state:s}=l;return{unitValueToPixelsRatio:a(s.spatialReference)/i[e.valueUnit??"meters"]/s.resolution}}}function m(e,a){const i=a.length;if(e<a[0].value||1===i)return a[0].size;for(let l=1;l<i;l++)if(e<a[l].value){const i=(e-a[l-1].value)/(a[l].value-a[l-1].value);return a[l-1].size+i*(a[l].size-a[l-1].size)}return a[i-1].size}function S(a){const{minDataValue:i,maxDataValue:l,minSize:s,maxSize:t}=a;if("object"==typeof s&&"object"==typeof t)return(a,r)=>{const u=a.state.scale,n=e(m(u,s.stops)),o=e(m(u,t.stops));return{minMaxValueAndSize:[i,l,n,o]}};if("object"==typeof s||"object"==typeof t)throw new Error("InternalError: Found a partial VisualVariableSizeMinMaxValue");return{minMaxValueAndSize:[i,l,e(s),e(t)]}}const b={visualVariableColor:null,visualVariableOpacity:null,visualVariableRotation:null,visualVariableSizeStops:null,visualVariableSizeScaleStops:null,visualVariableSizeOutlineScaleStops:null,visualVariableSizeUnitValue:null,visualVariableSizeMinMaxValue:null};function z(e,a=o,i=u){if(e.visualVariableSizeMinMaxValue)return"function"==typeof e.visualVariableSizeMinMaxValue?n:Math.max(e.visualVariableSizeMinMaxValue.minMaxValueAndSize[3]*i,a);if(e.visualVariableSizeScaleStops){if("function"==typeof e.visualVariableSizeScaleStops)return n;const l=e.visualVariableSizeScaleStops.sizes;return Math.max(l[l.length-1]*i,a)}if(e.visualVariableSizeStops){if("function"==typeof e.visualVariableSizeStops)return n;const l=e.visualVariableSizeStops.sizes;return Math.max(l[l.length-1]*i,a)}return e.visualVariableSizeUnitValue?2*n:0}function x(e){const a={...b};if(!e||!("visualVariables"in e)||!e.visualVariables)return a;for(const i of r(e.visualVariables))switch(i.type){case"color":a.visualVariableColor=p(i);break;case"opacity":a.visualVariableOpacity=c(i);break;case"rotation":a.visualVariableRotation=v(i);break;case"size":switch(y(i)){case"field-stops":a.visualVariableSizeStops=V(i);break;case"scale-stops":"outline"===i.target?a.visualVariableSizeOutlineScaleStops=V(i):a.visualVariableSizeScaleStops=V(i);break;case"min-max":a.visualVariableSizeMinMaxValue=S(i);break;case"unit-value":a.visualVariableSizeUnitValue=f(i)}break}return a}function y(e){return"number"==typeof e.minDataValue&&"number"==typeof e.maxDataValue&&null!=e.minSize&&null!=e.maxSize?"min-max":"$view.scale"===e?.valueExpression&&Array.isArray(e.stops)?"scale-stops":null==e.field&&"$view.scale"===e?.valueExpression||!(Array.isArray(e.stops)||"levels"in e&&e.levels)?null!=e.field||"$view.scale"!==e?.valueExpression?"unit-value":null:"field-stops"}export{p as createVisualVariableColor,c as createVisualVariableOpacity,v as createVisualVariableRotation,S as createVisualVariableSizeMinMaxValue,V as createVisualVariableSizeStops,f as createVisualVariableSizeUnitValue,x as createVisualVariableUniforms,z as getMaxSizeVVSize,b as noVisualVariables};
