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