import { getAddedCartItems, getVariant, totalValue } from '../../utils/cart'; import { pushDatalayer } from '../../utils/pushDatalayer'; import type { CartResponse, DeleteCartRequest } from '../../../Api/types'; export function removeFromCartAction( deletePayload: DeleteCartRequest, cartPayload: CartResponse, oldCartResponse?: CartResponse, ) { if (!cartPayload.orders.length) { pushDatalayer({ event: 'remove_from_cart', ecommerce: { currency: 'BRL', value: 0, items: [ { coupon: cartPayload.discountMap.coupon?.discountMainCode || '', item_category: '', item_brand: `${oldCartResponse!.orders[0].items[0].product.brandId}`, item_id: oldCartResponse!.orders[0].items[0].product.productId, item_name: oldCartResponse!.orders[0].items[0].product.name, item_variant: getVariant(oldCartResponse!.orders[0].items[0]), price: oldCartResponse!.orders[0].items[0].unitValue, quantity: 1, }, ], }, }); return; } pushDatalayer({ event: 'remove_from_cart', ecommerce: { currency: 'BRL', value: totalValue(cartPayload, deletePayload), items: getAddedCartItems(cartPayload, deletePayload).map(item => { return { coupon: cartPayload.discountMap.coupon?.discountMainCode || '', item_category: '', item_brand: `${item.product.brandId}`, item_id: item.product.productId, item_name: item.product.name, item_variant: getVariant(item), price: item.unitValue, quantity: item.quantity, }; }), }, }); }