/**
 * Calculates the total quantity of a receipt item that has already been returned.
 *
 * This function sums the `quantity` values from the `referencedInOtherReceipts` array,
 * representing how many units of the item have been processed for return in other receipts.
 * If no return history is present, it returns 0.
 *
 * @param item - The receipt item object containing an `id` and optional return history.
 * @returns The total number of units already returned for this receipt item.
 */
export function getReceiptItemReturnedQuantity(item: {
  id: number;
  referencedInOtherReceipts?: { quantity: number }[];
}): number {
  return (
    item.referencedInOtherReceipts?.reduce(
      (sum, receipt) => sum + (receipt?.quantity || 0),
      0,
    ) || 0
  ); // Default to 0 if not specified
}
