import { Return } from '../models';

/**
 * Helper function to calculate the total item quantity from all receipts in a return.
 * If no receipts are present, returns 0.
 * @param {Return} returnData - The return object containing receipts
 * @return {number} Total item quantity from all receipts
 */
export const getReceiptItemQuantityFromReturn = (
  returnData: Return,
): number => {
  if (!returnData?.receipts || returnData.receipts.length === 0) {
    return 0;
  }

  return returnData.receipts.reduce((totalItems, receipt) => {
    const items = receipt.data?.items;
    return totalItems + (items ? items.length : 0);
  }, 0);
};
