import 'package:pshared/models/payment/operation.dart';


List<OperationItem> sortOperations(List<OperationItem> operations) {
  final sorted = List<OperationItem>.from(operations);
  sorted.sort((a, b) {
    final aTime = a.date.millisecondsSinceEpoch;
    final bTime = b.date.millisecondsSinceEpoch;
    final aUnknown = isUnknownDate(a.date);
    final bUnknown = isUnknownDate(b.date);

    if (aUnknown != bUnknown) {
      return aUnknown ? 1 : -1;
    }
    if (aTime != bTime) {
      return bTime.compareTo(aTime);
    }
    return a.payId.compareTo(b.payId);
  });
  return sorted;
}

bool isUnknownDate(DateTime date) => date.millisecondsSinceEpoch == 0;
