/// "Send later…": the quick times the composer offers for scheduling the /// operator's own message (the server holds it until `send_at`). library; import 'chat_account_usage.dart'; import 'chat_usage_limit.dart'; enum ChatSendLaterKind { halfHour, hour, tomorrowMorning, usageReset } typedef ChatSendLaterChoice = ({ChatSendLaterKind kind, DateTime at}); /// When the account's quota is back: the exhausted limit's reset if one is /// reported, else the next reported window reset; a minute later, as /// auto-continue does. Null when no reset time is known. DateTime? chatSendLaterUsageReset(ChatAccountUsage? usage, DateTime now) { final limit = chatUsageLimit(usage, now); if (limit != null) return limit.sendAt; if (usage == null || usage.unsupported) return null; int? next; for (final window in usage.windows) { final at = window.resetsAt; if (at <= now.millisecondsSinceEpoch) continue; if (next == null || at < next) next = at; } if (next == null) return null; return DateTime.fromMillisecondsSinceEpoch(next).add(chatScheduleMargin); } /// In 30 minutes, in 1 hour, tomorrow at 9:00 and, when known, at the usage /// reset. List chatSendLaterChoices( DateTime now, { DateTime? usageReset, }) { final local = now.toLocal(); return [ ( kind: ChatSendLaterKind.halfHour, at: local.add(const Duration(minutes: 30)), ), (kind: ChatSendLaterKind.hour, at: local.add(const Duration(hours: 1))), ( kind: ChatSendLaterKind.tomorrowMorning, at: DateTime(local.year, local.month, local.day + 1, 9), ), if (usageReset != null && usageReset.isAfter(now)) (kind: ChatSendLaterKind.usageReset, at: usageReset.toLocal()), ]; } /// A picked time of day: today when it is still ahead, else tomorrow. DateTime chatSendLaterAt(DateTime now, int hour, int minute) { final local = now.toLocal(); final today = DateTime(local.year, local.month, local.day, hour, minute); return today.isAfter(local) ? today : DateTime(local.year, local.month, local.day + 1, hour, minute); }