'use client' import { useState, useEffect } from 'react' import { Sheet, SheetContent, SheetDescription, SheetHeader, SheetTitle, SheetTrigger } from '@/components/ui/sheet' import { Button } from '@/components/ui/button' import { Badge } from '@/components/ui/badge' import { Separator } from '@/components/ui/separator' import { ShoppingCart, X, Plus, Minus } from 'lucide-react' import { cartManager } from '@/lib/cart' import Link from 'next/link' import Image from 'next/image' import { toast } from 'sonner' interface CartItem { id: string name: string price: number quantity: number image: string | null } interface CartSidebarProps { children?: React.ReactNode } export default function CartSidebar({ children }: CartSidebarProps) { const [cart, setCart] = useState([]) const [isOpen, setIsOpen] = useState(false) const loadCart = () => { const cartItems = cartManager.getCart() setCart(cartItems) } useEffect(() => { loadCart() const handleCartUpdate = () => loadCart() window.addEventListener('cartUpdated', handleCartUpdate) return () => { window.removeEventListener('cartUpdated', handleCartUpdate) } }, []) const updateQuantity = (productId: string, newQuantity: number) => { cartManager.updateQuantity(productId, newQuantity) loadCart() } const removeItem = (productId: string) => { cartManager.removeFromCart(productId) toast.success('Item removed from cart') loadCart() } const itemCount = cart.reduce((sum, item) => sum + item.quantity, 0) const cartTotal = cartManager.getTotalPrice() return ( {children || ( )} Shopping Cart ({itemCount}) Manage your cart items
{cart.length === 0 ? (

Your cart is empty

) : (
{cart.map((item) => (
{item.name}

{item.name}

₹{(item.price || 0).toFixed(2)}
{item.quantity || 0}
))}
Total ₹{cartTotal.toFixed(2)}
)}
) }