/*!
* WP Grid Builder Map Facet Add-on
*
* @package   WP Grid Builder - Map Facet
* @author    Loïc Blascos
* @link      https://www.wpgridbuilder.com
* @copyright 2019-2024 Loïc Blascos
*
*/
(()=>{"use strict";var t={43:t=>{t.exports=function t(e,s){if(e===s)return!0;if(e&&s&&"object"==typeof e&&"object"==typeof s){if(e.constructor!==s.constructor)return!1;var r,i,o;if(Array.isArray(e)){if((r=e.length)!=s.length)return!1;for(i=r;0!=i--;)if(!t(e[i],s[i]))return!1;return!0}if(e.constructor===RegExp)return e.source===s.source&&e.flags===s.flags;if(e.valueOf!==Object.prototype.valueOf)return e.valueOf()===s.valueOf();if(e.toString!==Object.prototype.toString)return e.toString()===s.toString();if((r=(o=Object.keys(e)).length)!==Object.keys(s).length)return!1;for(i=r;0!=i--;)if(!Object.prototype.hasOwnProperty.call(s,o[i]))return!1;for(i=r;0!=i--;){var n=o[i];if(!t(e[n],s[n]))return!1}return!0}return e!=e&&s!=s}}},e={};function s(r){var i=e[r];if(void 0!==i)return i.exports;var o=e[r]={exports:{}};return t[r](o,o.exports,s),o.exports}s.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return s.d(e,{a:e}),e},s.d=(t,e)=>{for(var r in e)s.o(e,r)&&!s.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:e[r]})},s.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),(()=>{let t,e,r,i;function o(s){s?.closest?.(".wp-grid-builder.wpgb-enabled[data-instance]")&&s?.querySelector(".wpgb-marker-content")?.classList?.add?.("wpgb-has-grid"),function(){const t=window?.wpgb_settings?.lightbox?.plugin||"";e=["modulobox","modulobox_lite"].includes(t),r="easy_fancybox"===t,i="foobox"===t,e&&"ModuloBox"in window&&(window.ModuloBox.prototype.openFromQuery=()=>{})}(),function(){const s=function(){if(!e||!("ModuloBox"in window))return;if(t)return t;t=new window.ModuloBox,t.init(),t&&(t.options.history=!1);return t}();if(!s)return;s.getGalleries()}(),r&&window.easy_fancybox_handler&&window.easy_fancybox_auto&&(jQuery(window.easy_fancybox_handler),jQuery(window.easy_fancybox_auto)),i&&window?.FOOBOX?.init&&window.FOOBOX.init()}!function(){if(!window?.WP_Grid_Builder?.Lightbox)return;const{lightbox:t}=window.wpgb_settings?wpgb_settings:{};WP_Grid_Builder.Lightbox({...t,selector:".wp-grid-builder.wpgb-marker-content:not(.wpgb-has-grid) .wpgb-lightbox"}).init()}();const n=document.head.appendChild(document.createElement("style")).sheet;n.facets=[];class a{loadMarkerStyles(){if(n.facets.includes(this.facet.id)||!this?.facet?.settings?.map_markers_style)return;this.facet?.settings?.map_fonts&&n.insertRule(`@import url(${this.facet.settings.map_fonts})`,0),n.facets.push(this.facet.id);const t=new RegExp("((\\s*?(?:\\/\\*[\\s\\S]*?\\*\\/)?\\s*?@media[\\s\\S]*?){([\\s\\S]*?)}\\s*?})|(([\\s\\S]*?){([\\s\\S]*?)})","gi");let e=t.exec(this.facet.settings.map_markers_style);for(;e&&null!==e;){-1===(void 0===e[2]?e[5]:e[2]).indexOf("@media")&&(n.insertRule(e[0].replace(/\.wpgb-marker-style-/g,`.wpgb-marker-content.wpgb-marker-style-${this.facet.id}-`),n.cssRules.length),e=t.exec(this.facet.settings.map_markers_style))}}generateMarker(t,e=!1){let s;if(s=""!==t?.icon?.content&&void 0!==t?.icon?.content?this.generateMarkerContent(t):this.generateMarkerIcon(t),e){this.generateMarkerWrapper();const t=this.markerWrapper.cloneNode();return t.appendChild(s),t}return s}generateMarkerWrapper(){this.markerWrapper||(this.markerWrapper=document.createElement("div"),this.markerWrapper.className="wpgb-marker",this.facet.settings.map_marker_content&&(this.markerWrapper.role="button",this.markerWrapper.tabIndex=0))}generateClusterWrapper(){this.clusterWrapper||(this.clusterWrapper=document.createElement("div"),this.clusterWrapper.className="wpgb-marker",this.clusterWrapper.role="button",this.clusterWrapper.tabIndex=0)}generateMarkerIcon(t){this.markerIcon||(this.markerIcon=document.createElement("img"),this.markerIcon.className="wpgb-marker-icon");const e=this.markerIcon.cloneNode();return e.src=t?.icon?.url,e.height=t?.icon?.size?.height||40,e.style.height=`${t?.icon?.size?.height||40}px`,e}generateMarkerContent(t){this.markerContent||(this.markerContent=document.createElement("div"),this.markerContent.className="wpgb-marker-content");const e=this.markerContent.cloneNode();return e.className+=t?.icon?.style_id?` wpgb-marker-style-${this.facet.id}-${t.icon.style_id}`:" wpgb-marker-style-default",e.innerHTML=t.icon.content,e}generateCluster(t,e=!1){const{map_clusters:s}=this.facet.settings;let r=s.filter((e=>t<=(e?.cluster_count||1/0))).sort(((t,e)=>(t?.cluster_count||1/0)-(e?.cluster_count||1/0)))?.[0];r?.cluster_icon||(r=s.reduce(((e,s)=>Math.abs((s?.cluster_count||1/0)-t)<Math.abs((e?.cluster_count||1/0)-t)?s:e))),this.clusterIcon||(this.clusterElement=document.createElement("div"),this.clusterIcon=document.createElement("img"),this.clusterElement.className="wpgb-cluster-icon",this.clusterElement.append(this.clusterIcon),this.clusterElement.append(document.createElement("span")));const i=this.clusterElement.cloneNode(!0);if(i.firstChild.src=r?.cluster_icon,i.firstChild.height=r?.cluster_size||50,i.firstChild.style.height=`${r?.cluster_size||50}px`,i.lastChild.textContent=t,i.lastChild.style.color=r?.cluster_color||"",e){this.generateClusterWrapper(!0);const t=this.clusterWrapper.cloneNode();return t.appendChild(i),t}return i}setPopupClass(t){if(!Object.prototype.hasOwnProperty.call(t?.attributes||{},"width"))return;const e=this.facet?.holder?.querySelector?.(".wpgb-map-facet");e?.classList?.add?.("wpgb-marker-popup")}setPopupWidth(t){if(!Object.prototype.hasOwnProperty.call(t?.attributes||{},"width"))return;const e=document.createRange();e.selectNodeContents(e.createContextualFragment("<template>").lastChild);const s=e.createContextualFragment(`<div class="wp-grid-builder wpgb-marker-content wpgb-enabled">${t.content}</div>`),r=s?.querySelector?.(".wpgb-marker-content"),i=document.createElement("div"),o=this.facet?.holder?.offsetWidth;r&&(r.style.width=`${Math.min(o-24,t.attributes.width)}px`),i.appendChild(s.cloneNode(!0)),t.content=i.innerHTML}updateMarkerContent(t,e){this.setPopupClass(t),this.setPopupWidth(t),this.setMarkerContent(t.content||t.message,e),this.setPopupClass(t),o(this.facet?.holder)}getMarkerContent(t,e,s){const{mapFacet:{restUrl:r,restNonce:i}}=wpgb_settings,{source:o}=this.facet.settings,{lang:n}=this.facet,a=`${r}?id=${t}&facet=${this.facet.id}&lat=${e}&lng=${s}&source=${o}${n?`&lang=${n}`:""}`;if(this.cache||(this.cache=new Map),this.cache.has(a)){const e=this.cache.get(a);this.updateMarkerContent({...e},t)}else this.xhr&&this.xhr.abort(),delete this.xhr,this.xhr=new XMLHttpRequest,this.xhr.onload=e=>this.onLoad(e,t,a),this.xhr.onerror=e=>this.onLoad(e,t,a),this.xhr.open("GET",a),this.xhr.setRequestHeader("Content-Type","application/json;charset=UTF-8"),i&&this.xhr.setRequestHeader("X-WP-Nonce",i),this.xhr.send()}onLoad(t,e,s){const r=JSON.parse(t.target.responseText);r?.success&&this.cache.set(s,r),this.updateMarkerContent({...r},e),delete this.xhr}setMapPosition(){this.hasBounds()||(this.hasLocation()?this.updateLocation():this.shouldCenter()?this.centerMarkers():this.canCenter()||this.autoCenterMap())}refreshPosition(){const t=this.facet.queryString,e=this.hasMarkers(),s=this.hasBounds(),r=this.canCenter(),i=this.hasLocation();this.hasRefreshed||(!i||s?t&&e||!r?s?this.updateBounds():e?this.centerMarkers():e||this.autoCenterMap():this.centerMap():this.updateLocation())}setLocation(t,e,s,r="km",i,o){if(this.circleColor=o||"rgb(54, 138, 252)",this.removeLocation(),!t&&!e)return void(this.location=!1);t=parseFloat(t),e=parseFloat(e),s=parseFloat(s),s="mi"===r?1.60934*s:s;const n=.009*(s=Math.min(20037.5,s)+.005),a=Math.max(-90,t-n),h=Math.min(90,t+n),c=Math.max(-180,e-n/Math.cos(t*Math.PI/180)),p=Math.min(180,e+n/Math.cos(t*Math.PI/180));this.location={lat:t,lng:e,rad:s,min:{lat:a,lng:c},max:{lat:h,lng:p}},i&&this.drawLocation()}hasLocation(){return this.location&&Object.keys(this.location).length>4}updateLocation(){!this.hasBounds()&&this.centerLocation()}hasBounds(){const{selected:t}=this.facet;return t&&t.length>=4}hasMarkers(){return this.facet.geoJSON.features.length>0}canCenter(){const{map_lat:t,map_lng:e}=this.facet.settings;return 0!==t&&""!==t&&void 0!==t||0!==e&&""!==e&&void 0!==e}shouldCenter(){return!this.hasBounds()&&this.hasMarkers()&&(!this.canCenter()||this.facet.queryString)}getBounds(){const{bounds:t}=this.facet;return!(!t||!t.min_lat)&&[{lat:parseFloat(t.min_lat),lng:parseFloat(t.min_lng)},{lat:parseFloat(t.max_lat),lng:parseFloat(t.max_lng)}]}getLat(){const{map_lat:t}=this.facet.settings;return this.hasBounds()?this.getBoundsLat():parseFloat(t||0)}getLng(){const{map_lng:t}=this.facet.settings;return this.hasBounds()?this.getBoundsLng():parseFloat(t||0)}getSWLat(){const{selected:t}=this.facet;return t&&t[0]?parseFloat(t[0]):-90}getSWLng(){const{selected:t}=this.facet;return t&&t[1]?parseFloat(t[1]):-180}getNELat(){const{selected:t}=this.facet;return t&&t[2]?parseFloat(t[2]):90}getNELng(){const{selected:t}=this.facet;return t&&t[3]?parseFloat(t[3]):180}getBoundsLat(){const t=this.getSWLat();return t+(this.getNELat()-t)/2}getBoundsLng(){const t=this.getSWLng();return t+(this.getNELng()-t)/2}getZoom(){const{map_zoom:t}=this.facet.settings;return this.hasBounds()?parseInt(this.facet.selected[4]):t||2}}const h=()=>{function t(t){t=t||{},this.ready_=!1,void 0===t.visible&&(t.visible=!0),void 0===t.anchor&&(t.anchor=e.BOTTOM),this.setValues(t)}t.prototype=new google.maps.OverlayView,t.prototype.getVisible=function(){return this.get("visible")},t.prototype.setVisible=function(t){this.set("visible",t)},t.prototype.visible_changed=function(){this.ready_&&(this.markerWrapper_.style.display=this.getVisible()?"":"none",this.draw())},t.prototype.getWidth=function(){return this.get("width")},t.prototype.getHeight=function(){return this.get("height")},t.prototype.setZIndex=function(t){this.set("zIndex",t)},t.prototype.getZIndex=function(){return this.get("zIndex")},t.prototype.zIndex_changed=function(){this.getZIndex()&&this.ready_&&(this.markerWrapper_.style.zIndex=this.getZIndex())},t.prototype.getPosition=function(){return this.get("position")},t.prototype.setPosition=function(t){this.set("position",t)},t.prototype.getAnchor=function(){return this.get("anchor")},t.prototype.setAnchor=function(t){this.set("anchor",t)},t.prototype.anchor_changed=function(){this.draw()},t.prototype.removeChildren_=function(t){if(t)for(;t.firstChild;)t.firstChild.remove()},t.prototype.setContent=function(t){this.set("content",t)},t.prototype.getContent=function(){return this.get("content")},t.prototype.addEventListener=function(t,e,s,r){return r=r||!1,t.addEventListener(e,s,r),{target:t,type:e,handler:s,useCapture:r}},t.prototype.removeEventListener=function(t){t.target.removeEventListener(t.type,t.handler,t.useCapture)},t.prototype.content_changed=function(){if(!this.markerContent_)return;this.removeChildren_(this.markerWrapper_);const t=this.getContent();t&&(this.markerWrapper_.appendChild(t),google.maps.event.trigger(this,"domready")),this.ready_&&this.draw()},t.prototype.getOffset_=function(){const t=this.getAnchor();if("object"==typeof t)return t;const s=new google.maps.Size(0,0);if(!this.markerWrapper_)return s;const r=this.markerWrapper_.offsetWidth,i=this.markerWrapper_.offsetHeight;switch(t){case e.TOP_LEFT:break;case e.TOP:s.width=-r/2;break;case e.TOP_RIGHT:s.width=-r;break;case e.LEFT:s.height=-i/2;break;case e.MIDDLE:s.width=-r/2,s.height=-i/2;break;case e.RIGHT:s.width=-r,s.height=-i/2;break;case e.BOTTOM_LEFT:s.height=-i;break;case e.BOTTOM:s.width=-r/2,s.height=-i;break;case e.BOTTOM_RIGHT:s.width=-r,s.height=-i}return s},t.prototype.onAdd=function(){if(this.markerWrapper_||(this.markerWrapper_=document.createElement("DIV"),this.markerWrapper_.className="wpgb-marker",this.markerWrapper_.style.position="absolute",(this.get("hasPopup")||this.get("isCluster"))&&(this.markerWrapper_.tabIndex=0,this.markerWrapper_.role="button")),this.getZIndex()&&(this.markerWrapper_.style.zIndex=this.getZIndex()),this.markerWrapper_.style.display=this.getVisible()?"":"none",!this.markerContent_){const t=this;this.markerContent_=!0,this.addEventListener(this.markerWrapper_,"click",(function(e){google.maps.event.trigger(t,"click",e)})),this.addEventListener(this.markerWrapper_,"mouseover",(function(e){google.maps.event.trigger(t,"mouseover",e)})),this.addEventListener(this.markerWrapper_,"mouseout",(function(e){google.maps.event.trigger(t,"mouseout",e)}))}this.ready_=!0,this.content_changed();const t=this.getPanes();t&&t.overlayMouseTarget.appendChild(this.markerWrapper_),google.maps.event.trigger(this,"ready")},t.prototype.draw=function(){if(!this.ready_)return;const t=this.getProjection();if(!t)return;const e=this.get("position"),s=t.fromLatLngToDivPixel(e),r=this.getOffset_();this.markerWrapper_.style.top=s.y+r.height+"px",this.markerWrapper_.style.left=s.x+r.width+"px";const i=this.markerWrapper_.offsetHeight,o=this.markerWrapper_.offsetWidth;o!==this.get("width")&&this.set("width",o),i!==this.get("height")&&(this.set("height",i),this.set("anchorPoint",new google.maps.Point(0,-1*i)))},t.prototype.onRemove=function(){this.markerWrapper_&&this.markerWrapper_.parentNode&&this.markerWrapper_.parentNode.removeChild(this.markerWrapper_)};const e={TOP_LEFT:1,TOP:2,TOP_RIGHT:3,LEFT:4,MIDDLE:5,RIGHT:6,BOTTOM_LEFT:7,BOTTOM:8,BOTTOM_RIGHT:9};return t};var c=s(43),p=s.n(c);const l=[Int8Array,Uint8Array,Uint8ClampedArray,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array];class d{static from(t){if(!(t instanceof ArrayBuffer))throw new Error("Data must be an instance of ArrayBuffer.");const[e,s]=new Uint8Array(t,0,2);if(219!==e)throw new Error("Data does not appear to be in a KDBush format.");const r=s>>4;if(1!==r)throw new Error(`Got v${r} data when expected v1.`);const i=l[15&s];if(!i)throw new Error("Unrecognized array type.");const[o]=new Uint16Array(t,2,1),[n]=new Uint32Array(t,4,1);return new d(n,o,i,t)}constructor(t,e=64,s=Float64Array,r){if(isNaN(t)||t<0)throw new Error(`Unpexpected numItems value: ${t}.`);this.numItems=+t,this.nodeSize=Math.min(Math.max(+e,2),65535),this.ArrayType=s,this.IndexArrayType=t<65536?Uint16Array:Uint32Array;const i=l.indexOf(this.ArrayType),o=2*t*this.ArrayType.BYTES_PER_ELEMENT,n=t*this.IndexArrayType.BYTES_PER_ELEMENT,a=(8-n%8)%8;if(i<0)throw new Error(`Unexpected typed array class: ${s}.`);r&&r instanceof ArrayBuffer?(this.data=r,this.ids=new this.IndexArrayType(this.data,8,t),this.coords=new this.ArrayType(this.data,8+n+a,2*t),this._pos=2*t,this._finished=!0):(this.data=new ArrayBuffer(8+o+n+a),this.ids=new this.IndexArrayType(this.data,8,t),this.coords=new this.ArrayType(this.data,8+n+a,2*t),this._pos=0,this._finished=!1,new Uint8Array(this.data,0,2).set([219,16+i]),new Uint16Array(this.data,2,1)[0]=e,new Uint32Array(this.data,4,1)[0]=t)}add(t,e){const s=this._pos>>1;return this.ids[s]=s,this.coords[this._pos++]=t,this.coords[this._pos++]=e,s}finish(){const t=this._pos>>1;if(t!==this.numItems)throw new Error(`Added ${t} items when expected ${this.numItems}.`);return m(this.ids,this.coords,this.nodeSize,0,this.numItems-1,0),this._finished=!0,this}range(t,e,s,r){if(!this._finished)throw new Error("Data not yet indexed - call index.finish().");const{ids:i,coords:o,nodeSize:n}=this,a=[0,i.length-1,0],h=[];for(;a.length;){const c=a.pop()||0,p=a.pop()||0,l=a.pop()||0;if(p-l<=n){for(let n=l;n<=p;n++){const a=o[2*n],c=o[2*n+1];a>=t&&a<=s&&c>=e&&c<=r&&h.push(i[n])}continue}const d=l+p>>1,m=o[2*d],u=o[2*d+1];m>=t&&m<=s&&u>=e&&u<=r&&h.push(i[d]),(0===c?t<=m:e<=u)&&(a.push(l),a.push(d-1),a.push(1-c)),(0===c?s>=m:r>=u)&&(a.push(d+1),a.push(p),a.push(1-c))}return h}within(t,e,s){if(!this._finished)throw new Error("Data not yet indexed - call index.finish().");const{ids:r,coords:i,nodeSize:o}=this,n=[0,r.length-1,0],a=[],h=s*s;for(;n.length;){const c=n.pop()||0,p=n.pop()||0,l=n.pop()||0;if(p-l<=o){for(let s=l;s<=p;s++)k(i[2*s],i[2*s+1],t,e)<=h&&a.push(r[s]);continue}const d=l+p>>1,m=i[2*d],u=i[2*d+1];k(m,u,t,e)<=h&&a.push(r[d]),(0===c?t-s<=m:e-s<=u)&&(n.push(l),n.push(d-1),n.push(1-c)),(0===c?t+s>=m:e+s>=u)&&(n.push(d+1),n.push(p),n.push(1-c))}return a}}function m(t,e,s,r,i,o){if(i-r<=s)return;const n=r+i>>1;u(t,e,n,r,i,o),m(t,e,s,r,n-1,1-o),m(t,e,s,n+1,i,1-o)}function u(t,e,s,r,i,o){for(;i>r;){if(i-r>600){const n=i-r+1,a=s-r+1,h=Math.log(n),c=.5*Math.exp(2*h/3),p=.5*Math.sqrt(h*c*(n-c)/n)*(a-n/2<0?-1:1);u(t,e,s,Math.max(r,Math.floor(s-a*c/n+p)),Math.min(i,Math.floor(s+(n-a)*c/n+p)),o)}const n=e[2*s+o];let a=r,h=i;for(g(t,e,r,s),e[2*i+o]>n&&g(t,e,r,i);a<h;){for(g(t,e,a,h),a++,h--;e[2*a+o]<n;)a++;for(;e[2*h+o]>n;)h--}e[2*r+o]===n?g(t,e,r,h):(h++,g(t,e,h,i)),h<=s&&(r=h+1),s<=h&&(i=h-1)}}function g(t,e,s,r){f(t,s,r),f(e,2*s,2*r),f(e,2*s+1,2*r+1)}function f(t,e,s){const r=t[e];t[e]=t[s],t[s]=r}function k(t,e,s,r){const i=t-s,o=e-r;return i*i+o*o}const y={minZoom:0,maxZoom:16,minPoints:2,radius:40,extent:512,nodeSize:64,log:!1,generateId:!1,reduce:null,map:t=>t},_=Math.fround||(w=new Float32Array(1),t=>(w[0]=+t,w[0]));var w;const b=3,v=5,M=6;class x{constructor(t){this.options=Object.assign(Object.create(y),t),this.trees=new Array(this.options.maxZoom+1),this.stride=this.options.reduce?7:6,this.clusterProps=[]}load(t){const{log:e,minZoom:s,maxZoom:r}=this.options;e&&console.time("total time");const i=`prepare ${t.length} points`;e&&console.time(i),this.points=t;const o=[];for(let e=0;e<t.length;e++){const s=t[e];if(!s.geometry)continue;const[r,i]=s.geometry.coordinates,n=_(P(r)),a=_(O(i));o.push(n,a,1/0,e,-1,1),this.options.reduce&&o.push(0)}let n=this.trees[r+1]=this._createTree(o);e&&console.timeEnd(i);for(let t=r;t>=s;t--){const s=+Date.now();n=this.trees[t]=this._createTree(this._cluster(n,t)),e&&console.log("z%d: %d clusters in %dms",t,n.numItems,+Date.now()-s)}return e&&console.timeEnd("total time"),this}getClusters(t,e){let s=((t[0]+180)%360+360)%360-180;const r=Math.max(-90,Math.min(90,t[1]));let i=180===t[2]?180:((t[2]+180)%360+360)%360-180;const o=Math.max(-90,Math.min(90,t[3]));if(t[2]-t[0]>=360)s=-180,i=180;else if(s>i){const t=this.getClusters([s,r,180,o],e),n=this.getClusters([-180,r,i,o],e);return t.concat(n)}const n=this.trees[this._limitZoom(e)],a=n.range(P(s),O(o),P(i),O(r)),h=n.data,c=[];for(const t of a){const e=this.stride*t;c.push(h[e+v]>1?C(h,e,this.clusterProps):this.points[h[e+b]])}return c}getChildren(t){const e=this._getOriginId(t),s=this._getOriginZoom(t),r="No cluster with the specified id.",i=this.trees[s];if(!i)throw new Error(r);const o=i.data;if(e*this.stride>=o.length)throw new Error(r);const n=this.options.radius/(this.options.extent*Math.pow(2,s-1)),a=o[e*this.stride],h=o[e*this.stride+1],c=i.within(a,h,n),p=[];for(const e of c){const s=e*this.stride;o[s+4]===t&&p.push(o[s+v]>1?C(o,s,this.clusterProps):this.points[o[s+b]])}if(0===p.length)throw new Error(r);return p}getLeaves(t,e,s){e=e||10,s=s||0;const r=[];return this._appendLeaves(r,t,e,s,0),r}getTile(t,e,s){const r=this.trees[this._limitZoom(t)],i=Math.pow(2,t),{extent:o,radius:n}=this.options,a=n/o,h=(s-a)/i,c=(s+1+a)/i,p={features:[]};return this._addTileFeatures(r.range((e-a)/i,h,(e+1+a)/i,c),r.data,e,s,i,p),0===e&&this._addTileFeatures(r.range(1-a/i,h,1,c),r.data,i,s,i,p),e===i-1&&this._addTileFeatures(r.range(0,h,a/i,c),r.data,-1,s,i,p),p.features.length?p:null}getClusterExpansionZoom(t){let e=this._getOriginZoom(t)-1;for(;e<=this.options.maxZoom;){const s=this.getChildren(t);if(e++,1!==s.length)break;t=s[0].properties.cluster_id}return e}_appendLeaves(t,e,s,r,i){const o=this.getChildren(e);for(const e of o){const o=e.properties;if(o&&o.cluster?i+o.point_count<=r?i+=o.point_count:i=this._appendLeaves(t,o.cluster_id,s,r,i):i<r?i++:t.push(e),t.length===s)break}return i}_createTree(t){const e=new d(t.length/this.stride|0,this.options.nodeSize,Float32Array);for(let s=0;s<t.length;s+=this.stride)e.add(t[s],t[s+1]);return e.finish(),e.data=t,e}_addTileFeatures(t,e,s,r,i,o){for(const n of t){const t=n*this.stride,a=e[t+v]>1;let h,c,p;if(a)h=E(e,t,this.clusterProps),c=e[t],p=e[t+1];else{const s=this.points[e[t+b]];h=s.properties;const[r,i]=s.geometry.coordinates;c=P(r),p=O(i)}const l={type:1,geometry:[[Math.round(this.options.extent*(c*i-s)),Math.round(this.options.extent*(p*i-r))]],tags:h};let d;d=a||this.options.generateId?e[t+b]:this.points[e[t+b]].id,void 0!==d&&(l.id=d),o.features.push(l)}}_limitZoom(t){return Math.max(this.options.minZoom,Math.min(Math.floor(+t),this.options.maxZoom+1))}_cluster(t,e){const{radius:s,extent:r,reduce:i,minPoints:o}=this.options,n=s/(r*Math.pow(2,e)),a=t.data,h=[],c=this.stride;for(let s=0;s<a.length;s+=c){if(a[s+2]<=e)continue;a[s+2]=e;const r=a[s],p=a[s+1],l=t.within(a[s],a[s+1],n),d=a[s+v];let m=d;for(const t of l){const s=t*c;a[s+2]>e&&(m+=a[s+v])}if(m>d&&m>=o){let t,o=r*d,n=p*d,u=-1;const g=((s/c|0)<<5)+(e+1)+this.points.length;for(const r of l){const h=r*c;if(a[h+2]<=e)continue;a[h+2]=e;const p=a[h+v];o+=a[h]*p,n+=a[h+1]*p,a[h+4]=g,i&&(t||(t=this._map(a,s,!0),u=this.clusterProps.length,this.clusterProps.push(t)),i(t,this._map(a,h)))}a[s+4]=g,h.push(o/m,n/m,1/0,g,-1,m),i&&h.push(u)}else{for(let t=0;t<c;t++)h.push(a[s+t]);if(m>1)for(const t of l){const s=t*c;if(!(a[s+2]<=e)){a[s+2]=e;for(let t=0;t<c;t++)h.push(a[s+t])}}}}return h}_getOriginId(t){return t-this.points.length>>5}_getOriginZoom(t){return(t-this.points.length)%32}_map(t,e,s){if(t[e+v]>1){const r=this.clusterProps[t[e+M]];return s?Object.assign({},r):r}const r=this.points[t[e+b]].properties,i=this.options.map(r);return s&&i===r?Object.assign({},i):i}}function C(t,e,s){return{type:"Feature",id:t[e+b],properties:E(t,e,s),geometry:{type:"Point",coordinates:[(r=t[e],360*(r-.5)),T(t[e+1])]}};var r}function E(t,e,s){const r=t[e+v],i=r>=1e4?`${Math.round(r/1e3)}k`:r>=1e3?Math.round(r/100)/10+"k":r,o=t[e+M],n=-1===o?{}:Object.assign({},s[o]);return Object.assign(n,{cluster:!0,cluster_id:t[e+b],point_count:r,point_count_abbreviated:i})}function P(t){return t/360+.5}function O(t){const e=Math.sin(t*Math.PI/180),s=.5-.25*Math.log((1+e)/(1-e))/Math.PI;return s<0?0:s>1?1:s}function T(t){const e=(180-360*t)*Math.PI/180;return 360*Math.atan(Math.exp(e))/Math.PI-90}function I(t,e){var s={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(s[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(r=Object.getOwnPropertySymbols(t);i<r.length;i++)e.indexOf(r[i])<0&&Object.prototype.propertyIsEnumerable.call(t,r[i])&&(s[r[i]]=t[r[i]])}return s}class S{static isAdvancedMarkerAvailable(t){return google.maps.marker&&!0===t.getMapCapabilities().isAdvancedMarkersAvailable}static isAdvancedMarker(t){return google.maps.marker&&t instanceof google.maps.marker.AdvancedMarkerElement}static setMap(t,e){this.isAdvancedMarker(t)?t.map=e:t.setMap(e)}static getPosition(t){if(this.isAdvancedMarker(t)){if(t.position){if(t.position instanceof google.maps.LatLng)return t.position;if(t.position.lat&&t.position.lng)return new google.maps.LatLng(t.position.lat,t.position.lng)}return new google.maps.LatLng(null)}return t.getPosition()}static getVisible(t){return!!this.isAdvancedMarker(t)||t.getVisible()}}class B{constructor({markers:t,position:e}){this.markers=t,e&&(e instanceof google.maps.LatLng?this._position=e:this._position=new google.maps.LatLng(e))}get bounds(){if(0===this.markers.length&&!this._position)return;const t=new google.maps.LatLngBounds(this._position,this._position);for(const e of this.markers)t.extend(S.getPosition(e));return t}get position(){return this._position||this.bounds.getCenter()}get count(){return this.markers.filter((t=>S.getVisible(t))).length}push(t){this.markers.push(t)}delete(){this.marker&&(S.setMap(this.marker,null),this.marker=void 0),this.markers.length=0}}class W{constructor({maxZoom:t=16}){this.maxZoom=t}noop({markers:t}){return Z(t)}}const Z=t=>{const e=t.map((t=>new B({position:S.getPosition(t),markers:[t]})));return e};class z extends W{constructor(t){var{maxZoom:e,radius:s=60}=t,r=I(t,["maxZoom","radius"]);super({maxZoom:e}),this.state={zoom:-1},this.superCluster=new x(Object.assign({maxZoom:this.maxZoom,radius:s},r))}calculate(t){let e=!1;const s={zoom:t.map.getZoom()};if(!p()(t.markers,this.markers)){e=!0,this.markers=[...t.markers];const s=this.markers.map((t=>{const e=S.getPosition(t);return{type:"Feature",geometry:{type:"Point",coordinates:[e.lng(),e.lat()]},properties:{marker:t}}}));this.superCluster.load(s)}return e||(this.state.zoom<=this.maxZoom||s.zoom<=this.maxZoom)&&(e=!p()(this.state,s)),this.state=s,e&&(this.clusters=this.cluster(t)),{clusters:this.clusters,changed:e}}cluster({map:t}){return this.superCluster.getClusters([-180,-90,180,90],Math.round(t.getZoom())).map((t=>this.transformCluster(t)))}transformCluster({geometry:{coordinates:[t,e]},properties:s}){if(s.cluster)return new B({markers:this.superCluster.getLeaves(s.cluster_id,1/0).map((t=>t.properties.marker)),position:{lat:e,lng:t}});const r=s.marker;return new B({markers:[r],position:S.getPosition(r)})}}class A{constructor(t,e){this.markers={sum:t.length};const s=e.map((t=>t.count)),r=s.reduce(((t,e)=>t+e),0);this.clusters={count:e.length,markers:{mean:r/e.length,sum:r,min:Math.min(...s),max:Math.max(...s)}}}}class N{render({count:t,position:e},s,r){const i=`<svg fill="${t>Math.max(10,s.clusters.markers.mean)?"#ff0000":"#0000ff"}" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 240 240" width="50" height="50">\n<circle cx="120" cy="120" opacity=".6" r="70" />\n<circle cx="120" cy="120" opacity=".3" r="90" />\n<circle cx="120" cy="120" opacity=".2" r="110" />\n<text x="50%" y="50%" style="fill:#fff" text-anchor="middle" font-size="50" dominant-baseline="middle" font-family="roboto,arial,sans-serif">${t}</text>\n</svg>`,o=`Cluster of ${t} markers`,n=Number(google.maps.Marker.MAX_ZINDEX)+t;if(S.isAdvancedMarkerAvailable(r)){const t=(new DOMParser).parseFromString(i,"image/svg+xml").documentElement;t.setAttribute("transform","translate(0 25)");const s={map:r,position:e,zIndex:n,title:o,content:t};return new google.maps.marker.AdvancedMarkerElement(s)}const a={position:e,zIndex:n,title:o,icon:{url:`data:image/svg+xml;base64,${btoa(i)}`,anchor:new google.maps.Point(25,25)}};return new google.maps.Marker(a)}}class F{constructor(){!function(t,e){for(let s in e.prototype)t.prototype[s]=e.prototype[s]}(F,google.maps.OverlayView)}}var j;!function(t){t.CLUSTERING_BEGIN="clusteringbegin",t.CLUSTERING_END="clusteringend",t.CLUSTER_CLICK="click"}(j||(j={}));const R=(t,e,s)=>{s.fitBounds(e.bounds)};class $ extends F{constructor({map:t,markers:e=[],algorithmOptions:s={},algorithm:r=new z(s),renderer:i=new N,onClusterClick:o=R}){super(),this.markers=[...e],this.clusters=[],this.algorithm=r,this.renderer=i,this.onClusterClick=o,t&&this.setMap(t)}addMarker(t,e){this.markers.includes(t)||(this.markers.push(t),e||this.render())}addMarkers(t,e){t.forEach((t=>{this.addMarker(t,!0)})),e||this.render()}removeMarker(t,e){const s=this.markers.indexOf(t);return-1!==s&&(S.setMap(t,null),this.markers.splice(s,1),e||this.render(),!0)}removeMarkers(t,e){let s=!1;return t.forEach((t=>{s=this.removeMarker(t,!0)||s})),s&&!e&&this.render(),s}clearMarkers(t){this.markers.length=0,t||this.render()}render(){const t=this.getMap();if(t instanceof google.maps.Map&&t.getProjection()){google.maps.event.trigger(this,j.CLUSTERING_BEGIN,this);const{clusters:e,changed:s}=this.algorithm.calculate({markers:this.markers,map:t,mapCanvasProjection:this.getProjection()});if(s||null==s){const t=new Set;for(const s of e)1==s.markers.length&&t.add(s.markers[0]);const s=[];for(const e of this.clusters)null!=e.marker&&(1==e.markers.length?t.has(e.marker)||S.setMap(e.marker,null):s.push(e.marker));this.clusters=e,this.renderClusters(),requestAnimationFrame((()=>s.forEach((t=>S.setMap(t,null)))))}google.maps.event.trigger(this,j.CLUSTERING_END,this)}}onAdd(){this.idleListener=this.getMap().addListener("idle",this.render.bind(this)),this.render()}onRemove(){google.maps.event.removeListener(this.idleListener),this.reset()}reset(){this.markers.forEach((t=>S.setMap(t,null))),this.clusters.forEach((t=>t.delete())),this.clusters=[]}renderClusters(){const t=new A(this.markers,this.clusters),e=this.getMap();this.clusters.forEach((s=>{1===s.markers.length?s.marker=s.markers[0]:(s.marker=this.renderer.render(s,t,e),s.markers.forEach((t=>S.setMap(t,null))),this.onClusterClick&&s.marker.addListener("click",(t=>{google.maps.event.trigger(this,j.CLUSTER_CLICK,s),this.onClusterClick(t,s,e)}))),S.setMap(s.marker,e)}))}}let G;const q=function(){const t={},e=()=>null;let s=null;return t.enableLogger=function(){window.console&&null!==s&&(window.console.warn=s)},t.disableLogger=function(){window.console&&window.console.warn!==e&&(s=window.console.warn,window.console.warn=e)},t}();class U extends a{constructor(t){super(),this.facet=t}emit(){}load(){if(G=document.getElementById("wpgb-gmap-script"),G&&G.loaded)this.init();else{if(!G){const{map_api_key:t}=this.facet.settings;G=document.createElement("script"),G.id="wpgb-gmap-script",G.src=`https://maps.googleapis.com/maps/api/js?key=${t}&libraries=places&loading=async&callback=wpgbGmapsLoaded`,document.querySelector("head").appendChild(G)}window.addEventListener("wpgbGmapsLoaded",(()=>this.init())),this.loadMarkerStyles()}}init(){q.disableLogger();const t=this.facet.holder.querySelector(".wpgb-map-facet");this.getOptions(),this.emit("beforeInit"),G.loaded=!0,t.classList.add("wpgb-map-loaded"),this.map=new google.maps.Map(t,this.options),this.popup=new google.maps.InfoWindow({maxWidth:300}),this.marker=h(),this.setLayers(),this.setClusters(),this.addMarkers(),this.setMapPosition(),this.bindEvents(),this.emit("afterInit"),setTimeout(q.enableLogger,500)}getOptions(){const{settings:t}=this.facet,e=["roadmap","satellite","hybrid","terrain"].includes(t?.map_google_style||"roadmap");this.options={center:{lat:this.getLat(),lng:this.getLng()},zoom:this.getZoom(),draggable:t.map_dragging,minZoom:t.map_min_zoom||0,maxZoom:t.map_max_zoom||20,zoomControl:t.map_zoom_ctrl,mapTypeControl:t.map_type_ctrl,scaleControl:t.map_scale_ctrl,streetViewControl:t.map_streetview_ctrl,rotateControl:t.map_rotate_ctrl,fullscreenControl:t.map_fullscreen_ctrl,animatedZoom:!1,clickableIcons:!1},e?this.options.mapTypeId=t.map_google_style||"roadmap":this.options.mapId=t.map_google_style,t.map_scrolling||(this.options.scrollwheel=!1),e&&!t.map_poi&&(this.options.styles=[{featureType:"poi",stylers:[{visibility:"off"}]},{featureType:"transit",stylers:[{visibility:"off"}]}])}setLayers(){const{map_transit:t,map_traffic:e,map_bicycling:s}=this.facet.settings;t&&(new google.maps.TransitLayer).setMap(this.map),e&&(new google.maps.TrafficLayer).setMap(this.map),s&&(new google.maps.BicyclingLayer).setMap(this.map)}setClusters(){const{map_cluster_max_zoom:t,map_marker_cluster:e,map_cluster_radius:s,map_cluster_size:r}=this.facet.settings;this.cluster=new $({map:this.map,markers:[],algorithm:new z({minPoints:e?r||2:999999,maxZoom:t||14,radius:s||50}),renderer:{render:t=>new this.marker({map:this.map,position:t.position,content:this.generateCluster(t.count),isCluster:!0})}})}bindEvents(){window.addEventListener("click",this.onFullScreen.bind(this)),this.popup.addListener("closeclick",(()=>delete this.popup.marker)),this?.facet?.holder?.addEventListener?.("click",(t=>{this.hasMoved||t?.target?.closest?.(".wpgb-marker")||(delete this.popup.marker,this.popup.close())}),!0),this.map.addListener("dragend",(()=>this.hasMoved=!0)),this.map.addListener("zoom_changed",(()=>this.hasMoved=!0)),window.addEventListener("resize",(()=>{this.popup.close(),this.setMapPosition()})),this.map.addListener("idle",(()=>{!this.fitMarkers&&this.hasMoved&&this.emit("refresh"),delete this.hasMoved,delete this.fitMarkers}))}getCoordinates(){const t=this.map.getBounds();return[t.getSouthWest().lat(),t.getSouthWest().lng(),t.getNorthEast().lat(),t.getNorthEast().lng(),this.map.getZoom()]}centerMap(){this.map.panTo({lat:this.getLat(),lng:this.getLng()}),this.map.setZoom(this.getZoom()),delete this.hasMoved}autoCenterMap(){const t=this.getBounds(),e=new google.maps.LatLngBounds;t&&(this.fitMarkers=!0,e.extend(t[0]),e.extend(t[1]),this.map.setCenter(e.getCenter()),this.map.fitBounds(e))}centerMarkers(){this.fitMarkers=!0,this.map.fitBounds(this.bounds)}updateBounds(){this.fitMarkers=!0,this.map.fitBounds({north:this.getNELat(),south:this.getSWLat(),east:this.getNELng(),west:this.getSWLng()}),this.map.setZoom(this.getZoom()),delete this.hasMoved}addMarkers(){this.markers={},this.bounds=new google.maps.LatLngBounds,this.cluster.clearMarkers(),this.cluster.addMarkers(this.facet.geoJSON.features.map(this.addMarker.bind(this)))}addMarker(t){const{settings:e}=this.facet,s=t.properties.id,r=t.geometry.coordinates,i=new google.maps.LatLng(r[1],r[0]),o=new this.marker({position:i,map:this.map,content:this.generateMarker(t?.properties||{}),hasPopup:e.map_marker_content,feature:t});return this.bounds.extend(i),this.markers[s]=o,o.addListener("mouseover",(()=>this.emit("marker.mouseenter",[t]))),o.addListener("mouseout",(()=>this.emit("marker.mouseleave",[t]))),o.addListener("click",(()=>this.emit("marker.click",[t]))),e.map_marker_content&&o.addListener("click",(()=>this.onMarkerClick(o,s))),o}getMarker(t){const e=this.markers?.[t];return e?{element:e?.content?.parentElement,feature:e?.feature||{}}:{}}updateMarkers(){this.popup.close(),this.addMarkers()}onMarkerClick(t,e){this.hasMoved||(this.popup.close(),this.popup.marker!==e?(document.activeElement&&document.activeElement.blur&&document.activeElement.blur(),this.popup.close(),this.popup.setContent('<div class="wpgb-map-marker-loader"></div>'),this.popup.open({map:this.map,anchor:t}),this.popup.marker=e,this.getMarkerContent(e,t.getPosition().lat(),t.getPosition().lng())):delete this.popup.marker)}onFullScreen({target:t}){const e=t&&t.classList.contains("gm-fullscreen-control"),s=e&&t.closest(".wpgb-facet"),r=s&&s===this.facet.holder;e&&s&&r&&(this.hasMoved=!0)}setMarkerContent(t,e){this.popup.marker===e&&this.popup.setContent(t)}drawLocation(){if(!this.map)return;const{lat:t,lng:e,rad:s}=this.location;q.disableLogger(),this.center=new google.maps.Marker({map:this.map,position:{lat:t,lng:e},clickable:!1,icon:{strokeColor:"rgb(255, 255, 255)",fillColor:this.circleColor,strokeWeight:2,fillOpacity:1,path:google.maps.SymbolPath.CIRCLE,scale:7.5,anchor:new google.maps.Point(0,0)}}),this.circle=new google.maps.Circle({map:this.map,center:{lat:t,lng:e},radius:1e3*s,clickable:!1,strokeColor:this.circleColor,fillColor:this.circleColor,strokeOpacity:.8,strokeWeight:2,fillOpacity:.1}),setTimeout(q.enableLogger,64)}removeLocation(){this.circle&&this.circle.setMap(null),this.center&&this.center.setMap(null)}centerLocation(){const t=new google.maps.LatLngBounds;t.extend(this.location.min),t.extend(this.location.max),this.fitMarkers=!0,this.map.fitBounds(t,0)}}const D=document.createElement("script"),H=document.createElement("link");class V extends a{constructor(t){super(),this.facet=t}emit(){}load(){D.loaded?this.init():(D.addEventListener("load",(()=>this.init())),D.src||(H.href="https://api.tiles.mapbox.com/mapbox-gl-js/v2.10.0/mapbox-gl.css",H.rel="stylesheet",D.src="https://api.tiles.mapbox.com/mapbox-gl-js/v3.6.0/mapbox-gl.js",document.querySelector("head").appendChild(H),document.querySelector("head").appendChild(D)),this.loadMarkerStyles())}init(){const t=this.facet.holder.querySelector(".wpgb-map-facet");this.getOptions(t),this.emit("beforeInit"),D.loaded=!0,t.classList.add("wpgb-map-loaded"),mapboxgl.accessToken=this.options.accessToken,this.map=new mapboxgl.Map(this.options),this.popup=new mapboxgl.Popup,this.setControls(),this.addMarkers(),this.bindEvents(),this.setMapPosition(),this.hasBounds()&&this.updateBounds(),this.emit("afterInit")}getOptions(t){const{settings:e}=this.facet;this.options={accessToken:e.map_access_token,container:t,center:{lat:this.getLat(),lng:this.getLng()},scrollZoom:e.map_scrolling,dragPan:e.map_dragging,dragRotate:!1,zoom:this.getZoom(),minZoom:e.map_min_zoom||0,maxZoom:e.map_max_zoom||20,style:e.map_mapbox_style_url||e.map_mapbox_style||"mapbox://styles/mapbox/streets-v11",projection:"mercator"}}setControls(){const{settings:t}=this.facet;t.map_fullscreen_ctrl&&this.map.addControl(new mapboxgl.FullscreenControl),t.map_scale_ctrl&&this.map.addControl(new mapboxgl.ScaleControl),t.map_zoom_ctrl&&this.map.addControl(new mapboxgl.NavigationControl),t.map_geo_ctrl&&this.map.addControl(new mapboxgl.GeolocateControl)}bindEvents(){this.event=()=>{!this.resized&&this.emit("refresh"),delete this.resized,delete this.feature},this.map.on("moveend",this.event),this.resize||(this.map.on("resize",(()=>{this.setMapPosition(),this.resized=!0})),this.resize=!0)}getCoordinates(){const t=this.map.getBounds();return[t.getSouthWest().lat,t.getSouthWest().lng,t.getNorthEast().lat,t.getNorthEast().lng,this.map.getZoom()]}centerMap(){this.map.off("moveend",this.event),this.map.panTo([this.getLng(),this.getLat()],{duration:0}),this.map.setZoom(this.getZoom()),this.bindEvents()}autoCenterMap(){const t=this.getBounds(),e=new mapboxgl.LngLatBounds;t&&(e.extend([t[0].lng,t[0].lat]),e.extend([t[1].lng,t[1].lat]),this.map.off("moveend",this.event),this.map.fitBounds(e,{duration:0,padding:50}),this.bindEvents())}centerMarkers(){const t=new mapboxgl.LngLatBounds;this.facet.geoJSON.features.map((({geometry:e})=>t.extend(e.coordinates))),this.map.off("moveend",this.event),this.map.fitBounds(t,{duration:0,padding:67}),this.bindEvents()}updateBounds(){const t=new mapboxgl.LngLatBounds;t.extend([this.getNELng(),this.getNELat()]),t.extend([this.getSWLng(),this.getSWLat()]),this.map.off("moveend",this.event),this.map.fitBounds(t,{duration:0}),this.bindEvents()}addMarkers(){this.map.on("load",(()=>this.onMapLoad())),this.popup.on("close",(()=>delete this.popup.marker))}getMarker(t){const e=this.markers?.[`marker${t}`];return e?{element:e?.element,feature:e?.feature||{}}:{}}updateMarkers(){const t=this.map.getSource("markers");for(const t in this.markers)this.markers[t]?.marker?.remove?.(),delete this.markers[t];this.popup.isOpen()&&this.popup.remove(),t&&t.setData(this.facet.geoJSON)}onMapLoad(){this.emit("loaded"),this.setMarkers(),this.setCluster()}onMarkerClick(t,e){const{properties:s}=t;if(this.popup.marker===s.id)return delete this.popup.marker,void this.popup.remove();this.popup.isOpen()&&this.popup.remove(),this.popup.setLngLat(t.geometry.coordinates),this.popup.setOffset([0,-1*e.getBoundingClientRect()?.height]),this.popup.setHTML('<div class="wpgb-map-marker-loader"></div>'),this.popup.addTo(this.map),this.popup.marker=s.id,this.getMarkerContent(s.id,t.geometry.coordinates[1],t.geometry.coordinates[0])}onClusterClick(t){this.map.getSource("markers").getClusterExpansionZoom(t.properties.cluster_id,((e,s)=>{e||this.map.easeTo({center:t.geometry.coordinates,zoom:s})}))}setMarkers(){const{settings:t}=this.facet;this.markers={},this.map.addSource("markers",{type:"geojson",data:this.facet.geoJSON,cluster:!!t.map_marker_cluster,clusterMinPoints:t.map_marker_size||2,clusterMaxZoom:t.map_cluster_max_zoom||14,clusterRadius:t.map_cluster_radius||50}),this.map.on("data",(()=>requestAnimationFrame(this.renderMarkers.bind(this)))),this.map.on("moveend",(()=>requestAnimationFrame(this.renderMarkers.bind(this))))}renderMarkers(){if(!this.map.isSourceLoaded("markers"))return;const t=[],e=this.map.querySourceFeatures("markers");for(const s of e){const{properties:e,geometry:r}=s,i=e?.cluster,o=""+(i?`cluster${e.cluster_id}`:`marker${e.id}`);if(i&&!this.markers[o]){const t=this.generateCluster(e.point_count,!0),i=new mapboxgl.Marker(t,{anchor:"bottom"}).setLngLat(r.coordinates).addTo(this.map);i.getElement().addEventListener("click",(()=>this.onClusterClick(s))),this.markers[o]={element:t,marker:i}}if(!i&&!this.markers[o]){const t=JSON.parse(e.icon),i=this.generateMarker({properties:e,icon:t},!0),n=new mapboxgl.Marker(i,{anchor:"bottom"}).setLngLat(r.coordinates).addTo(this.map);n.getElement().addEventListener("mouseenter",(()=>this.emit("marker.mouseenter",[this.feature=s]))),n.getElement().addEventListener("mouseleave",(()=>this.feature&&this.emit("marker.mouseleave",[this.feature]))),n.getElement().addEventListener("click",(t=>{t.stopPropagation(),this.facet.settings.map_marker_content&&this.onMarkerClick(s,i),this.emit("marker.click",[s])})),this.markers[o]={feature:{geometry:s.geometry,properties:{icon:t},type:"Feature"},element:i,marker:n}}t.push(o)}for(const e in this.markers)t.includes(e)||(this.markers[e].marker.remove?.(),delete this.markers[e])}setCluster(){this.map.addLayer({id:"clusters",type:"circle",source:"markers",filter:["has","point_count"],paint:{"circle-radius":0}})}setMarkerContent(t,e){this.popup.marker===e&&this.popup.setHTML(t)}drawLocation(){if(!this.map)return;const{lat:t,lng:e}=this.location,s=this.map.getSource("geolocation"),r={type:"geojson",data:{type:"FeatureCollection",features:[{type:"Feature",geometry:{type:"Point",coordinates:[e,t]},properties:{id:"geolocation"}}]}};return s?(s.setData(r.data),this.locationCenter(),void this.locationCircle()):this.map.isStyleLoaded()?(this.map.addSource("geolocation",r),this.locationCenter(),void this.locationCircle()):void this.map.on("load",(()=>{const t=this.map.getSource("geolocation");t?t.setData(r.data):(this.map.addSource("geolocation",r),this.locationCenter(),this.locationCircle())}))}removeLocation(){this.map&&(this.map.getLayer("circle")&&this.map.removeLayer("circle"),this.map.getLayer("center")&&this.map.removeLayer("center"))}centerLocation(){const t=new mapboxgl.LngLatBounds;t.extend([this.location.min.lng,this.location.min.lat]),t.extend([this.location.max.lng,this.location.max.lat]),this.map.off("moveend",this.event),this.map.fitBounds(t,{duration:0,padding:50}),this.bindEvents()}locationCenter(){this.map.addLayer({id:"center",source:"geolocation",type:"circle",paint:{"circle-radius":7.5,"circle-opacity":1,"circle-color":this.circleColor,"circle-stroke-color":"rgb(255, 255, 255)","circle-stroke-width":2,"circle-stroke-opacity":1}},this.map.getLayer("marker")?"marker":"")}locationCircle(){const t=1e3*this.location.rad/.075/Math.cos(this.location.lat*Math.PI/180);this.map.addLayer({id:"circle",source:"geolocation",type:"circle",paint:{"circle-radius":{stops:[[0,0],[20,t]],base:2},"circle-opacity":.1,"circle-color":this.circleColor,"circle-stroke-color":this.circleColor,"circle-stroke-width":2,"circle-stroke-opacity":.8}},this.map.getLayer("center")?"center":"")}}const J=document.createElement("script"),X=document.createElement("link"),K={};wpgb_settings.mapFacet&&wpgb_settings.mapFacet.vendors.map((({type:t,source:e,version:s})=>K[t]=`${e}?v=${s}`));class Q extends a{constructor(t){super(),this.facet=t}emit(){}load(){J.loaded?this.init():(J.addEventListener("load",(()=>this.init())),J.src||(X.href=K.css,X.rel="stylesheet",J.src=K.js,document.querySelector("head").appendChild(X),document.querySelector("head").appendChild(J)),this.loadMarkerStyles())}init(){const t=this.facet.holder.querySelector(".wpgb-map-facet");this.getOptions(),this.emit("beforeInit"),J.loaded=!0,t&&(t.classList.add("wpgb-map-loaded"),this.map=new L.Map(t,this.options),this.popup=new L.popup({offset:[0,-20],autoPan:!0}),this.extendedMarker=(t,e)=>new(L.Marker.extend({_setPos(t){L.Marker.prototype?._setPos?.call?.(this,t),this._icon.style.transform+=" translate(-50%,-100%)"}}))(t,e),this.setProvider(),this.setClusters(),this.addMarkers(),this.setMapPosition(),this.bindEvents(),this.emit("afterInit"))}getOptions(){const{settings:t}=this.facet;this.options={center:{lat:this.getLat(),lng:this.getLng()},zoom:this.getZoom(),minZoom:t.map_min_zoom||0,maxZoom:t.map_max_zoom||20,zoomControl:t.map_zoom_ctrl,dragging:t.map_dragging,scrollWheelZoom:t.map_scrolling,gestureHandling:t.map_dragging&&t.map_scrolling,debounceMoveend:!0}}setProvider(){const{settings:t}=this.facet,e=t.map_leaflet_style;new L.tileLayer(e.url,e.options).addTo(this.map),e.options.bounds&&this.map.fitBounds(e.options.bounds,{animate:!1});for(const t in e.options)this.map.options[t]=e.options[t];this.map.options.minZoom=Math.max(this.options.minZoom,e.options.minZoom||0),this.map.options.maxZoom=Math.min(this.options.maxZoom,e.options.maxZoom||20)}setClusters(){const{map_cluster_max_zoom:t,map_cluster_radius:e,map_marker_cluster:s}=this.facet.settings;s&&(this.cluster=new L.MarkerClusterGroup({animate:!1,spiderfyOnMaxZoom:!0,showCoverageOnHover:!1,zoomToBoundsOnClick:!0,disableClusteringAtZoom:t||14,maxClusterRadius:e||50,iconCreateFunction:t=>{const e=t.getChildCount();return L.divIcon({html:this.generateCluster(e),className:"wpgb-marker",iconSize:null})}}))}bindEvents(){const{settings:t}=this.facet;this.event||(this.event=()=>{!this.autoPan&&!this.resized&&this.emit("refresh"),delete this.resized},this.layer.on("mouseover",(({layer:t})=>this.emit("marker.mouseenter",[t.feature]))),this.layer.on("mouseout",(({layer:t})=>this.emit("marker.mouseleave",[t.feature]))),this.layer.on("click",(({layer:t})=>this.emit("marker.click",[t.feature]))),t.map_marker_content&&this.layer.on("click",(({layer:t})=>{const{properties:e,geometry:s}=t.feature,{coordinates:r}=s;if(this.popup.marker===e.id)return delete this.autoPan,void delete this.popup.marker;this.autoPan=!0,this.popup.marker=e.id,this.popup.setLatLng([r[1],r[0]]),this.popup.options.offset=[0,-1*t.getElement().getBoundingClientRect()?.height],this.popup.setContent('<div class="wpgb-map-marker-loader"></div>'),this.popup.openOn(this.map),this.getMarkerContent(e.id,r[1],r[0])})),this.map.on("popupclose",(()=>{document.activeElement&&document.activeElement.classList.contains("leaflet-marker-icon")||delete this.popup.marker,delete this.autoPan})),this.map.on("movestart",(()=>delete this.resized)),this.map.on("resize",(()=>{this.setMapPosition(),this.resized=!0}))),delete this.popup.marker,delete this.resized,this.map.closePopup(),this.map.on("moveend",this.event)}getCoordinates(){const t=this.map.getBounds();return[t._southWest.lat,t._southWest.lng,t._northEast.lat,t._northEast.lng,this.map.getZoom()]}centerMap(){this.event&&this.map.off("moveend",this.event),this.map.setView(L.latLng(this.getLat(),this.getLng()),this.getZoom(),{animate:!1,duration:0}),this.bindEvents()}autoCenterMap(){const t=this.getBounds(),e=new L.LatLngBounds;t&&(e.extend([t[0].lat,t[0].lng]),e.extend([t[1].lat,t[1].lng]),this.event&&this.map.off("moveend",this.event),this.map.fitBounds(e,{animate:!1,duration:0}),this.bindEvents())}centerMarkers(){this.event&&this.map.off("moveend",this.event),this.map.fitBounds(this.layer.getBounds(),{animate:!1,duration:0}),this.bindEvents()}updateBounds(){const t=L.latLngBounds(L.latLng(this.getSWLat(),this.getSWLng()),L.latLng(this.getNELat(),this.getNELng()));this.event&&this.map.off("moveend",this.event),this.map.fitBounds(t,{animate:!1,duration:0}),this.bindEvents()}updateMarkers(){this.markers={},this.map.closePopup(),this.layer.clearLayers(),this.layer.addData(this.facet.geoJSON),this.cluster?(this.cluster.clearLayers(),this.cluster.addLayer(this.layer),this.map.addLayer(this.cluster)):this.layer.addTo(this.map)}addMarkers(){this.markers={},this.layer=new L.geoJSON(this.facet.geoJSON,{pointToLayer:this.setIcon.bind(this)}),this.cluster?(this.cluster.addLayer(this.layer),this.cluster.addTo(this.map)):this.layer.addTo(this.map)}getMarker(t){const e=this.markers?.[t];return e?{element:e?.getElement?.(),feature:e?.feature||{}}:{}}setIcon(t,e){return t?.properties?.id?this.markers[t.properties.id]=this.extendedMarker(e,{icon:new L.DivIcon({html:this.generateMarker(t?.properties||{}),className:"wpgb-marker",iconSize:null})}):null}setMarkerContent(t,e){if(this.popupAutoPanTimout&&clearTimeout(this.popupAutoPanTimout),!this.popup.marker)return this.map.closePopup(),void delete this.autoPan;this.popup.marker===e&&this.popup.setContent(t),this.popupAutoPanTimout=setTimeout((()=>delete this.autoPan),300),this.popup.update()}drawLocation(){if(!this.map)return;const{lat:t,lng:e,rad:s}=this.location;this.circle=L.circle([t,e],{interactive:!1,color:this.circleColor,fillColor:this.circleColor,opacity:.8,weight:2,fillOpacity:.1,radius:1e3*s}).addTo(this.map),this.center=L.circleMarker([t,e],{interactive:!1,color:"rgb(255, 255, 255)",fillColor:this.circleColor,weight:2,fillOpacity:1,radius:7.5}).addTo(this.map)}removeLocation(){this.map&&(this.circle&&this.map.removeLayer(this.circle),this.center&&this.map.removeLayer(this.center))}centerLocation(){const t=new L.LatLngBounds;t.extend(this.location.min),t.extend(this.location.max),this.event&&this.map.off("moveend",this.event),this.map.fitBounds(t,{animate:!1,duration:0,padding:[0,0]}),this.bindEvents()}}const Y="undefined"!=typeof Map&&new Map;function tt(t,e,s){const{target:r}=t,{instance:i}=e,o=function(t){if(!t||!t.classList.contains("wpgb-card"))return!1;const e=t.className.match(/wpgb-(post|user|term)-\d+/g);return e&&parseInt(e[0].replace(/wpgb-post-|wpgb-user-|wpgb-term-/g,""),10)}(r);if(!o)return;const n=i.getMarker(o);if(!n?.element||!n?.feature)return;const{element:a,feature:h}=n,c=a?.querySelector?.(".wpgb-marker-icon");Object.prototype.hasOwnProperty.call(a,"wpgbIndex")||(a.wpgbIndex=a.style.zIndex||""),a.classList?.[s?"add":"remove"]?.("wpgb-marker-highlighted"),a.style.zIndex=s?9999:a.wpgbIndex,c&&h?.properties?.icon?.url&&h?.properties?.icon?.urlHover&&(c.src=s?h.properties.icon.urlHover:h.properties.icon.url)}const et=t=>{t.facets.on("render",(function(){if("map"!==this.facet.type)return;if(this.facet.queryString=!!Object.keys(t.facets.getParams()).length,this.facet.lang=t.options.lang,!this.facet.html)return this.facet.instance=!1,void(this.facet.rendered=!1);const e=this.facet.holder.querySelector(".wpgb-map-facet");if(e&&(this.facet.disabled?e.setAttribute("aria-disabled",!0):e.removeAttribute("aria-disabled")),this.facet.instance){const t=this.facet,e=setInterval((()=>st(t,e)),50);st(t,e)}else{!function(t,e){Y.has(e.holder)&&(t.element.removeEventListener("mouseenter",Y.get(e.holder).mouseenter,!0),t.element.removeEventListener("mouseleave",Y.get(e.holder).mouseleave,!0),Y.delete(e.holder)),Y.set(e.holder,{mouseenter:t=>tt(t,e,!0),mouseleave:t=>tt(t,e,!1)}),t.element.addEventListener("mouseenter",Y.get(e.holder).mouseenter,!0),t.element.addEventListener("mouseleave",Y.get(e.holder).mouseleave,!0)}(t,this.facet),function(t){const e=t.holder.querySelector('.wpgb-map-pan-to-search input[type="checkbox"]');e&&e.addEventListener("change",(t=>t.stopPropagation()))}(this.facet),this.facet.instance=function(t){const{map_type:e}=t.settings;if("google"===e)return new U(t);if("leaflet"===e)return new Q(t);return new V(t)}(this.facet),this.facet.instance.type=this.facet.settings.map_type,this.facet.instance.emit=function(e,s){"refresh"===e?rt.bind(this)(t):t.facets.emit(`map.${e}`,[this.facet.instance,...s||[]])},this.facet.instance.load();const e=this.facet;t.facets.on("geolocate",((...t)=>e.instance&&e.instance.setLocation(...t)))}this.facet.rendered=!0}))};function st(t,e){t.instance.map&&(delete t.instance.popup.marker,t.instance.facet=t,t.instance.updateMarkers(),t.instance.refreshPosition(),delete t.instance.hasRefreshed,clearInterval(e))}function rt(t){const e=this.getCoordinates();(function(t){const e=t.holder.querySelector('.wpgb-map-pan-to-search input[type="checkbox"]');return e&&e.checked||!e&&t.settings&&t.settings.map_filtering})(this.facet)&&t&&t.facets&&(t.facets.emit("change",[this.facet.slug,e]),t.facets.setParams(this.facet.slug,e),t.facets.refresh(),this.hasRefreshed=!0)}function it(t){et(t)}function ot(){if(WP_Grid_Builder.on("init",it),WP_Grid_Builder.instances)for(const t in WP_Grid_Builder.instances){const e=WP_Grid_Builder.instances[t];e.element.classList.contains("wpgb-enabled")&&it(e)}}window.WP_Grid_Builder?ot():window.addEventListener("wpgb.loaded",(()=>ot())),window.wpgbGmapsLoaded=()=>{let t;"function"==typeof Event?t=new Event("wpgbGmapsLoaded"):document.createEvent&&(t=document.createEvent("Event"),t.initEvent("wpgbGmapsLoaded")),window.dispatchEvent(t)}})()})();