import {
  ReceiptCompleteStatus,
  ReceiptCompleteStatusValue,
  Return,
} from '../models';

/**
 * Helper function to determine the receipt status from a return object.
 * Returns 'Offen' if no receipts or all are incomplete, otherwise returns 'Abgeschlossen'.
 *
 * @param {Return} returnData - The return object containing receipts
 * @returns {ReceiptCompleteStatusValue} The completion status of the return
 */
export const getReceiptStatusFromReturn = (
  returnData: Return,
): ReceiptCompleteStatusValue => {
  if (!returnData?.receipts || returnData.receipts.length === 0) {
    return ReceiptCompleteStatus.Offen;
  }

  const hasCompletedReceipt = returnData.receipts.some(
    (receipt) => receipt.data?.completed,
  );

  return hasCompletedReceipt
    ? ReceiptCompleteStatus.Abgeschlossen
    : ReceiptCompleteStatus.Offen;
};
