'use client'; import { colorHexMap } from '@/components/products/utils/colorUtils'; import { Button } from '@/components/ui/Button'; import { Text } from '@/components/ui/Typography'; import { useTranslation } from '@/lib/hooks/useTranslation'; import { useCart } from 'components/cart/cart-context'; import Price from 'components/price'; import { CartItem } from 'lib/shopify/types'; import { Trash2 } from 'lucide-react'; import Image from 'next/image'; import { QuantityControls } from './QuantityControls'; interface CartProductItemProps { item: CartItem; onUpdate: (merchandiseId: string, updateType: 'plus' | 'minus' | 'delete', itemId?: string) => void; isPending: boolean; isInBox?: boolean; boxGroupId?: string; } export function CartProductItem({ item, onUpdate, isPending, isInBox = false, boxGroupId }: CartProductItemProps) { const { t } = useTranslation(); const { cart } = useCart(); // Check if this item has a color option const colorOption = item.merchandise.selectedOptions.find(option => option.name.toLowerCase() === 'color' || option.name.toLowerCase() === 'colour' || option.name.toLowerCase() === 'boja' ); // Get the color hex code from the color name if it exists const colorHex = colorOption ? colorHexMap[colorOption.value.toLowerCase()] || colorOption.value : null; // Get the unique ID of this cart item const itemId = item.id; // Handle quantity changes const handleIncrease = () => onUpdate(item.merchandise.id, 'plus', itemId); const handleDecrease = () => onUpdate(item.merchandise.id, 'minus', itemId); // Enhanced delete handler const handleDelete = () => { // Basic delete operation for this item onUpdate(item.merchandise.id, 'delete', itemId); // Special handling for box items if (isInBox && boxGroupId && cart) { // Find all items in this box group const boxItems = cart.lines.filter(line => { const attrs = line.attributes || []; const itemBoxGroupId = attrs.find(attr => attr.key === '_box_group_id')?.value; const itemBoxType = attrs.find(attr => attr.key === '_box_type')?.value; return itemBoxGroupId === boxGroupId && itemBoxType === 'item' && line.id !== itemId; }); // If this is the last item (only 1 left - the one we're deleting), also delete the box container if (boxItems.length === 0) { // Find the box container const boxContainer = cart.lines.find(line => { const attrs = line.attributes || []; const containerBoxGroupId = attrs.find(attr => attr.key === '_box_group_id')?.value; const containerBoxType = attrs.find(attr => attr.key === '_box_type')?.value; return containerBoxGroupId === boxGroupId && containerBoxType === 'container'; }); // Delete the box container too if (boxContainer && boxContainer.id) { onUpdate(boxContainer.merchandise.id, 'delete', boxContainer.id); } } } }; return (