import '../../../core/api_gateway.dart'; JsonMap healthMap(Object? value) => value is Map ? Map.from(value) : {}; String healthText(Object? value) => value is String && value.isNotEmpty ? value : '—'; String healthNumber(Object? value) => value is num ? _number(value.toDouble()) : '—'; String _number(double value) => value.isFinite ? value.toStringAsFixed(value == value.roundToDouble() ? 0 : 1) : '—'; String healthPercent(Object? value) => value is num ? '${_number(value.toDouble())}%' : '—'; String healthBytes(Object? value) { if (value is! num || value < 0) return '—'; final bytes = value.toDouble(); const units = ['B', 'KiB', 'MiB', 'GiB', 'TiB']; var unit = 0; var amount = bytes; while (amount >= 1024 && unit < units.length - 1) { amount /= 1024; unit++; } return '${_number(amount)} ${units[unit]}'; } String healthDuration(Object? value) { if (value is! num || value < 0) return '—'; final seconds = value.toInt(); final days = seconds ~/ 86400; final hours = seconds ~/ 3600 % 24; final minutes = seconds ~/ 60 % 60; return days > 0 ? '${days}d ${hours}h' : hours > 0 ? '${hours}h ${minutes}m' : '${minutes}m'; } String healthAge(Object? value, DateTime now) { final sampled = DateTime.tryParse(value?.toString() ?? '')?.toLocal(); if (sampled == null) return 'Sampled —'; final seconds = now.difference(sampled).inSeconds.clamp(0, 999999); return 'Sampled ${seconds}s ago'; } double? healthRatio(Object? value) => value is num && value.isFinite ? (value.toDouble() / 100).clamp(0, 1) : null; const healthMeterHigh = 80.0; const healthMeterCritical = 95.0; /// Missing byte counts remain unknown rather than appearing as zero usage. double? healthUsageRatio(Object? used, Object? total) => used is num && total is num && used.isFinite && total.isFinite && total > 0 ? (used / total).clamp(0, 1).toDouble() : null;