import { CartItem } from '@/app/constants/types';

export const calculateTotal = (items: CartItem[]): number => {
  return items.reduce((total, item) => {
    return total + item.price * item.quantity;
  }, 0);
};

export const updateItemQuantity = (
  items: CartItem[],
  itemId: string,
  newQuantity: number
): CartItem[] => {
  // Don't allow negative quantities
  if (newQuantity < 0) {
    return items;
  }

  return items.map((item) =>
    item.id === itemId ? { ...item, quantity: newQuantity } : item
  );
};