pickMostDue method

CardId pickMostDue (CardsSchedule s, State state)

Implementation

static CardId pickMostDue(CardsSchedule s, State state) {
  List<String> scheduleKey = ["learning", "overdue", "due"];
  for (int i = 0; i < scheduleKey.length; i++) {
    String key = scheduleKey[i];
    List<CardId> propertyValue = s.getPropertyValue(key);
    if (propertyValue.length > 0) {
      List<CardId> first = propertyValue.sublist(0);

      first.sort((CardId a, CardId b) {
        CardState cardA = state.cardStates[a.uniqueId];
        CardState cardB = state.cardStates[b.uniqueId];

        var reviewDiff = (cardA.lastReviewed == null &&
                cardB.lastReviewed != null)
            ? 1
            : (cardB.lastReviewed == null && cardA.lastReviewed != null)
                ? -1
                : (cardA.lastReviewed == null && cardB.lastReviewed == null)
                    ? 0
                    : (cardB.lastReviewed).compareTo(cardA.lastReviewed);

        if (reviewDiff != 0) {
          return -reviewDiff;
        }
        if (a == b) {
          throw Exception("comparing duplicate id: $a");
        }
        //TODO CHECK THAT
        return a.id > b.id ? 0 : 1;
      });
      return first[0];
    }
  }
}