import '../../../core/api_gateway.dart'; /// Search hits newest first, the order the server sends them in. The app /// sorts again so each displayed group stays newest first even from an older /// server that ranked by match tier. Equal or missing dates keep the server's /// order; a hit without a date sorts last. List searchHitsNewestFirst(List hits) { final indexed = [ for (var i = 0; i < hits.length; i++) (i, _hitMillis(hits[i]), hits[i]), ]; indexed.sort((a, b) { final byDate = b.$2.compareTo(a.$2); return byDate != 0 ? byDate : a.$1.compareTo(b.$1); }); return [for (final entry in indexed) entry.$3]; } /// A chat hit's last-used time (`chat.updated_at`, epoch milliseconds), else /// any hit's `recency` timestamp; -1 when neither is present. int _hitMillis(JsonMap hit) { final chat = hit['chat']; if (chat is Map) { final updated = chat['updated_at']; if (updated is num && updated > 0) return updated.toInt(); } final recency = DateTime.tryParse('${hit['recency'] ?? ''}'); return recency?.millisecondsSinceEpoch ?? -1; }