var stocks_list_more_txt="więcej informacji",stocks_list_not_found="Nie znaleziono";const StocksSite=function(t){this.params=t||{},this.vars={stocks:[]},this.googleVars={key:!1,loaded:!1,map:!1,markerClusters:!1,markerInfo:!1,callbackFnName:"app_shop.fn.stocksSite.initGoogleMap"},this.leafletVars={url:"https://nominatim.openstreetmap.org/search?format=json&q=",loaded:!1,map:!1,markerClusters:!1},this.elementInViewport=t=>{if(!t)return!1;const e=t.getBoundingClientRect(),{top:s,left:o,right:a,bottom:r}=e,{innerWidth:n,innerHeight:i}=window,{clientWidth:l,clientHeight:c}=document.documentElement;return s>=0&&o>=0&&a<=(n||l)&&r<=(i||c)},this.scrollToElement=(t={})=>{const{element:e,additionalYOffset:s=(app_shop.vars.view<3?50:0),callback:o}=t;if(!e)return;if(this.elementInViewport(e))return void o?.();const a=e.getBoundingClientRect(),r=document.querySelector(".--fixed-header header"),n=r?r.offsetHeight:0,{top:i}=a,l=i+window.pageYOffset-60-n-(s||0),c=l.toFixed(),d=()=>{window.pageYOffset.toFixed()===c&&(window.removeEventListener("scroll",d),o?.())};window.addEventListener("scroll",d),d(),window.scrollTo({top:l,behavior:"smooth"})},this.buildMarkerInfo=t=>{const{name:e,street:s,zipcode:o,city:a,link:r}=t||{};return`<div class="stocks_marker">\n        <strong class="stocks_marker__name">${e}</strong>\n        <div class="stocks_marker__address">\n          <span class="stocks_marker__street">${s}</span>\n          <span class="stocks_marker__city">${o}&nbsp;${a}</span>\n        </div>\n        <a href="${r}" target="_blank" class="stocks_marker__link">${stocks_list_more_txt}</a>\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:s}={},formatted_address:o}={}]=[]}=e||{};return!!s&&{lat:s?.lat(),lng:s?.lng(),name:o}}const e=await fetch(`https://nominatim.openstreetmap.org/search?format=json&q=${encodeURIComponent(t)}`),s=await e.json(),o=this.getLeafletPlace(s);return this.getLeafletLocation(o)},this.findGoogleMarker=t=>this.googleVars.markerClusters.markers.find((e=>e.position.lat===+t.lat&&e.position.lng===+t.lng)),this.findLeafletMarker=t=>this.leafletVars.markerClusters.getLayers().find((e=>e._latlng.lat===+t.lat&&e._latlng.lng===+t.lng)),this.findMarker=t=>this.googleVars.key?this.findGoogleMarker(t):this.findLeafletMarker(t),this.centerGoogleMarker=(t,e)=>{this.googleVars.map.setOptions({center:t.position,zoom:15}),this.googleVars.markerInfo.setContent(this.buildMarkerInfo(e)),this.googleVars.markerInfo.open(this.googleVars.map,t)},this.centerLeafletMarker=t=>{t&&(this.leafletVars.map.setView(t.getLatLng(),15),t.openPopup())},this.centerMarker=(t,e)=>{this.googleVars.key?this.centerGoogleMarker(t,e):this.centerLeafletMarker(t)},this.getAddress=t=>{const{street:e,zipcode:s,city:o,country:a}=t||{};return`${e},${this.googleVars.key?` ${s}`:""} ${o}, ${a}`},this.centerGoogleMap=t=>{if(!t.length)return;const e=new google.maps.LatLngBounds;t.forEach((t=>e.extend(t.position))),this.googleVars.map.fitBounds(e)},this.updateGoogleMarkers=async()=>{const t=this.vars.stocks.map((async t=>{let{lat:e,lng:s}=t;const o=this.getAddress(t);if(!e||!s){const a=await this.findAddress(o);if(!a)return!1;e=a.lat,s=a.lng,t.lat=e,t.lng=s}const a=new google.maps.marker.AdvancedMarkerElement({position:{lat:+e,lng:+s}});return a.addListener("click",(()=>{this.googleVars.markerInfo.setContent(this.buildMarkerInfo(t)),this.googleVars.markerInfo.open(this.googleVars.map,a)})),a})),e=(await Promise.all(t)).filter(Boolean);this.googleVars.markerClusters.addMarkers(e),this.centerGoogleMap(e),this.removeLoading()},this.initGoogleMap=()=>{this.googleVars.geocoder=new google.maps.Geocoder,this.googleVars.map=new google.maps.Map(document.getElementById("stocks_map"),{center:{lat:52.237049,lng:21.017532},zoom:10,fullscreenControl:!1,mapTypeControl:!1,streetViewControl:!1,mapId:"DEMO_MAP_ID"}),this.googleVars.markerClusters=new markerClusterer.MarkerClusterer({map:this.googleVars.map}),this.googleVars.markerInfo=new google.maps.InfoWindow({disableAutoPan:!0,content:""}),this.googleVars.loaded=!0,this.updateGoogleMarkers()},this.centerLeafletMap=t=>{if(!t.length)return;const e=L.latLngBounds();t.forEach((t=>e.extend(t.getLatLng()))),this.leafletVars.map.fitBounds(e)},this.updateLeafletMarkers=async()=>{const t=this.vars.stocks.map((async t=>{let{lat:e,lng:s}=t;const o=this.getAddress(t);if(!e||!s){const a=await this.findAddress(o);if(!a)return!1;e=a.lat,s=a.lng,t.lat=e,t.lng=s}const a=new L.Marker([+e,+s]);return a.bindPopup(this.buildMarkerInfo(t)),this.leafletVars.markerClusters.addLayer(a),a})),e=(await Promise.all(t)).filter(Boolean);this.leafletVars.map.addLayer(this.leafletVars.markerClusters),this.centerLeafletMap(e),this.removeLoading()},this.initLeafletMap=()=>{this.leafletVars.map=L.map("stocks_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.leafletVars.markerClusters=new L.MarkerClusterGroup({maxClusterRadius:50}),this.updateLeafletMarkers()},this.initMap=()=>{if(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.getStocks=()=>{this.vars.stocks=[...document.querySelectorAll(".stocks_list__item")].map((t=>{const e={};return Object.entries(t.dataset).forEach((([t,s])=>{e[t]=s})),e}))},this.showStock=t=>{const e=this.vars.stocks.find((e=>e.id===t));if(!e)return void Alertek.Start(`${stocks_list_not_found}`);const s=this.findMarker(e);if(!s)return void Alertek.Start(`${stocks_list_not_found} ${this.getAddress(e)}`);this.centerMarker(s,e);const o=document.getElementById("stocks_map");o&&this.scrollToElement({element:o})},this.showOnMap=t=>{t.preventDefault();const{target:e}=t,s=e.closest(".stocks_list__item");if(!s)return!1;const o=s.dataset.id;return this.showStock(o),!1},this.initEvents=()=>{document.querySelectorAll(".stocks_list__show_on_map").forEach((t=>{t.addEventListener("click",this.showOnMap)}))},this.addLoading=()=>{const t=document.getElementById("stocks_map");t?.classList.add("--loading")},this.removeLoading=()=>{const t=document.getElementById("stocks_map");t?.classList.remove("--loading")},this.init=t=>{this.addLoading(),this.vars.initOptions=t||{},this.getStocks(),this.vars.stocks.length?(this.initMap(),this.initEvents()):this.removeLoading()}};app_shop.fn.stocksSite=new StocksSite,app_shop.run((function(){app_shop.fn.stocksSite.googleVars.key=window.google_api_key,app_shop.fn.stocksSite.init()}),"all","#stocks_map");