/**
 * 2007-2025 PrestaShop
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to http://www.prestashop.com for more information.
 *
 * @author    Boxtal
 * @copyright 2007-2025 PrestaShop SA / 2018-2025 Boxtal
 * @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 * International Registered Trademark & Property of PrestaShop SA
 */var pluginParcelPoint=pluginParcelPoint||{};pluginParcelPoint["bx"]=pluginParcelPoint["bx"]?pluginParcelPoint["bx"]:{},pluginParcelPoint["bx"]={branding:"bx",trigger:".bx-select-parcel",initialized:!1,mapContainer:null,map:null,markers:[],carrierTextCache:{},getData:function(){let e=null;return pluginData&&pluginData["bx"]&&(e=pluginData["bx"]),e},init:function(){const e=this;e.initialized||(e.initialized=!0,e.initCarriers(),e.on("body","click",e.trigger,(function(){e.initMap((function(){e.openMap(),e.getPoints()}))})),window.MutationObserver?(e.observeDomMutations(e.getAllCarrierInputsSelector(),(function(){e.initCarriers()})),e.on("body","click",e.getAllCarrierInputsSelector(),(function(){e.initCarriers()}))):e.on("body","change",e.getAllCarrierInputsSelector(),(function(){e.initCarriers()})),e.on("body","click","."+this.branding+"-parcel-point-button",(function(t){e.selectPoint(this.getAttribute("data-code"),unescape(this.getAttribute("data-name")),this.getAttribute("data-network"),unescape(this.getAttribute("data-street")),unescape(this.getAttribute("data-zipcode")),unescape(this.getAttribute("data-city")),unescape(this.getAttribute("data-country")),unescape(this.getAttribute("data-openinghours")),unescape(this.getAttribute("data-distance")),(function(){e.initCarriers(),e.closeMap()}),(function(t){e.showError(t)}))})))},initMapContainer:function(e){const t=this,n=document.createElement("div"),r=t.getData();n.setAttribute("class",this.branding+"-close"),n.setAttribute("title",r.translation.text.closeMap),n.addEventListener("click",(function(){t.closeMap()}));const o=document.createElement("div");o.setAttribute("id",this.branding+"-map-canvas");const i=document.createElement("div");i.setAttribute("id",this.branding+"-map-container"),i.appendChild(o);const a=document.createElement("div");a.setAttribute("id",this.branding+"-pp-container");const c=document.createElement("div");c.setAttribute("id",this.branding+"-map-inner"),c.appendChild(n),c.appendChild(i),c.appendChild(a),t.mapContainer=document.createElement("div"),t.mapContainer.setAttribute("id",this.branding+"-map"),t.mapContainer.appendChild(c),document.body.appendChild(t.mapContainer),t.map=new maplibregl.Map({container:this.branding+"-map-canvas",style:e,zoom:14}),t.map.addControl(new maplibregl.NavigationControl);const s=document.createElement("img");s.setAttribute("src",r.mapLogoImageUrl);const l=document.createElement("a");l.setAttribute("href",r.mapLogoHrefUrl),l.setAttribute("target","_blank"),l.appendChild(s);const d=document.createElement("div");d.setAttribute("id",this.branding+"-logo"),d.appendChild(l);const p=document.querySelector(".maplibre-ctrl-top-left");p&&p.appendChild(d)},initMap:function(e){const t=this;null===t.mapContainer?t.getMapUrl((function(n){t.initMapContainer(n),e()}),(function(e){t.showError(e)})):e()},initCarriers:function(){const e=this,t=e.getCarriers(),n=e.getSelectedCarrier();for(let r=0;r<t.length;r++){const o=t[r];if(null===o.querySelector("."+this.branding+"-extra-content")){const e=document.createElement("div");e.setAttribute("class","col-sm-12 "+this.branding+"-extra-content"),o.appendChild(e)}const i=o.querySelector("."+this.branding+"-extra-content");n===e.getCarrierId(o)?e.getSelectedCarrierText(n,i):i.innerHTML=""}},getCarriers:function(){let e=document.querySelectorAll(".delivery-option");return 0===e.length&&(e=document.querySelectorAll("td.delivery_option_radio")),0===e.length&&(e=document.querySelectorAll(".delivery_option")),e},openMap:function(){this.mapContainer.classList.add(this.branding+"-modal-show");let e=window.pageYOffset+(window.innerHeight-this.mapContainer.offsetHeight)/2;e<window.pageYOffset&&(e=window.pageYOffset),this.mapContainer.style.top=e+"px",this.map.resize()},closeMap:function(){this.mapContainer.classList.remove(this.branding+"-modal-show"),this.clearMarkers()},getPoints:function(){const e=this;e.getParcelPoints((function(t){e.addParcelPointMarkers(t.nearbyParcelPoints),e.fillParcelPointPanel(t.nearbyParcelPoints),e.addRecipientMarker(t.searchLocation),e.setMapBounds()}),(function(t){e.showError(t)}))},getParcelPoints:function(e,t){const n=this.getSelectedCarrier(),r=this.getData();n||t(r.translation.error.carrierNotFound);const o=new XMLHttpRequest;o.onreadystatechange=function(){if(4===o.readyState)if(200!==o.status)t();else{const t="object"==typeof o.response&&null!==o.response?o.response:JSON.parse(o.response);e(t)}},o.open("POST",r.ajaxUrl),o.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),o.responseType="json",o.send("route=getPoints&carrier="+encodeURIComponent(n)+"&cartId="+encodeURIComponent(r.cartId)+"&token="+encodeURIComponent(r.token))},getMapUrl:function(e,t){const n=this.getSelectedCarrier(),r=this.getData();n||t(r.translation.error.carrierNotFound);const o=new XMLHttpRequest;o.onreadystatechange=function(){if(4===o.readyState)if(200!==o.status)t();else{const t="object"==typeof o.response&&null!==o.response?o.response:JSON.parse(o.response);e(t.mapUrl)}},o.open("POST",r.ajaxUrl),o.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),o.responseType="json",o.send("route=getMapUrl&token="+encodeURIComponent(r.token))},addParcelPointMarkers:function(e){for(let t=0;t<e.length;t++)e[t].index=t,this.addParcelPointMarker(e[t])},generateParcelPointTagData:function(e){return' data-code="'+e.parcelPoint.code+'" data-name="'+escape(e.parcelPoint.name)+'" data-network="'+e.parcelPoint.network+'" data-zipcode="'+escape(e.parcelPoint.location.zipCode)+'" data-country="'+escape(e.parcelPoint.location.country)+'" data-city="'+escape(e.parcelPoint.location.city)+'" data-street="'+escape(e.parcelPoint.location.street)+'" data-openinghours="'+escape(JSON.stringify(e.parcelPoint.openingDays))+'" data-distance="'+escape(JSON.stringify(e.distanceFromSearchLocation))+'" '},formatOpeningDays:function(e){var t=[];const n=this.getData();for(var r=0;r<e.length;r++){var o=e[r];if(o.weekday){for(var i=o.weekday[0]+" ",a=o.openingPeriods,c=[],s=0;s<a.length;s++){var l=a[s],d=l.openingTime===undefined?"":l.openingTime,p=l.closingTime===undefined?"":l.closingTime;""!==d&&""!==p&&c.push(d+"-"+p)}for(;c.length<2;)c.push(n.translation.text.closedLabel);i+=c.join(" "),r%2==1&&(i='<span style="background-color: #d8d8d8;">'+i+"</span>"),t.push(i)}}return t.join("\n")},addParcelPointMarker:function(e){const t=this,n=t.getData();let r=null;"number"==typeof e.distanceFromSearchLocation&&(r=Math.round(e.distanceFromSearchLocation/100)/10);let o="<div class='"+this.branding+"-marker-popup'><b>"+e.parcelPoint.name+"</b><br/>"+e.parcelPoint.location.street+"<br/>"+e.parcelPoint.location.zipCode+" "+e.parcelPoint.location.city+(null===r?"":" ("+n.translation.distance.replace("%s",r)+")")+'<br/><a href="#" class="'+this.branding+'-parcel-point-button" '+this.generateParcelPointTagData(e)+"><b>"+n.translation.text.chooseParcelPoint+'</b></a><pre class="'+this.branding+'-parcel-point-schedule">'+this.formatOpeningDays(e.parcelPoint.openingDays)+"</pre>";const i=this.getMarkerHtmlElement(e.index+1),a=new maplibregl.Popup({offset:25}).setHTML(o),c=new maplibregl.Marker({element:i,anchor:"bottom"}).setLngLat(new maplibregl.LngLat(parseFloat(e.parcelPoint.location.position.longitude),parseFloat(e.parcelPoint.location.position.latitude))).setPopup(a).addTo(t.map);t.markers.push(c),t.addRightColMarkerEvent(c,e.parcelPoint.code)},addRightColMarkerEvent:function(e,t){this.on("body","click","."+this.branding+"-show-info-"+t,(function(){e.togglePopup()}))},formatHours:function(e){const t=e.split(":");return 3===t.length&&(e=t[0]+":"+t[1]),e},addRecipientMarker:function(e){const t=document.createElement("div");t.className=this.branding+"-marker-recipient";const n=new maplibregl.Marker({element:t,anchor:"bottom"}).setLngLat(new maplibregl.LngLat(parseFloat(e.position.longitude),parseFloat(e.position.latitude))).addTo(this.map);this.markers.push(n)},setMapBounds:function(){let e=new maplibregl.LngLatBounds;for(let t=0;t<this.markers.length;t++){const n=this.markers[t];e=e.extend(n.getLngLat())}this.map.fitBounds(e,{padding:30,linear:!0})},fillParcelPointPanel:function(e){const t=this.getData();let n="";n+="<table><tbody>";for(let r=0;r<e.length;r++){const o=e[r];let i=null;"number"==typeof o.distanceFromSearchLocation&&(i=Math.round(o.distanceFromSearchLocation/100)/10),n+="<tr>",n+="<td>"+this.getMarkerHtmlElement(r+1).outerHTML,n+='<div class="'+this.branding+'-parcel-point-title"><a class="'+this.branding+"-show-info-"+o.parcelPoint.code+'">'+o.parcelPoint.name+"</a></div><br/>",n+=o.parcelPoint.location.street+"<br/>",n+=o.parcelPoint.location.zipCode+" "+o.parcelPoint.location.city+(null===i?"":" ("+t.translation.distance.replace("%s",i)+")")+"<br/>",n+='<a class="'+this.branding+'-parcel-point-button" '+this.generateParcelPointTagData(o)+'"><b>'+t.translation.text.chooseParcelPoint+"</b></a>",n+="</td>",n+="</tr>"}n+="</tbody></table>",document.querySelector("#"+this.branding+"-pp-container").innerHTML=n},getMarkerHtmlElement:function(e){const t=document.createElement("div");return t.className=this.branding+"-marker",t.innerHTML=e,t},selectPoint:function(e,t,n,r,o,i,a,c,s,l,d){const p=this.getData(),u=this.getSelectedCarrier();u||d(p.translation.error.carrierNotFound);const g=new XMLHttpRequest;g.onreadystatechange=function(){4===g.readyState&&(200!==g.status?d(p.translation.error.couldNotSelectPoint):l())},g.open("POST",p.ajaxUrl),g.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),g.responseType="json",g.send("route=setPoint&carrier="+encodeURIComponent(u)+"&code="+encodeURIComponent(e)+"&name="+encodeURIComponent(t)+"&address="+encodeURIComponent(r)+"&zipcode="+encodeURIComponent(o)+"&city="+encodeURIComponent(i)+"&country="+encodeURIComponent(a)+"&openingHours="+encodeURIComponent(c)+"&distance="+encodeURIComponent(s)+"&network="+encodeURIComponent(n)+"&cartId="+encodeURIComponent(p.cartId)+"&token="+encodeURIComponent(p.token))},clearMarkers:function(){for(let e=0;e<this.markers.length;e++)this.markers[e].remove();this.markers=[]},getUniqueCarrier:function(){return document.querySelector('input[type="hidden"].shipping_method')},hasUniqueCarrier:function(){return null!==this.getUniqueCarrier()},getCarrierId:function(e){if(this.hasUniqueCarrier()){return this.getUniqueCarrier().getAttribute("value")}{const t=e.querySelector("input[type=radio]");return null===t?null:t.getAttribute("value")}},getSelectedCarrier:function(){let e;if(this.hasUniqueCarrier()){e=this.getUniqueCarrier().getAttribute("value")}else{e=this.getSelectedInput().getAttribute("value")}return e},getSelectedCarrierText:function(e,t){const n=this.getData();if(null===e)return"";const r=this.carrierTextCache;r[e]!==undefined&&(t.innerHTML=r[e]);const o=new XMLHttpRequest;o.onreadystatechange=function(){if(4===o.readyState){if(200!==o.status)t.innerHTML="";else{const e="object"==typeof o.response&&null!==o.response?o.response:JSON.parse(o.response);t.innerHTML=e.text}r[e]=t.innerHTML}},o.open("POST",n.ajaxUrl),o.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),o.responseType="json",o.send("route=getSelectedCarrierText&carrier="+encodeURIComponent(e)+"&cartId="+encodeURIComponent(n.cartId)+"&token="+encodeURIComponent(n.token))},getSelectedInput:function(){var e=document.querySelector(".delivery-option input[type='radio'][name^='delivery_option']:checked");return null===e&&(e=document.querySelector(".delivery_option_radio input[type='radio']:checked")),null===e&&(e=document.querySelector(".delivery_option input[type='radio']:checked")),e},getAllCarrierInputsSelector:function(){let e=".delivery-option input[type='radio'][name^='delivery_option']";return e+=", .delivery_option_radio input[type='radio']",e+=", .delivery_option input[type='radio']",".delivery-option input[type='radio'][name^='delivery_option'], .delivery_option_radio input[type='radio'], .delivery_option input[type='radio']"},showError:function(e){this.closeMap(),alert(e)},isDescendant:function(e,t){for(var n=t.parentNode;null!=n;){if(n==e)return!0;n=n.parentNode}return!1},on:function(e,t,n,r){const o=document.querySelector(e);o.addEventListener(t,(function(e){const t=o.querySelectorAll(n),i=e.target;for(let n=0,a=t.length;n<a;n++){let a=i;const c=t[n];for(;a&&a!==o;){if(a===c)return r.call(c,e);a=a.parentNode}}}))},observeDomMutations:function(e,t){const n=this;window.MutationObserver&&new MutationObserver((function(r){const o=[];for(let t of r)if("childList"===t.type){const r=Array.from(document.body.querySelectorAll(e)),i=Array.from(t.addedNodes);-1===r.indexOf(t.target)&&i.find((e=>-1!==r.indexOf(e)))===undefined&&i.find((t=>!(t instanceof Text)&&t.querySelector&&null!==t.querySelector(e)))===undefined&&r.find((e=>i.find((t=>n.isDescendant(e,t)))!==undefined))===undefined||o.push(t)}o.length>0&&t(o)})).observe(document.body,{attributes:!0,childList:!0,subtree:!0})}};
