/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import has from"../../../core/has.js";import{PooledRBush as e}from"../../../core/libs/rbush/PooledRBush.js";import t from"../MeshGeoreferencedVertexSpace.js";import{convertVertexSpace as r}from"./vertexSpaceConversion.js";class n{async createIndex(t,r){const n=new Array;if(!t.vertexAttributes?.position)return new e;const o=a(t),s=null!=r?await r.invoke("createIndexThread",o,{transferList:n}):this.createIndexThread(o).result;return i().fromJSON(s)}createIndexThread(e){const t=i();if(!e)return{result:t.toJSON()};const r=new Float64Array(e.position);return e.components?s(t,r,e.components.map(e=>new Uint32Array(e))):o(t,r)}}function o(e,t){const r=new Array(t.length/9);let n=0;for(let o=0;o<t.length;o+=9)r[n++]=c(t,o,o+3,o+6);return e.load(r),{result:e.toJSON()}}function s(e,t,r){let n=0;for(const a of r)n+=a.length/3;const o=new Array(n);let s=0;for(const a of r)for(let e=0;e<a.length;e+=3)o[s++]=c(t,3*a[e],3*a[e+1],3*a[e+2]);return e.load(o),{result:e.toJSON()}}function a(e){const{vertexAttributes:{position:n},vertexSpace:o,spatialReference:s,transform:a}=e,i=r({vertexAttributes:{position:n},vertexSpace:o,spatialReference:s,transform:a},t.absolute,{allowBufferReuse:!0})?.position;return i?!e.components||e.components.some(e=>!e.faces)?{position:i.buffer}:{position:i.buffer,components:e.components.map(e=>e.faces)}:null}function i(){return new e(9,has("esri-csp-restrictions")?e=>e:[".minX",".minY",".maxX",".maxY"])}function c(e,t,r,n){return{minX:Math.min(e[t],e[r],e[n]),maxX:Math.max(e[t],e[r],e[n]),minY:Math.min(e[t+1],e[r+1],e[n+1]),maxY:Math.max(e[t+1],e[r+1],e[n+1]),p0:[e[t],e[t+1],e[t+2]],p1:[e[r],e[r+1],e[r+2]],p2:[e[n],e[n+1],e[n+2]]}}export{n as default};
