(function($) {
    if (typeof sbnData !== 'undefined') {
        var currency = sbnData.currency || '';
        var newCart = sbnData.currentCartProducts || [];
        //var _sbnaq = _sbnaq || [];

        _sbnaq.push(['setCurrencyCode', currency]);

        if (sbnData.orderDetail !=null && sbnData.orderDetail.lines && sbnData.orderDetail.lines.length>0) {
            var orderDetail = sbnData.orderDetail || {};
            var orderProcessed = localStorage.getItem('lastProcessedOrderId');

            if (orderDetail.orderId && orderDetail.orderId !== orderProcessed && orderDetail.lines.length > 0) {
                orderDetail.lines.forEach(function(item) {
                    _sbnaq.push([
                        'trkProductSale',
                        orderDetail.orderId,
                        item.productId,
                        item.quantity,
                        item.coupon,
                        item.price,
                        item.priceWithTax,
                        item.deliveryPrice,
                        item.currency,
                        item.dateTime
                    ]);
                });
                localStorage.setItem('lastProcessedOrderId', orderDetail.orderId);
                localStorage.setItem('cartItems', JSON.stringify([]));
            }
        } else {
            var oldCart = [];
            var oldCartItems = [];
            var oldCartQuantities = [];
            if (localStorage.getItem('cartItems')) {
                oldCart = JSON.parse(localStorage.getItem('cartItems'));
                oldCartItems = oldCart.map(oldItem => oldItem.productId);
                oldCartQuantities = oldCart.map(oldItem => oldItem.quantity);
            }
            var newCartItems = newCart.map((item) => item.productId);
            var newCartQuantities = newCart.map((item) => item.quantity);
            if(newCartItems.length > 0){
                _sbnaq.push(["setCtxParamProductIds", newCartItems.join(',')]);
                _sbnaq.push(["setCtxParamProductQuantities", newCartQuantities.join(',')]);
            }
            newCartItems.forEach((productInNewCart, index) => {
                let oldProductIndex = oldCartItems.indexOf(productInNewCart);
                let oldProductQty = oldProductIndex !== -1 ? oldCartQuantities[oldProductIndex] : 0;
                for (let i = 0; i < newCartQuantities[index]-oldProductQty; i++) {
                    _sbnaq.push(['trkProductBasketAdd', productInNewCart]);
                }
              })
            oldCartItems.forEach((productInOldCart,index) => {
                let newProductIndex = newCartItems.indexOf(productInOldCart);
                let newProductQty = newProductIndex !== -1 ? newCartQuantities[newProductIndex] : 0;
                for (let i = 0; i < oldCartQuantities[index]-newProductQty; i++) {
                    _sbnaq.push(['trkProductBasketRemove', productInOldCart]);
                }
            })
            if (newCartItems.length > 0) {
                _sbnaq.push(["setCtxParamProductIds", newCartItems.map(el => el.productId).join(',')]);
                _sbnaq.push(["setCtxParamProductQuantities", newCartItems.map(el => el.quantity).join(',')]);
            }
            localStorage.setItem('cartItems', JSON.stringify(newCart));
        }
    }
})(jQuery);
