/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
function n(n,t,e){return h(o(n,t,e))}function t(n,t,e,m=e){return h(a(n,t,e,Math.min(m,e)))}function e(n,t,e){const x=n.xmax-n.xmin,i=n.ymax-n.ymin,o=t.xmax-t.xmin,r=t.ymax-t.ymin,a=Math.log2(x/o),u=Math.log2(i/r),c=Math.floor(Math.min(a,u))+1;let s=Math.min(c,e);const f=x/2**s,y=i/2**s,h=Math.floor(t.xmin/f),l=Math.floor(t.xmax/f),d=Math.floor(t.ymin/y),p=Math.floor(t.ymax/y);return(l-h+1>2||p-d+1>2)&&(s-=1),m(n,t.xmin,t.ymin,e,s)}function m(n,t,e,m,x=m){let i=0,o=0n,{xmin:r,ymin:a,xmax:c,ymax:s}=n;for(;i!==x;){const n=(r+c)/2;let x=0;t>n?(x=1,r=n):c=n;const f=(a+s)/2;let y=0;e>f?(y=1,a=f):s=f;o+=u(x|y<<1,i,m),i+=1}return o}function x(n,t){let e=0n;for(let m=0;m<n.length;m++)e+=u(n[m],m,t);return e}function i(n,t){const e=[...n];for(let m=n.length;m<t;m++)e.push(3);return{start:x(n,t),end:x(e,t)}}function o(n,t,e){const m=[],x=[{extent:n,codeSum:0n,depth:0}];for(;x.length;){const{extent:n,codeSum:i,depth:o}=x.pop();if(!c(n,t))continue;if(s(t,n)){const n=i;let t=i;for(let m=o;m<e;m++)t+=u(3,m,e);m.push({start:n,end:t});continue}if(o===e){m.push({start:i,end:i});continue}const r=f(n);for(let t=0;t<r.length;t++)x.push({extent:r[t],codeSum:i+u(t,o,e),depth:o+1})}return m}function r(n){const t=n.xmax-n.xmin,e=n.ymax-n.ymin;return{xmin:n.xmin,ymin:n.ymin,xmax:n.xmax+t,ymax:n.ymax+e}}function a(n,t,e,m){const x=[],i=[{extent:n,codeSum:0n,depth:0}];for(;i.length;){const{extent:n,codeSum:o,depth:a}=i.pop();if(!c(r(n),t))continue;if(s(t,r(n))){const n=o;let t=o;for(let m=a;m<e;m++)t+=u(3,m,e);x.push({start:n,end:t});continue}if(x.push({start:o,end:o}),a===m)continue;const y=f(n);for(let t=0;t<y.length;t++)i.push({extent:y[t],codeSum:o+u(t,a,e),depth:a+1})}return x}function u(n,t,e){return BigInt(n)*y(e,t)+1n}function c(n,t){return!(n.xmax<=t.xmin||n.xmin>=t.xmax||n.ymax<=t.ymin||n.ymin>=t.ymax)}function s(n,t){return t.xmin>=n.xmin&&t.xmax<=n.xmax&&t.ymin>=n.ymin&&t.ymax<=n.ymax}function f(n){const t=(n.xmin+n.xmax)/2,e=(n.ymin+n.ymax)/2;return[{xmin:n.xmin,xmax:t,ymin:n.ymin,ymax:e},{xmin:t,xmax:n.xmax,ymin:n.ymin,ymax:e},{xmin:n.xmin,xmax:t,ymin:e,ymax:n.ymax},{xmin:t,xmax:n.xmax,ymin:e,ymax:n.ymax}]}function y(n,t){return(4n**(BigInt(n)-BigInt(t))-1n)/3n}function h(n){if(0===n.length)return[];n.sort((n,t)=>n.start===t.start?n.end<t.end?-1:1:n.start<t.start?-1:1);const t=[];let e=n[0];for(let m=1;m<n.length;m++){const x=n[m];e.end+1n>=x.start?e={start:e.start,end:e.end>x.end?e.end:x.end}:(t.push(e),e=x)}return t.push(e),t}export{e as getExtentXZCode,m as getPointXZCode,n as getQueryXZCodeRanges,t as getQueryXZCodeRangesExpanded,x as getXZCodeForSequence,i as getXZInterval};
