/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{getSphericalPCPF as e}from"../../../../geometry/spatialReferenceEllipsoidUtils.js";import{create as t,contains as s,intersects as n}from"../../../../geometry/support/aaBoundingBox.js";class i{constructor(t,s,n){this._pages=[],this.pageSize=0,this._nodeSR=t,this._renderSR=s,this._renderSRSphericalPCPF=e(s),this.pageSize=n}addPage(e,t,s=0){for(;this._pages.length<e;)this._pages.push(null);r(t,this._nodeSR,this._renderSR,s,this._renderSRSphericalPCPF),this._pages[e]={nodes:t,parents:new Uint32Array(this.pageSize)},p(this._pages,this.pageSize)}hasPage(e){return!!this._pages[e]}getNode(e){const t=this.pageSize;return this._pages[a(e,t)].nodes[h(e,t)]}getRenderObb(e){const t=this.pageSize;return this._pages[a(e,t)].nodes[h(e,t)].obbInRenderSR}setRenderObb(e,t){const s=this.pageSize;this._pages[a(e,s)].nodes[h(e,s)].obbInRenderSR.copy(t)}getParentId(e){const t=this.pageSize;return this._pages[a(e,t)].parents[h(e,t)]}hasNodes(e,t){const s=a(e,this.pageSize),n=a(e+t-1,this.pageSize);for(let i=s;i<=n;i++)if(null==this._pages[i])return!1;return!0}forEachNodeId(e){for(let t=0;t<this._pages.length;t++){const s=this._pages[t];if(s)for(let n=0;n<s.nodes.length;n++)e(t*this.pageSize+n)}}createVisibilityTraverse(){const e={index:this,queue:[],masks:[],tempAabb:t()};return(t,s)=>o(e,t,s)}}function o(e,t,i){const o=e.index;if(!o.hasNodes(0,1))return;const r=e.queue;r.length=0,r.push(0);const p=e.masks;for(p.length=0,p.push(0);r.length>0;){const h=r.pop();let g=p.pop();const l=o.getNode(h),c=o.getRenderObb(h);let u=!0;if(null!=t.clippingBox){const i=1<<t.frustum.length;0===(g&i)&&(c.toAaBoundingBox(e.tempAabb),s(t.clippingBox,e.tempAabb)?g|=i:n(t.clippingBox,e.tempAabb)||(u=!1))}for(let e=0;e<t.frustum.length&&u;e++){const s=1<<e;if(0===(g&s)){const n=c.intersectPlane(t.frustum[e]);n>0?u=!1:n<0&&(g|=s)}}if(i.predicate(h,l,u)){const e=l.firstChild,t=l.childCount;let s=!1;const n=a(e,o.pageSize),c=a(e+t-1,o.pageSize);for(let r=n;r<=c;r++)if(!o.hasPage(r)){i.pageMiss(h,r),s=!0;break}if(!s)for(let i=0;i<t;i++)r.push(e+i),p.push(g)}}}function r(e,t,s,n,i){for(let o=0;o<e.length;o++){const r=e[o];r.obb.transform(r.obbInRenderSR,t,s,n,i)}}function p(e,t){const s=[0];for(;s.length;){const n=s.pop(),i=e[a(n,t)].nodes[h(n,t)];for(let o=0;o<i.childCount;o++){const r=i.firstChild+o;null!=e[a(r,t)]&&(e[a(r,t)].parents[h(r,t)]=n,s.push(r))}}}function a(e,t){return e/t|0}function h(e,t){return e%t}export{i as PagedNodeIndex};
