/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{pt2px as e}from"../../../../core/screenUtils.js";import{Seconds as t}from"../../../../core/time.js";import{isFlowPathBuffer as n,valuesPerFlowVertex as i}from"../../../3d/support/flow/loadUtils.js";function o(t){const n=s(l(t)),i=n,o=!0,r=Math.max(n/2,5),a=Math.round(e(t.maxPathLength)/r)+1,u=10,{density:c}=t;return{smoothing:e(t.smoothing),interpolate:!0,velocityScale:"flow-from"===t.flowRepresentation?1:-1,verticesPerLine:a,minSpeedThreshold:.001,segmentLength:r,maxTurnAngle:1,collisions:o,lineCollisionWidth:i,lineSpacing:u,density:c,onlyForwardTracing:!0,wrapAround:!1}}function s(e){return"constant"===e.kind?e.value[0]:e.values[e.values.length-1]}function r(e){const t=e.toRgba();return[t[0]/255,t[1]/255,t[2]/255,t[3]]}function a(e){return{kind:"constant",value:[.1,.1,.1,1]}}function l(t){if(!t.hasVisualVariables("size"))return{kind:"constant",value:[e(t.trailWidth)]};const n=t.getVisualVariablesForType("size")[0],i=[],o=[];let s;if(n.stops){for(const t of n.stops)i.push(t.value),o.push(e(t.size));s=n.stops.length}else i.push(n.minDataValue,n.maxDataValue),o.push(e(n.minSize),e(n.maxSize)),s=2;return{kind:"ramp",stops:i,values:o,count:s}}function u(e){if(!e.hasVisualVariables("color"))return{kind:"constant",value:r(e.color)};const t=e.getVisualVariablesForType("color")[0],n=[],i=[];for(const o of t.stops)n.push(o.value),Array.prototype.push.apply(i,r(o.color));return{kind:"ramp",stops:n,values:i,count:t.stops.length}}function c(e){if(!e.hasVisualVariables("opacity"))return{kind:"constant",value:[1]};const t=e.getVisualVariablesForType("opacity")[0],n=[],i=[];for(const o of t.stops)n.push(o.value),i.push(o.opacity);return{kind:"ramp",stops:n,values:i,count:t.stops.length}}function m(e,t,n,i){switch(t){case"int":e.setUniform1iv(n,i);break;case"float":e.setUniform1fv(n,i);break;case"vec2":e.setUniform2fv(n,i);break;case"vec3":e.setUniform3fv(n,i);break;case"vec4":e.setUniform4fv(n,i)}}function p(e,t,n,i){"constant"===i.kind?m(e,n,`u_${t}`,i.value):(m(e,"float",`u_${t}_stops`,i.stops),m(e,n,`u_${t}_values`,i.values),e.setUniform1i(`u_${t}_count`,i.count))}function f(e,t){let n=!0;return n=n&&e.collisions===t.collisions,n=n&&e.density===t.density,n=n&&e.interpolate===t.interpolate,n=n&&e.lineCollisionWidth===t.lineCollisionWidth,n=n&&e.lineSpacing===t.lineSpacing,n=n&&e.maxTurnAngle===t.maxTurnAngle,n=n&&e.minSpeedThreshold===t.minSpeedThreshold,n=n&&e.segmentLength===t.segmentLength,n=n&&e.smoothing===t.smoothing,n=n&&e.velocityScale===t.velocityScale,n=n&&e.verticesPerLine===t.verticesPerLine,n}function h(e,t){return e===t||null!=e&&null!=t&&e.equals(t)}function d(e,t){if(!f(e.simulationSettings,t.simulationSettings))return!1;if(!h(e.timeExtent,t.timeExtent))return!1;let n=!0;return n=n&&e.loadImagery===t.loadImagery,n=n&&e.createFlowMesh===t.createFlowMesh,n=n&&e.color.kind===t.color.kind,n=n&&e.opacity.kind===t.opacity.kind,n=n&&e.size.kind===t.size.kind,n}function x(e,t,n,i){return e/=n.size[0],t=1-(t/=n.size[1]),e*=n.extent.xmax-n.extent.xmin,t*=n.extent.ymax-n.extent.ymin,e+=n.extent.xmin,t+=n.extent.ymin,e-=i.extent.xmin,t-=i.extent.ymin,e/=i.extent.xmax-i.extent.xmin,t=1-(t/=i.extent.ymax-i.extent.ymin),{x:e*=i.size[0],y:t*=i.size[1]}}function g(e,o,s,r){if(0===e.length)return[];const a=[],l=s.time-o.time,u=n(e),c=u?i(e[0].hasMagnitude):1,m=u?({vertices:e},n)=>{const i=e,o=n*c;return{x:i[o],y:i[o+1],time:t(i[o+2]),speed:0}}:({vertices:e},t)=>e[t];for(const n of e){const{stage:e,vertices:i}=n;if(2===e)continue;const u=i.length/c,p=m(n,0),f=p.time,h=(l*r-f)%(m(n,u-1).time-f)+f;let d=p.x,g=p.y,v=p.time;for(let o=1;o<u&&v<h;o++){const e=m(n,o),i=Math.min(e.time,h)-v,s=e.time-v;d+=(e.x-d)*(i/s),g+=(e.y-g)*(i/s),v=t(v+i)}a.push(x(d,g,o,s))}return a}export{d as areStreamlinesCompatible,x as changeQuery,u as getColor,o as getFlowSimulationSettings,a as getForegroundColor,s as getMax,c as getOpacity,g as getPositions,l as getSize,m as setUniform,p as setUniforms,f as simulationSettingsEqual,h as timeExtentsEqual,r as toRgba};
