/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import"../core/has.js";import{rad2deg as t}from"../core/mathUtils.js";import{set as o}from"../core/libs/gl-matrix-2/math/vec2.js";import{create as r}from"../core/libs/gl-matrix-2/factories/vec2f64.js";import n from"../geometry/Polygon.js";import e from"../geometry/SpatialReference.js";import{toExtent as s}from"../geometry/support/aaBoundingRect.js";import{earth as c}from"../geometry/support/Ellipsoid.js";import{convertFromPolygon as i}from"../layers/graphics/featureConversionUtils.js";import{project as f}from"../layers/graphics/data/projectionSupport.js";const l=16,a=8,u=4,h=2,m=1,p=r(),g=r(),x=-90,d=90,y=-180,M=180,j="0123456789bcdefghjkmnpqrstuvwxyz",S=64;function b(t,o,r,c){const l=[t.xmin,t.ymin,t.xmax,t.ymax],a=n.fromExtent(s(l,c)),u=f(a,c,e.WGS84,{extendedParams:{densificationStep:o*S}});if(!u)return null;const h=i(u,!1,!1),m=h.coords.filter((t,o)=>!(o%2)),p=h.coords.filter((t,o)=>o%2),g=Math.min(...m),x=Math.min(...p),d=Math.max(...m),y=Math.max(...p),M=v(g,x,r,e.WGS84),j=v(d,y,r,e.WGS84);return M&&j?{bounds:l,geohashBounds:{xLL:M[0],yLL:M[1],xTR:j[0],yTR:j[1]},level:r}:null}function v(o,r,n,s){if(s.isWebMercator){const e=t(o/c.radius),s=e-360*Math.floor((e+180)/360),i=[0,0];return B(i,0,t(Math.PI/2-2*Math.atan(Math.exp(-r/c.radius))),s,n),i}const i=f({x:o,y:r},s,e.WGS84);if(!i)return null;const l=[0,0];return B(l,0,i.y,i.x,n),l}function W(t){return t<=57?t-48:t<=104?t-88:t<=107?t-89:t<=110?t-90:t-91}function C(t){return j[t]}function G(t){return(t[0]+t[1])/2}function L(t,o){const r=G(t),n=o,e=!o;t[0]=e*t[0]+n*r,t[1]=e*r+n*t[1]}function R(t,o){const r=o>G(t);return L(t,r),r}function A(t,r){const n=o(p,x,d),e=o(g,y,M);for(let o=0;o<r.length;o++){const t=W(r.charCodeAt(o));o%2==0?(L(e,!!(l&t)),L(e,!!(u&t)),L(e,!!(m&t)),L(n,!!(a&t)),L(n,!!(h&t))):(L(n,!!(l&t)),L(n,!!(u&t)),L(n,!!(m&t)),L(e,!!(a&t)),L(e,!!(h&t)))}return t[0]=G(n),t[1]=G(e),t}function P(t,o){let r=0,n=0,e=30,s=30;for(let c=0;c<o.length;c++){const t=W(o.charCodeAt(c)),i=c%2==0;e-=i?3:2,s-=i?2:3,r|=U(t,i)<<e,n|=k(t,i)<<s}return{geohashX:r,geohashY:n}}function X(t,o){let r=-90,n=90,e=-180,s=180;for(let c=0;c<o;c++){const o=Math.ceil((c+1)/2),i=Math.floor((c+1)/2),f=1-c%2,l=30-(3*o+2*i),a=30-(2*o+3*i),u=3*f+2*(1-f),h=2*f+3*(1-f),m=3*f+7*(1-f)<<a,p=(7*f+3*(1-f)<<l&t.geohashX)>>l,g=(m&t.geohashY)>>a;for(let t=u-1;t>=0;t--){const o=(e+s)/2,r=p&1<<t?1:0;e=(1-r)*e+r*o,s=(1-r)*o+r*s}for(let t=h-1;t>=0;t--){const o=(r+n)/2,e=g&1<<t?1:0;r=(1-e)*r+e*o,n=(1-e)*o+e*n}}return[e,r,s,n]}function Y(t,o,r,n){n%2&&(n+=1);let e=0,s=0,c=-90,i=90,f=-180,l=180;for(let a=0;a<n/2;a++){for(let t=0;t<5;t++){const o=(f+l)/2,n=r>o?1:0;e|=n<<29-(t+5*a),f=(1-n)*f+n*o,l=(1-n)*o+n*l}for(let t=0;t<5;t++){const r=(c+i)/2,n=o>r?1:0;s|=n<<29-(t+5*a),c=(1-n)*c+n*r,i=(1-n)*r+n*i}}t.geohashX=e,t.geohashY=s}function B(t,o,r,n,e){e%2&&(e+=1);let s=0,c=0,i=-90,f=90,l=-180,a=180;for(let u=0;u<e/2;u++){for(let t=0;t<5;t++){const o=(l+a)/2,r=n>o?1:0;s|=r<<29-(t+5*u),l=(1-r)*l+r*o,a=(1-r)*o+r*a}for(let t=0;t<5;t++){const o=(i+f)/2,n=r>o?1:0;c|=n<<29-(t+5*u),i=(1-n)*i+n*o,f=(1-n)*o+n*f}}t[2*o]=s,t[2*o+1]=c}function E(t,r,n){let e="";const s=o(p,-90,90),c=o(g,-180,180);for(let o=0;o<n;o++){let n=0;!(o%2)?(n|=R(c,r)<<4,n|=R(s,t)<<3,n|=R(c,r)<<2,n|=R(s,t)<<1,n|=R(c,r)|0):(n|=R(s,t)<<4,n|=R(c,r)<<3,n|=R(s,t)<<2,n|=R(c,r)<<1,n|=R(s,t)|0),e+=C(n)}return e}function T(t,o,r){return r?t&m|(o&m)<<1|(t&h)<<1|(o&h)<<2|(t&u)<<2:o&m|(t&m)<<1|(o&h)<<1|(t&h)<<2|(o&u)<<2}function U(t,o){return o?m&t|(u&t)>>1|(l&t)>>2:(h&t)>>1|(a&t)>>2}function k(t,o){return o?(h&t)>>1|(a&t)>>2:m&t|(u&t)>>1|(l&t)>>2}function q(t,o,r){const n=!((t.length-1)%2),e=t.slice(0,-1),s=W(t.charCodeAt(t.length-1));let c=0,i=0,f=0,p=0;n?(c=8,i=4,f=m&s|(u&s)>>1|(l&s)>>2,p=(h&s)>>1|(a&s)>>2):(c=4,i=8,p=m&s|(u&s)>>1|(l&s)>>2,f=(h&s)>>1|(a&s)>>2);const g=f+o,x=p+r,d=Math.floor(g/c),y=Math.floor(x/i),M=C(T(g-d*c,x-y*i,n));return t.length>1&&(d||y)?q(e,d,y)+M:e+M}export{P as convertGeohash32ToXY,W as decodeBase32Char,A as decodeGeohash,X as decodeGeohashXY,C as encodeBase32Char,E as encodeGeohash,v as getGeohash,b as getGeohashBounds,q as getRelativeGeohash,B as setGeohashBuf,Y as setGeohashXY,U as unpackXBits,k as unpackYBits};
