import { create } from 'zustand' import { persist } from 'zustand/middleware' export interface CartItem { product_id: string name: string price: number quantity: number image?: string } interface CartState { items: CartItem[] total: number addItem: (item: CartItem) => void removeItem: (product_id: string) => void updateQuantity: (product_id: string, qty: number) => void clearCart: () => void } function calcTotal(items: CartItem[]): number { return items.reduce((sum, item) => sum + item.price * item.quantity, 0) } export const useCartStore = create()( persist( (set, get) => ({ items: [], total: 0, addItem: (item: CartItem) => { const existing = get().items.find(i => i.product_id === item.product_id) let newItems: CartItem[] if (existing) { newItems = get().items.map(i => i.product_id === item.product_id ? { ...i, quantity: i.quantity + item.quantity } : i ) } else { newItems = [...get().items, item] } set({ items: newItems, total: calcTotal(newItems) }) }, removeItem: (product_id: string) => { const newItems = get().items.filter(i => i.product_id !== product_id) set({ items: newItems, total: calcTotal(newItems) }) }, updateQuantity: (product_id: string, qty: number) => { if (qty <= 0) { get().removeItem(product_id) return } const newItems = get().items.map(i => i.product_id === product_id ? { ...i, quantity: qty } : i ) set({ items: newItems, total: calcTotal(newItems) }) }, clearCart: () => { set({ items: [], total: 0 }) }, }), { name: 'cart', } ) )