define(['uiComponent','Magento_Customer/js/customer-data','jquery','ko','underscore','escaper','sidebar','mage/translate','mage/dropdown','mage/cookies'],function(Component,customerData,$,ko,_,escaper){'use strict';var sidebarInitialized=false,addToCartCalls=0,miniCart;miniCart=$('[data-block=\'minicart\']');function initSidebar(){if(miniCart.data('mageSidebar')){miniCart.sidebar('update');} if(!$('[data-role=product-item]').length){return false;} let slickCarouselElement=miniCart.find('.minicart-crossell .slick-initialized');if(slickCarouselElement.length){if(slickCarouselElement.is(':visible')){slickCarouselElement.trigger('refresh',[slickCarouselElement.slick('getSlick')]);}else{slickCarouselElement.css('visibility','hidden');setTimeout(function(){slickCarouselElement.css('visibility','');slickCarouselElement.trigger('refresh',[slickCarouselElement.slick('getSlick')]);},500);}} miniCart.trigger('contentUpdated');if(sidebarInitialized){return false;} sidebarInitialized=true;miniCart.sidebar({'targetElement':'div.block.block-minicart','url':{'checkout':window.checkout.checkoutUrl,'update':window.checkout.updateItemQtyUrl,'remove':window.checkout.removeItemUrl,'removeAll':window.checkout.ias.removeItemUrl,'loginUrl':window.checkout.customerLoginUrl,'isRedirectRequired':window.checkout.isRedirectRequired},'button':{'checkout':'#top-cart-btn-checkout','remove':'#mini-cart a.action.delete','clear':'a.action.clear','close':'#btn-minicart-close'},'showcart':{'parent':'span.counter','qty':'span.counter-number','label':'span.counter-label'},'minicart':{'list':'#mini-cart','content':'#minicart-content-wrapper','qty':'div.items-total','subtotal':'div.subtotal span.price','maxItemsVisible':window.checkout.minicartMaxItemsVisible},'item':{'qty':':input.cart-item-qty','button':':button.update-cart-item'},'confirmMessage':$.mage.__('Are you sure you would like to remove this item from the shopping cart?'),'clearConfirmMessage':$.mage.__('Are you sure you would like to remove all items from the shopping cart?')});} miniCart.on('dropdowndialogopen',function(){initSidebar();});return Component.extend({shoppingCartUrl:window.checkout.shoppingCartUrl,maxItemsToDisplay:window.checkout.maxItemsToDisplay,showQtyControls:window.checkout.ias.qty_controls,allowRemoveOnUpdate:window.checkout.ias.remove_on_update_qty,cart:{},messages:window.checkout.ias.minicart_messages?customerData.get('messages'):false,messagesVisible:ko.observable(false),allowedTags:['div','span','b','strong','i','em','u','a'],ajaxUpdate:false,crosssellActive:false,initialize:function(){var self=this,cartData=customerData.get('cart');if(window?.checkout?.ias?.minicart_crosssell){this.crosssellActive=true;} this.update(cartData());cartData.subscribe(function(updatedCart){addToCartCalls--;this.isLoading(addToCartCalls>0);sidebarInitialized=false;this.update(updatedCart);initSidebar();if(this.ajaxUpdate){if(!this.isLoading()){if(this.messages&&!this.isLoading()){this.messagesVisible(true);} this.ajaxUpdate=false;miniCart.trigger('ias.minicartUpdated');}}},this);miniCart.on('contentLoading',function(){addToCartCalls++;self.isLoading(true);});if(cartData().website_id!==window.checkout.websiteId&&cartData().website_id!==undefined||cartData().storeId!==window.checkout.storeId&&cartData().storeId!==undefined){customerData.reload(['cart'],false);} $(document).on('ajax:addToCart',function(){this.ajaxUpdate=true;}.bind(this));if(window.checkout.ias.open_minicart_on_update){miniCart.on('ias.minicartUpdated',function(){this.openMinicart();}.bind(this));if(this.messages){miniCart.on('dropdowndialogclose',function(){this.messagesVisible(false);}.bind(this));}} return this._super();},isLoading:ko.observable(false),initSidebar:initSidebar,openMinicart:function(){miniCart.find('[data-role="dropdownDialog"]').dropdownDialog('open');},closeMinicart:function(){miniCart.find('[data-role="dropdownDialog"]').dropdownDialog('close');},closeSidebar:function(){miniCart.on('click','[data-action="close"]',function(event){event.stopPropagation();miniCart.find('[data-role="dropdownDialog"]').dropdownDialog('close');});return true;},getItemRenderer:function(productType){return this.itemRenderer[productType]||'defaultRenderer';},update:function(updatedCart){if(updatedCart['crosssell']){let minicartCrosssells=$(updatedCart['crosssell']);minicartCrosssells.find('[name=form_key]').val($.cookie('form_key'));let currentLocationUENC=encodeURIComponent(btoa(document.location.href).replace(/=/gi,","));let elem,data,parsedUrl;minicartCrosssells.find('[name=uenc]').val(currentLocationUENC);minicartCrosssells.find('[data-role=tocart-form]').each(function(){elem=$(this);parsedUrl=elem.attr('action').split('/uenc/');parsedUrl elem.attr('action',parsedUrl[0]+'/uenc/'+currentLocationUENC+parsedUrl[1].substring(parsedUrl[1].indexOf("/")))});minicartCrosssells.find('[data-post]').each(function(){elem=$(this);data=JSON.parse(elem.attr('data-post'));if(data.data&&data.data.uenc){data.data.uenc=currentLocationUENC;} elem.attr('data-post',JSON.stringify(data));});updatedCart['crosssell']='';minicartCrosssells.each(function(i,elem){if(elem.outerHTML){updatedCart['crosssell']+=elem.outerHTML}});} _.each(updatedCart,function(value,key){if(!this.cart.hasOwnProperty(key)){this.cart[key]=ko.observable();} this.cart[key](value);},this);miniCart.trigger('updated',updatedCart);},getCartParamUnsanitizedHtml:function(name){if(!_.isUndefined(name)){if(!this.cart.hasOwnProperty(name)){this.cart[name]=ko.observable();}} return this.cart[name]();},getCartParam:function(name){return this.getCartParamUnsanitizedHtml(name);},getCartItems:function(){var items=this.getCartParamUnsanitizedHtml('items')||[];items=items.slice(parseInt(-this.maxItemsToDisplay,10));return items;},getCartLineItemsCount:function(){var items=this.getCartParamUnsanitizedHtml('items')||[];return parseInt(items.length,10);},prepareMessageForHtml:function(message){return escaper.escapeHtml(message,this.allowedTags);}});});