import { coerceBooleanProperty } from '@angular/cdk/coercion';
import { ReceiptItem } from '../../models';
import { getReceiptItemAction } from './get-receipt-item-action.helper';

/**
 * Determines if a receipt item can be returned.
 *
 * @param receiptItem - The receipt item to check for return eligibility. Must have an 'actions' property (array of action objects).
 * @returns {boolean} True if the item has a 'canReturn' action with a truthy value (coerced to boolean), otherwise false.
 *
 * @remarks
 * - Returns false if the 'actions' property is missing or not an array.
 * - Returns false if the 'canReturn' action is not present in the actions array.
 * - Uses Angular's coerceBooleanProperty to interpret the action value.
 */
export function canReturnReceiptItem(receiptItem: ReceiptItem): boolean {
  if (!receiptItem.actions) {
    return false;
  }
  const canReturnAction = getReceiptItemAction(receiptItem, 'canReturn');
  if (!canReturnAction) {
    return false;
  }
  return coerceBooleanProperty(canReturnAction.value);
}
