import { getVariant } from '../../utils/cart'; import { pushDatalayer } from '../../utils/pushDatalayer'; import type { Search, Showcase, UrlBrand, UrlListing, } from '../../../Api/types'; export function selectItemAction( page: UrlBrand | UrlListing | Showcase | Search, productIndex: number, ) { if ('type' in page) { const product = page.content.registers[productIndex]; pushDatalayer({ event: 'select_item', ecommerce: { item_list_id: page.type === 'search' ? 'Search' : page.entity.name, item_list_name: page.type === 'search' ? 'Search' : page.entity.name, currency: 'BRL', items: [ { item_id: product.id, item_name: product.name, item_list_id: page.type === 'search' ? 'Search' : page.entity.name, item_list_name: page.type === 'search' ? 'Search' : page.entity.name, item_brand: product.brand?.name || '', item_variant: getVariant(product), item_category: product.mainCategory?.name || '', price: product.price?.salePrice || 0, index: productIndex + 1, }, ], }, }); return; } const product = page.products[productIndex]; pushDatalayer({ event: 'select_item', ecommerce: { item_list_id: page.name, item_list_name: page.name, currency: 'BRL', items: [ { item_id: product.id, item_name: product.name, item_list_id: page.name, item_list_name: page.name, item_brand: product.brand?.name || '', item_variant: getVariant(product), item_category: product.mainCategory?.name || '', price: product.price?.salePrice || 0, index: productIndex + 1, }, ], }, }); }