/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{width as t,height as n,containsXY as e,create as r}from"../../../../geometry/support/aaBoundingRect.js";import{modelToMapSpace as o,insideMapExtent as l,isFullExtent as u}from"./FlowQuery3D.js";import{createFlowFieldFromData as i}from"../../../support/flow/dataUtils.js";function s(t){return t?4:3}function f(t){return t[0].vertices instanceof Float32Array}class a{constructor(t,n,e,r,o,l){this.data=t,this.mask=n,this.width=e,this.height=r,this.lij=o,this.extent=l}}function c(t,n){let[e,o,l,i]=[null,null,null,null];for(const r of t){const t=r.extent;e=m(t[0],e),o=m(t[1],o),l=h(t[2],l),i=h(t[3],i)}if(null==e||null==o||null==l||null==i)return null;if(u(e,l,n)){const n=[...t].toSorted((t,n)=>t.extent[0]-n.extent[0]);let r=n[0].extent[2],o=0,u=0,i=0;for(let t=1;t<n.length;t++){const[e,l,s]=n[t].extent;if(r<e&&(i++,o=r,u=e,i>1))break;r=Math.max(r,s)}1===i&&(e=u,l=o)}return r([e,o,l,i])}function h(t,n){return null==n?t:Math.max(t,n)}function m(t,n){return null==n?t:Math.min(t,n)}function d(r,u,s){const[f,a]=s.modelSize;let c=null;const h=new Map;u.forEach(t=>{h.set(t.lij,i(r,t))});const m=(t,n,r)=>e(t.extent,n,r);return(e,i)=>{const d=Math.round(e),x=Math.round(i);if(!r.wrapAround&&(d<0||d>=f||x<0||x>=a))return[0,0];const[p,g]=o(s,e,i,!0);if(!l(s,p,g))return[0,0];if(null==c||!m(c,p,g)){c=null;for(const[t,n]of u)if(m(n,p,g)){c=n;break}}if(null==c?.data)return[0,0];const j=h.get(c.lij);if(null==j)return[0,0];const{width:w,height:M,extent:k}=c;return j((p-k[0])/t(k)*w,M-(g-k[1])/n(k)*M)}}export{a as FlowDataTile,c as boundingRectOfTiles,d as createFlowFieldFromDataTiled,f as isFlowPathBuffer,s as valuesPerFlowVertex};
