import '../../../core/api_gateway.dart'; import 'chat_transcript.dart'; /// When a transcript row was recorded, in epoch milliseconds, or null when it /// carries no valid time (cards, grouped chips, local rows). /// /// An agent turn records its first message's time on the row; a user turn, a /// notice or a report carries it on its `message`; a run of grouped notices /// takes its first notice's. int? chatRowCreatedAt(JsonMap row) { final own = chatMessageCreatedAt(row); if (own != null) return own; final message = row['message']; if (message is Map) { return chatMessageCreatedAt(Map.from(message)); } final messages = row['messages']; if (messages is List) { for (final message in messages) { if (message is! Map) continue; final time = chatMessageCreatedAt(Map.from(message)); if (time != null) return time; } } return null; } /// The local calendar day of epoch milliseconds [createdAt]. DateTime chatLocalDay(int createdAt) { final local = DateTime.fromMillisecondsSinceEpoch( createdAt, isUtc: true, ).toLocal(); return DateTime(local.year, local.month, local.day); } /// For each of [rows], the local day a date divider above it names, or null. /// /// The first timed row of each calendar day gets its day, so a transcript /// shows the date once per day and each row only its time. Rows without a /// time never start a day and never break one. List chatDayDividers(List rows) { DateTime? current; return [ for (final row in rows) switch (chatRowCreatedAt(row)) { null => null, final time when chatLocalDay(time) == current => null, final time => current = chatLocalDay(time), }, ]; }