const ContactMap=function(t){this.params=t||{},this.vars={point:{}},this.googleVars={key:!1,loaded:!1,map:!1,markerInfo:!1,marker:!1,callbackFnName:"app_shop.fn.contactMap.initGoogleMap"},this.leafletVars={url:"https://nominatim.openstreetmap.org/search?format=json&q=",loaded:!1,map:!1,marker:!1},this.buildMarkerInfo=t=>{const{name:e,street:a,zipcode:s,city:o}=t||{};return`<div class="contact_marker">\n        <strong class="contact_marker__name">${e}</strong>\n        <div class="contact_marker__address">\n          <span class="contact_marker__street">${a}</span>\n          <span class="contact_marker__city">${s}&nbsp;${o}</span>\n        </div>\n      </div>`},this.getLeafletPlace=t=>{if(!t)return!1;return t.find((t=>t.lat&&t.lon))},this.getLeafletLocation=t=>!!t&&{lat:t.lat,lng:t.lon},this.findAddress=async t=>{if(this.googleVars.key){const e=await this.googleVars.geocoder.geocode({address:t}),{results:[{geometry:{location:a}={},formatted_address:s}={}]=[]}=e||{};return!!a&&{lat:a?.lat(),lng:a?.lng(),name:s}}const e=await fetch(`https://nominatim.openstreetmap.org/search?format=json&q=${encodeURIComponent(t)}`),a=await e.json(),s=this.getLeafletPlace(a);return this.getLeafletLocation(s)},this.centerGoogleMarker=t=>{this.googleVars.map.setOptions({center:t.position,zoom:15}),this.googleVars.markerInfo.setContent(this.buildMarkerInfo(this.vars.point)),this.googleVars.markerInfo.open(this.googleVars.map,t)},this.centerLeafletMarker=t=>{t&&(this.leafletVars.map.setView(t.getLatLng(),15),t.openPopup())},this.centerMarker=()=>{this.googleVars.key?this.centerGoogleMarker(this.googleVars.marker):this.centerLeafletMarker(this.leafletVars.marker)},this.getAddress=t=>{const{street:e,zipcode:a,city:s,country:o}=t||{};return`${e},${this.googleVars.key?` ${a}`:""} ${s}, ${o}`},this.updateGoogleMarker=async()=>{let{lat:t,lng:e}=this.vars.point;const a=this.getAddress(this.vars.point);if(!t||!e){const s=await this.findAddress(a);t=s.lat,e=s.lng,this.vars.point.lat=t,this.vars.point.lng=e}this.googleVars.marker=new google.maps.marker.AdvancedMarkerElement({position:{lat:+t,lng:+e}}),this.googleVars.marker.addListener("click",(()=>{this.googleVars.markerInfo.setContent(this.buildMarkerInfo(this.vars.point)),this.googleVars.markerInfo.open(this.googleVars.map,this.googleVars.marker)})),this.googleVars.marker.setMap(this.googleVars.map),this.centerGoogleMarker(this.googleVars.marker),this.removeLoading()},this.initGoogleMap=()=>{this.googleVars.geocoder=new google.maps.Geocoder,this.googleVars.map=new google.maps.Map(document.getElementById("contact_map"),{center:{lat:52.237049,lng:21.017532},zoom:10,fullscreenControl:!1,mapTypeControl:!1,streetViewControl:!1,mapId:"DEMO_MAP_ID"}),this.googleVars.markerInfo=new google.maps.InfoWindow({disableAutoPan:!0,content:""}),this.googleVars.loaded=!0,this.updateGoogleMarker()},this.updateLeafletMarker=async()=>{let{lat:t,lng:e}=this.vars.point;const a=this.getAddress(this.vars.point);if(!t||!e){const s=await this.findAddress(a);t=s.lat,e=s.lng,this.vars.point.lat=t,this.vars.point.lng=e}this.leafletVars.marker=new L.Marker([+t,+e]),this.leafletVars.marker.bindPopup(this.buildMarkerInfo(this.vars.point)),this.leafletVars.map.addLayer(this.leafletVars.marker),this.centerLeafletMarker(this.leafletVars.marker),this.removeLoading()},this.initLeafletMap=()=>{this.leafletVars.map=L.map("contact_map",{center:{lat:52,lng:19.5},zoom:13,zoomControl:!1}),L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",{maxZoom:18,minZoom:3,attribution:'&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'}).addTo(this.leafletVars.map),this.leafletVars.loaded=!0,this.updateLeafletMarker()},this.initMap=()=>{if(document.getElementById("contact_map").classList.add("--active"),this.googleVars.key){const t=document.createElement("script");return t.src=`https://maps.googleapis.com/maps/api/js?key=${this.googleVars.key}&loading=async&libraries=places,marker&callback=${this.googleVars.callbackFnName}`,t.async=!0,void document.body.appendChild(t)}this.initLeafletMap()},this.getPoint=()=>{const t=document.getElementById("contact_map");t&&Object.entries(t.dataset).forEach((([t,e])=>{this.vars.point[t]=e}))},this.addLoading=()=>{const t=document.getElementById("contact_map");t?.classList.add("--loading")},this.removeLoading=()=>{const t=document.getElementById("contact_map");t?.classList.remove("--loading")},this.init=t=>{this.addLoading(),this.vars.initOptions=t||{},this.getPoint(),this.vars.point.name?this.initMap():this.removeLoading()}};app_shop.run((function(){app_shop.fn.contactMap=new ContactMap,app_shop.fn.contactMap.googleVars.key=window.google_api_key,app_shop.fn.contactMap.init()}),"all","#contact_map");