import { CartItem } from '@/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[] => {
  if (newQuantity < 0) {
    return items;
  }

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