class PostcodeBarController extends Stimulus.Controller{static get targets(){return["loading","form","orderSummary","minimumOrderMessage","orderRemainingMessage","checkoutMessage","orderRemaining","minimumOrder","progressBar","freeDeliveryMessage","freeDeliveryExtraAmount","freeDeliveryType"]}initialize(){this.is_serviced=!1,this.is_local=!1,this.is_national=!1}async connect(){await this._retrievePostcodeStatus(),this._showRelevantBarComponents(),document.addEventListener("postcode_updated",async()=>{await this._retrievePostcodeStatus(),this._showRelevantBarComponents()}),$(document.body).on("afterGetCart.ajaxCart",cart=>{this._showRelevantBarComponents()}),$(document.body).on("afterChangeItem.ajaxCart",cart=>{this._showRelevantBarComponents()})}_showRelevantBarComponents(){this._hideAllComponents(),this.is_serviced?this._showOrderProgress():this._showPostcodeForm()}async _retrievePostcodeStatus(){await fieldDoctorAPI.delivery.available()?(this.is_serviced=!0,await fieldDoctorAPI.delivery.local()?this.is_local=!0:await fieldDoctorAPI.delivery.national()&&(this.is_national=!0)):(this.is_serviced=!1,this.is_local=!1,this.is_national=!1)}_hideAllComponents(){$(this.loadingTarget).hide(),$(this.formTarget).hide(),$(this.orderSummaryTarget).hide()}_showPostcodeForm(){$(this.formTarget).show()}async _orderAmount(){const orderAttribute=this.progressBarTarget.dataset.orderAmount;return Number.isInteger(orderAttribute)?orderAttribute:(await fieldDoctorAPI.getCart()).items_subtotal_price}async _showOrderProgress(){const minimumOrder=await fieldDoctorAPI.getMinimumOrder(),minimumOrderFormatted="\xA3"+(minimumOrder/100).toFixed(2);$(this.minimumOrderMessageTarget).hide(),$(this.orderRemainingMessageTarget).hide(),$(this.checkoutMessageTarget).hide(),$(this.freeDeliveryMessageTarget).hide(),$(this.orderSummaryTarget).show();const orderAmount=await this._orderAmount(),orderProgress=Math.min(orderAmount/minimumOrder,1)*100;if(this.progressBarTarget.style.width=orderProgress+"%",orderAmount==0)this.minimumOrderTarget.innerText=minimumOrderFormatted,$(this.minimumOrderMessageTarget).show();else if(orderAmount>=minimumOrder){const leftToFreeDelivery=await fieldDoctorAPI.delivery.orderVolumeForReducedCost()-orderAmount,deliveryCost=await fieldDoctorAPI.delivery.cost(),reducedDeliveryCost=await fieldDoctorAPI.delivery.reducedCost();leftToFreeDelivery>0&&minimumOrder!=0&&(this.freeDeliveryExtraAmountTarget.innerText="\xA3"+(leftToFreeDelivery/100).toFixed(2),reducedDeliveryCost==0?this.freeDeliveryTypeTarget.innerText="free delivery":this.freeDeliveryTypeTarget.innerText="reduced delivery",$(this.freeDeliveryMessageTarget).show()),$(this.checkoutMessageTarget).show()}else{const orderRemainingFormatted="\xA3"+((minimumOrder-orderAmount)/100).toFixed(2);$(this.orderRemainingMessageTarget).show(),this.orderRemainingTarget.innerText=orderRemainingFormatted}}}
//# sourceMappingURL=/cdn/shop/t/104/assets/postcode-bar_controller.js.map