import 'dart:convert'; import 'wallet.dart'; /// Local BFF transport only; provider and widgets never parse response maps. class BackendDTO { BackendDTO.decode(String body) { final envelope = jsonDecode(body) as Map; accepted = envelope['status'] == 'success'; data = envelope['data']; } late final bool accepted; late final dynamic data; String get error => (data as Map?)?['error'] as String? ?? 'unavailable'; String get locale => (data as Map)['locale'] as String; String get key => (data as Map)['idempotencyKey'] as String; List get accounts => ((data as Map)['accounts'] as List).map((v) { final d = v as Map; return AccountDTO( d['id'] as String, d['currency'] as String, d['precision'] as int, d['available'] as int, payoutMethod: d['payoutMethod'] as String, payoutLabel: d['payoutLabel'] as String, canFund: d['canFund'] as bool, ); }).toList(); List get operations => ((data as Map)['operations'] as List) .map((v) => operation(v as Map)) .toList(); QuoteDTO get quoteValue => quote(data as Map); OperationDTO? get operationValue => data == null ? null : operation(data as Map); static QuoteDTO quote(Map d) { final r = d['request'] as Map; return QuoteDTO( d['id'] as String, RequestDTO( r['kind'] as String, r['source'] as String, (r['destination'] as String?)?.isEmpty == true ? null : r['destination'] as String?, r['amount'] as int, r['recipient'] as String? ?? '', ), d['fee'] as int, d['debit'] as int, d['credit'] as int, DateTime.parse(d['expiresAt'] as String), d['rateNumerator'] as int, d['rateDenominator'] as int, ); } static OperationDTO operation(Map d) => OperationDTO( d['id'] as String, quote(d['quote'] as Map), DateTime.parse(d['createdAt'] as String), DateTime.parse(d['createdAt'] as String), status: d['status'] as String, ); ChallengeDTO get challenge => ChallengeDTO(data as Map); } class ChallengeDTO { ChallengeDTO(Map d) : id = d['challengeId'] as String, target = d['target'] as String, expires = DateTime.parse(d['expiresAt'] as String), resend = DateTime.parse(d['resendAt'] as String); final String id, target; final DateTime expires, resend; }