define(['jquery','Magento_Customer/js/customer-data','Magento_GoogleTagManager/js/google-analytics-universal','Magento_GoogleTagManager/js/google-analytics-universal-cart','underscore','jquery-ui-modules/widget'],function($,customerData,GoogleAnalyticsUniversal,GoogleAnalyticsUniversalCart,_){'use strict';$.widget('mage.gtmCart',{options:{dlCurrencyCode:window.dlCurrencyCode||'',dataLayer:window.dataLayer||[],staticImpressions:window.staticImpressions||[],staticPromotions:window.staticPromotions||[],updatedImpressions:window.updatedImpressions||[],updatedPromotions:window.updatedPromotions||[],cookieAddToCart:'',cookieRemoveFromCart:window.cookieRemoveFromCart||'',temporaryEventStorage:[],blockNames:[],events:{AJAX_ADD_TO_CART:'ajax:addToCart',AJAX_REMOVE_FROM_CART:'ajax:removeFromCart'},actions:{}},_create:function(){this.googleAnalyticsUniversalCart=new GoogleAnalyticsUniversalCart({dlCurrencyCode:this.options.dlCurrencyCode,dataLayer:this.options.dataLayer,cookieAddToCart:this.options.cookieAddToCart,cookieRemoveFromCart:this.options.cookieRemoveFromCart});this.googleAnalyticsUniversal=new GoogleAnalyticsUniversal({blockNames:this.options.blockNames,dlCurrencyCode:this.options.dlCurrencyCode,dataLayer:this.options.dataLayer,staticImpressions:this.options.staticImpressions,staticPromotions:this.options.staticPromotions,updatedImpressions:this.options.updatedImpressions,updatedPromotions:this.options.updatedPromotions});this.cartItemsCache=[];this._initActions();this._setListeners();this._setCartDataListener();this.googleAnalyticsUniversal.updatePromotions();this.googleAnalyticsUniversal.productImpressions();this.googleAnalyticsUniversalCart.parseAddToCartCookies();this.googleAnalyticsUniversalCart.parseRemoveFromCartCookies();this.googleAnalyticsUniversalCart.subscribeProductsUpdateInCart();this.googleAnalyticsUniversalCart.listenMinicartReload();this._iteCartItem();},_initActions:function(){var events=this.options.events;var header=window.gaHeaderData;this.cartItemsCache=customerData.get('cart')().items;this.options.actions[events.AJAX_ADD_TO_CART]=function(product){}.bind(this);this.options.actions[events.AJAX_REMOVE_FROM_CART]=function(product){}.bind(this);if($.cookie('vas-add')){this.googleAnalyticsUniversal.addToCart(header,[JSON.parse($.cookie("vas-add"))]);$.cookie('vas-add',null,{path:'/'});} if($.cookie('vas-remove')){this.googleAnalyticsUniversal.removeItemFromCart(header,[JSON.parse($.cookie("vas-remove"))]);$.cookie('vas-remove',null,{path:'/'});} $(document).on('submit','#draft-to-cart',function(){$.cookie("draft-to-cart",1);});$(document).on('submit','#modal-dq-form',function(){$.cookie("draft-to-cart",1);var items=customerData.get('cart')().items;var dlUpdate={'event':'remove_from_cart','ecommerce':{'currency':window.dlCurrencyCode,'items':items}};var addUpdate=Object.assign({},header,dlUpdate);window.dataLayer.push({'ecommerce':null});window.dataLayer.push(addUpdate);});if($.cookie('draft-to-cart')&&$('body').hasClass('checkout-cart-index')){this.googleAnalyticsUniversalCart.cartPageEvent('add_to_cart') $.cookie('draft-to-cart',null,{path:'/'});} if($(document).find('#ite-child-product-id').length&&!$.cookieStorage.get('mage-messages').length){var oldCartOnIte=[];if(this.cartItemsCache.length){this.cartItemsCache.each(function(item){oldCartOnIte.push({'item_id':item.product_sku,'item_name':item.product_name.toLowerCase(),'price':item.product_price_value,'quantity':item.qty})});} $.cookie('cart-data-on-ite',JSON.stringify(oldCartOnIte));}},_addToCartGaActions:function(oldCartItems,currentCartItems){var newCartItems=[];var header=window.gaHeaderData;if(oldCartItems.length===0){newCartItems.push({'item_id':currentCartItems[0].product_sku,'item_name':currentCartItems[0].product_name.toLowerCase(),'price':currentCartItems[0].product_price_value,'quantity':currentCartItems[0].qty});}else{var qty=0;var items=currentCartItems.filter(({item_id:id1})=>!oldCartItems.some(({item_id:id2})=>id2===id1));if(items.length===0){var items=currentCartItems.filter(({item_id:id1,qty:q1})=>!oldCartItems.some(({item_id:id2,qty:q2})=>id2===id1&&q1===q2));qty=currentCartItems.find(o=>o.item_id===items[0].item_id).qty-oldCartItems.find(o=>o.item_id===items[0].item_id).qty;} items.forEach(function(item,index){newCartItems.push({'item_id':item.product_sku,'item_name':item.product_name.toLowerCase(),'price':item.product_price_value,'quantity':qty?qty:item.qty})});} this.googleAnalyticsUniversal.addToCart(header,newCartItems);},getProduct:function(productInfo){var searchCriteria,productOptionValues=productInfo.optionValues||[],productFromCache,productFromCart;searchCriteria=function(item){var index=0;if(item['product_id']!==productInfo.id){return false;} if(productOptionValues.length===0){return true;} while(index!oldCartItems.some(({id:id2})=>id2===id1));newCartItems.forEach(item=>{delete item.id;});if(newCartItems.length){var dlUpdate={'event':'add_to_cart','ecommerce':{'currency':window.dlCurrencyCode,'items':newCartItems}};var addUpdate=Object.assign({},window.gaHeaderData,dlUpdate);dataLayer.push(addUpdate);} $.cookie('cart-data-on-ite',null,{path:'/'});}},_executeEvents:function(){var product;this.options.temporaryEventStorage.forEach(function(item,index){if(typeof item.productInfo==='undefined'){return;} item.productInfo.forEach(function(productInfoItem){product=this.getProduct(productInfoItem);if(!_.isUndefined(product['product_sku'])&&parseInt(product.qty,10)>0){this.options.actions[item.type](product);} this.options.temporaryEventStorage.splice(index,1);}.bind(this));}.bind(this));},_setListeners:function(){var handlerWrapper=function(callback,type,event,eventData){callback.call(this,type,eventData.productInfo);},opt=this.options;$(document).on(opt.events.AJAX_ADD_TO_CART,handlerWrapper.bind(this,this._setToTemporaryEventStorage,opt.events.AJAX_ADD_TO_CART)).on(opt.events.AJAX_REMOVE_FROM_CART,handlerWrapper.bind(this,this._setToTemporaryEventStorage,opt.events.AJAX_REMOVE_FROM_CART));}});return $.mage.gtmCart;});