'use server';

import { retrieveCart, updateCart, updateLineItem } from '@lib/data/cart';

export const updateCartPartnerLocation = async ({
  cartId,
  lineIds,
  partnerLocationId,
  partnerLocationName,
}: {
  cartId: string;
  lineIds: string[];
  partnerLocationId: string;
  partnerLocationName: string;
}) => {
  const cart = await retrieveCart(cartId);
  if (!cart) {
    throw new Error('Cart not found');
  }

  for (const lineItemId of lineIds) {
    await updateLineItem({
      lineId: lineItemId,
      quantity: 1,
      metadata: {
        partner_location_name: partnerLocationName,
        partner_location_id: partnerLocationId,
      },
    });
  }
  await updateCart({
    id: cartId,
    metadata: {
      partner_location_name: partnerLocationName,
      partner_location_id: partnerLocationId,
    },
  });
};
