import 'dart:convert'; import 'package:yumi/shared/business_logic/models/res/login_res.dart'; const String scChatBubbleCloudDataKey = 'scChatBubble'; const String scChatBubbleMessageExtensionKey = scChatBubbleCloudDataKey; String? scEncodeChatBubbleCloudCustomData(PropsResources? chatBubble) { final snapshot = scChatBubbleSnapshotJson(chatBubble); if (snapshot == null) { return null; } return jsonEncode({scChatBubbleCloudDataKey: snapshot}); } String? scEncodeChatBubbleSnapshotValue(PropsResources? chatBubble) { final snapshot = scChatBubbleSnapshotJson(chatBubble); return snapshot == null ? null : jsonEncode(snapshot); } PropsResources? scDecodeChatBubbleFromCloudCustomData(String? cloudCustomData) { final raw = cloudCustomData?.trim(); if (raw == null || raw.isEmpty) { return null; } try { final decoded = jsonDecode(raw); if (decoded is! Map) { return null; } final data = decoded[scChatBubbleCloudDataKey]; if (data is Map) { return PropsResources.fromJson(data); } } catch (_) {} return null; } PropsResources? scDecodeChatBubbleSnapshotValue(String? snapshotValue) { final raw = snapshotValue?.trim(); if (raw == null || raw.isEmpty) { return null; } try { final decoded = jsonDecode(raw); if (decoded is Map) { return PropsResources.fromJson(decoded); } } catch (_) {} return null; } Map? scChatBubbleSnapshotJson(PropsResources? resource) { if (!_hasChatBubbleImage(resource)) { return null; } final map = {}; void put(String key, String? value) { final text = value?.trim(); if (text != null && text.isNotEmpty) { map[key] = text; } } put('id', resource?.id); put('name', resource?.name); put('type', resource?.type); put('cover', resource?.cover); put('expand', resource?.expand); put('sourceUrl', resource?.sourceUrl); put('imSendCoverUrl', resource?.imSendCoverUrl); put('imOpenedUrl', resource?.imOpenedUrl); put('imOpenedUrlTwo', resource?.imOpenedUrlTwo); put('imNotOpenedUrl', resource?.imNotOpenedUrl); put('roomSendCoverUrl', resource?.roomSendCoverUrl); put('roomOpenedUrl', resource?.roomOpenedUrl); put('roomNotOpenedUrl', resource?.roomNotOpenedUrl); return map; } bool _hasChatBubbleImage(PropsResources? resource) { if (resource == null) { return false; } return [ resource.imSendCoverUrl, resource.imOpenedUrl, resource.imOpenedUrlTwo, resource.imNotOpenedUrl, resource.roomSendCoverUrl, resource.roomOpenedUrl, resource.roomNotOpenedUrl, resource.cover, resource.expand, resource.sourceUrl, ].any((value) => (value?.trim() ?? '').isNotEmpty); }