import React from 'react' import { Quantifier, MiniQuantifier } from 'web-components' import { useGetCart, useUpdateCartItem, useRemoveFromCart, useAddToCart } from '../hooks/cart-query-hooks' import { useCartStore } from '../lib/stores/cart-store' import { useCentralSlotStore } from '../lib/stores/central-slot-store' import { useProductSlotIdentifier } from '../hooks/useProductSlotIdentifier' import dayjs from 'dayjs' import { Truck, ShoppingCart, ImageOff } from 'lucide-react' interface ProductCardProps { item: any onPress?: () => void showDeliveryInfo?: boolean miniView?: boolean useAddToCartDialog?: boolean } const formatQuantity = (quantity: number, unit: string): { value: string; display: string } => { if (unit?.toLowerCase() === 'kg' && quantity < 1) { return { value: `${Math.round(quantity * 1000)} g`, display: `${Math.round(quantity * 1000)}g` } } return { value: `${quantity} ${unit}(s)`, display: `${quantity}${unit}` } } export function ProductCard({ item, onPress, showDeliveryInfo = true, miniView = false, useAddToCartDialog = false, }: ProductCardProps) { const [imageError, setImageError] = React.useState(false) const [imageLoading, setImageLoading] = React.useState(true) const imageUri = item.images?.[0] const { data: cartData } = useGetCart('regular') const { getQuickestSlot } = useProductSlotIdentifier() const { setAddedToCartProduct } = useCartStore() const updateCartItem = useUpdateCartItem('regular') const removeFromCart = useRemoveFromCart('regular') const addToCart = useAddToCart('regular') // Find current quantity from cart data const cartItem = cartData?.items?.find((cartItem: any) => cartItem.productId === item.id) const quantity = cartItem?.quantity || 0 // Get slots data from central store const slots = useCentralSlotStore((state) => state.slots) const productSlotsMap = useCentralSlotStore((state) => state.productSlotsMap) // Create slot lookup map const slotMap = React.useMemo(() => { const map: Record = {} slots?.forEach((slot: any) => { map[slot.id] = slot }) return map }, [slots]) // Get cart item's slot delivery time if item is in cart const cartSlot = cartItem?.slotId ? slotMap[cartItem.slotId] : null const displayDeliveryDate = cartSlot?.deliveryTime || item.nextDeliveryDate // Precompute the next slot and determine display out of stock status const slotId = getQuickestSlot(item.id) const productSlotInfo = productSlotsMap[item.id] const isOutOfStockFromSlots = productSlotInfo?.isOutOfStock const displayIsOutOfStock = isOutOfStockFromSlots || !slotId const handleQuantityChange = (newQuantity: number) => { if (useAddToCartDialog) { setAddedToCartProduct({ productId: item.id, product: item }) } else if (newQuantity === 0 && cartItem) { removeFromCart.mutate(cartItem.id) } else if (newQuantity === 1 && !cartItem) { const slotId = getQuickestSlot(item.id) if (!slotId) { alert('No available delivery slot for this product') return } const slot = slotMap[slotId] const deliveryTime = slot ? dayjs(slot.deliveryTime).format('ddd, DD MMM • h:mm A') : '' addToCart.mutate( { productId: item.id, quantity: 1, slotId, storeId: item.storeId }, { onSuccess: () => { alert(`Added ${item.name} for delivery at ${deliveryTime}`) }, } ) } else if (cartItem) { updateCartItem.mutate({ productId: item.id, quantity: newQuantity }) } } const discountPercent = item.marketPrice && Number(item.marketPrice) > Number(item.price) ? Math.round(((Number(item.marketPrice) - Number(item.price)) / Number(item.marketPrice)) * 100) : 0 return (
{/* Image — grows to fill card height in miniView so content area stays tight */}
{imageUri && !imageError ? ( {item.name} setImageError(true)} onLoad={() => setImageLoading(false)} /> ) : (
)} {imageLoading && imageUri && !imageError && (
)} {/* Discount label hidden for now {discountPercent > 0 && (

{discountPercent}% OFF

)} */} {displayIsOutOfStock && (

Out of Stock

)} {miniView && (
{quantity > 0 ? ( ) : (
{ e.stopPropagation() handleQuantityChange(1) }} >
)}
)}
{/* Content */}

{item.name}

₹{item.price}

{discountPercent > 0 && (

₹{item.marketPrice}

)}

Qty: {formatQuantity(item.productQuantity || 1, item.unitNotation).display}

{/* Delivery date hidden for now {showDeliveryInfo && displayDeliveryDate && (

{dayjs(displayDeliveryDate).format('ddd, DD MMM')}

{dayjs(displayDeliveryDate).format('h:mm A')}

)} */} {!miniView && (
{displayIsOutOfStock ? (

Unavailable

) : quantity > 0 ? ( ) : ( )}
)}
) }