function M(e){const t=+this._x.call(null,e),i=+this._y.call(null,e);return I(this.cover(t,i),t,i,e)}function I(e,t,i,r){if(isNaN(t)||isNaN(i))return e;var n,s=e._root,h={data:r},l=e._x0,o=e._y0,a=e._x1,u=e._y1,f,_,c,x,g,y,v,N;if(!s)return e._root=h,e;for(;s.length;)if((g=t>=(f=(l+a)/2))?l=f:a=f,(y=i>=(_=(o+u)/2))?o=_:u=_,n=s,!(s=s[v=y<<1|g]))return n[v]=h,e;if(c=+e._x.call(null,s.data),x=+e._y.call(null,s.data),t===c&&i===x)return h.next=s,n?n[v]=h:e._root=h,e;do n=n?n[v]=new Array(4):e._root=new Array(4),(g=t>=(f=(l+a)/2))?l=f:a=f,(y=i>=(_=(o+u)/2))?o=_:u=_;while((v=y<<1|g)===(N=(x>=_)<<1|c>=f));return n[N]=s,n[v]=h,e}function j(e){var t,i,r=e.length,n,s,h=new Array(r),l=new Array(r),o=1/0,a=1/0,u=-1/0,f=-1/0;for(i=0;i<r;++i)isNaN(n=+this._x.call(null,t=e[i]))||isNaN(s=+this._y.call(null,t))||(h[i]=n,l[i]=s,n<o&&(o=n),n>u&&(u=n),s<a&&(a=s),s>f&&(f=s));if(o>u||a>f)return this;for(this.cover(o,a).cover(u,f),i=0;i<r;++i)I(this,h[i],l[i],e[i]);return this}function Q(e,t){if(isNaN(e=+e)||isNaN(t=+t))return this;var i=this._x0,r=this._y0,n=this._x1,s=this._y1;if(isNaN(i))n=(i=Math.floor(e))+1,s=(r=Math.floor(t))+1;else{for(var h=n-i||1,l=this._root,o,a;i>e||e>=n||r>t||t>=s;)switch(a=(t<r)<<1|e<i,o=new Array(4),o[a]=l,l=o,h*=2,a){case 0:n=i+h,s=r+h;break;case 1:i=n-h,s=r+h;break;case 2:n=i+h,r=s-h;break;case 3:i=n-h,r=s-h;break}this._root&&this._root.length&&(this._root=l)}return this._x0=i,this._y0=r,this._x1=n,this._y1=s,this}function P(){var e=[];return this.visit(function(t){if(!t.length)do e.push(t.data);while(t=t.next)}),e}function X(e){return arguments.length?this.cover(+e[0][0],+e[0][1]).cover(+e[1][0],+e[1][1]):isNaN(this._x0)?void 0:[[this._x0,this._y0],[this._x1,this._y1]]}function w(e,t,i,r,n){this.node=e,this.x0=t,this.y0=i,this.x1=r,this.y1=n}function Y(e,t,i){var r,n=this._x0,s=this._y0,h,l,o,a,u=this._x1,f=this._y1,_=[],c=this._root,x,g;for(c&&_.push(new w(c,n,s,u,f)),i==null?i=1/0:(n=e-i,s=t-i,u=e+i,f=t+i,i*=i);x=_.pop();)if(!(!(c=x.node)||(h=x.x0)>u||(l=x.y0)>f||(o=x.x1)<n||(a=x.y1)<s))if(c.length){var y=(h+o)/2,v=(l+a)/2;_.push(new w(c[3],y,v,o,a),new w(c[2],h,v,y,a),new w(c[1],y,l,o,v),new w(c[0],h,l,y,v)),(g=(t>=v)<<1|e>=y)&&(x=_[_.length-1],_[_.length-1]=_[_.length-1-g],_[_.length-1-g]=x)}else{var N=e-+this._x.call(null,c.data),q=t-+this._y.call(null,c.data),b=N*N+q*q;if(b<i){var d=Math.sqrt(i=b);n=e-d,s=t-d,u=e+d,f=t+d,r=c.data}}return r}function B(e){if(isNaN(u=+this._x.call(null,e))||isNaN(f=+this._y.call(null,e)))return this;var t,i=this._root,r,n,s,h=this._x0,l=this._y0,o=this._x1,a=this._y1,u,f,_,c,x,g,y,v;if(!i)return this;if(i.length)for(;;){if((x=u>=(_=(h+o)/2))?h=_:o=_,(g=f>=(c=(l+a)/2))?l=c:a=c,t=i,!(i=i[y=g<<1|x]))return this;if(!i.length)break;(t[y+1&3]||t[y+2&3]||t[y+3&3])&&(r=t,v=y)}for(;i.data!==e;)if(n=i,!(i=i.next))return this;return(s=i.next)&&delete i.next,n?(s?n.next=s:delete n.next,this):t?(s?t[y]=s:delete t[y],(i=t[0]||t[1]||t[2]||t[3])&&i===(t[3]||t[2]||t[1]||t[0])&&!i.length&&(r?r[v]=i:this._root=i),this):(this._root=s,this)}function C(e){for(var t=0,i=e.length;t<i;++t)this.remove(e[t]);return this}function D(){return this._root}function E(){var e=0;return this.visit(function(t){if(!t.length)do++e;while(t=t.next)}),e}function F(e){var t=[],i,r=this._root,n,s,h,l,o;for(r&&t.push(new w(r,this._x0,this._y0,this._x1,this._y1));i=t.pop();)if(!e(r=i.node,s=i.x0,h=i.y0,l=i.x1,o=i.y1)&&r.length){var a=(s+l)/2,u=(h+o)/2;(n=r[3])&&t.push(new w(n,a,u,l,o)),(n=r[2])&&t.push(new w(n,s,u,a,o)),(n=r[1])&&t.push(new w(n,a,h,l,u)),(n=r[0])&&t.push(new w(n,s,h,a,u))}return this}function G(e){var t=[],i=[],r;for(this._root&&t.push(new w(this._root,this._x0,this._y0,this._x1,this._y1));r=t.pop();){var n=r.node;if(n.length){var s,h=r.x0,l=r.y0,o=r.x1,a=r.y1,u=(h+o)/2,f=(l+a)/2;(s=n[0])&&t.push(new w(s,h,l,u,f)),(s=n[1])&&t.push(new w(s,u,l,o,f)),(s=n[2])&&t.push(new w(s,h,f,u,a)),(s=n[3])&&t.push(new w(s,u,f,o,a))}i.push(r)}for(;r=i.pop();)e(r.node,r.x0,r.y0,r.x1,r.y1);return this}function H(e){return e[0]}function J(e){return arguments.length?(this._x=e,this):this._x}function K(e){return e[1]}function L(e){return arguments.length?(this._y=e,this):this._y}function O(e,t,i){var r=new A(t??H,i??K,NaN,NaN,NaN,NaN);return e==null?r:r.addAll(e)}function A(e,t,i,r,n,s){this._x=e,this._y=t,this._x0=i,this._y0=r,this._x1=n,this._y1=s,this._root=void 0}function k(e){for(var t={data:e.data},i=t;e=e.next;)i=i.next={data:e.data};return t}var p=O.prototype=A.prototype;p.copy=function(){var e=new A(this._x,this._y,this._x0,this._y0,this._x1,this._y1),t=this._root,i,r;if(!t)return e;if(!t.length)return e._root=k(t),e;for(i=[{source:t,target:e._root=new Array(4)}];t=i.pop();)for(var n=0;n<4;++n)(r=t.source[n])&&(r.length?i.push({source:r,target:t.target[n]=new Array(4)}):t.target[n]=k(r));return e};p.add=M;p.addAll=j;p.cover=Q;p.data=P;p.extent=X;p.find=Y;p.remove=B;p.removeAll=C;p.root=D;p.size=E;p.visit=F;p.visitAfter=G;p.x=J;p.y=L;export{O as q};
