"use client" import { create } from "zustand" import { persist } from "zustand/middleware" import type { Product } from "./products" export interface CartItem { product: Product quantity: number } interface CartStore { items: CartItem[] addItem: (product: Product, quantity?: number) => void removeItem: (productId: string) => void updateQuantity: (productId: string, quantity: number) => void clearCart: () => void getTotalItems: () => number getTotalPrice: () => number } export const useCart = create()( persist( (set, get) => ({ items: [], addItem: (product: Product, quantity = 1) => { set((state) => { const existingItem = state.items.find((item) => item.product.id === product.id) if (existingItem) { return { items: state.items.map((item) => item.product.id === product.id ? { ...item, quantity: item.quantity + quantity } : item, ), } } return { items: [...state.items, { product, quantity }], } }) }, removeItem: (productId: string) => { set((state) => ({ items: state.items.filter((item) => item.product.id !== productId), })) }, updateQuantity: (productId: string, quantity: number) => { if (quantity <= 0) { get().removeItem(productId) return } set((state) => ({ items: state.items.map((item) => (item.product.id === productId ? { ...item, quantity } : item)), })) }, clearCart: () => { set({ items: [] }) }, getTotalItems: () => { return get().items.reduce((total, item) => total + item.quantity, 0) }, getTotalPrice: () => { return get().items.reduce((total, item) => total + item.product.price * item.quantity, 0) }, }), { name: "plumbing-cart", }, ), )