import '../../../api_response.dart';
import '../chat/get_chat_response.dart';

/// Response of Talk's `GET /chat/{token}/share/overview`: the latest shared
/// items grouped by object type (`media`, `file`, `voice`, `audio`, `location`,
/// `recording`, `deckcard`, `other`). Reuses [GetChatResponseObject] for the
/// message structure.
class GetSharedItemsOverviewResponse extends ApiResponse {
  final Map<String, List<GetChatResponseObject>> itemsByType;

  GetSharedItemsOverviewResponse(this.itemsByType);

  factory GetSharedItemsOverviewResponse.fromOcs(Map<String, dynamic> ocs) {
    final data = ocs['data'];
    final result = <String, List<GetChatResponseObject>>{};
    if (data is Map<String, dynamic>) {
      for (final entry in data.entries) {
        final value = entry.value;
        final raw = switch (value) {
          List<dynamic> list => list,
          Map<dynamic, dynamic> map => map.values,
          _ => const <dynamic>[],
        };
        result[entry.key] = raw
            .whereType<Map<String, dynamic>>()
            .map(GetChatResponseObject.fromJson)
            .toList();
      }
    }
    return GetSharedItemsOverviewResponse(result);
  }
}
