import Foundation import HydraulophoneCore struct OfflineLibraryCollection: Hashable, Identifiable { enum Kind: Hashable { case album case artist } let id: String let kind: Kind let title: String let subtitle: String? let thumbnailURL: URL? let songs: [Song] } enum OfflineLibraryTracks { /// Merges complete streaming-cache entries with explicit downloads. /// Download metadata wins for duplicate IDs because it may include local artwork. static func songs(cached: [CachedTrack], downloaded: [Song]) -> [Song] { var songs = cached.map(\.song) var indices = Dictionary(uniqueKeysWithValues: songs.enumerated().map { ($1.id, $0) }) for song in downloaded { if let index = indices[song.id] { songs[index] = song } else { indices[song.id] = songs.count songs.append(song) } } return songs } static func albums(from songs: [Song]) -> [OfflineLibraryCollection] { var groups: [OfflineLibraryCollection] = [] var indices: [String: Int] = [:] for song in songs { let reference = song.albumReference let title = reference?.title ?? song.album guard let title, !title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { continue } let artists = reference?.artistReferences ?? song.artistReferences ?? [] let artistKey = artists.map(\.browseID).joined(separator: ",") let key = reference.map { "album:\($0.browseID)" } ?? "album-local:\(normalized(title)):\(artistKey)" if let index = indices[key] { groups[index] = replacingSongs(in: groups[index], appending: song) } else { indices[key] = groups.count groups.append( OfflineLibraryCollection( id: key, kind: .album, title: title, subtitle: reference?.subtitle ?? artists.map(\.title).joined(separator: ", ") .nilIfEmpty ?? song.artist.nilIfEmpty, thumbnailURL: reference?.thumbnailURL ?? song.thumbnailURL, songs: [song] )) } } return groups } static func artists(from songs: [Song]) -> [OfflineLibraryCollection] { var groups: [OfflineLibraryCollection] = [] var indices: [String: Int] = [:] for song in songs { let references = song.artistReferences ?? [] let artists: [(String, String, URL?)] = references.isEmpty ? [("artist-local:\(normalized(song.artist))", song.artist, song.thumbnailURL)] : references.map { ("artist:\($0.browseID)", $0.title, $0.thumbnailURL) } for (key, title, artwork) in artists { guard !title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { continue } if let index = indices[key] { groups[index] = replacingSongs(in: groups[index], appending: song) } else { indices[key] = groups.count groups.append( OfflineLibraryCollection( id: key, kind: .artist, title: title, subtitle: nil, thumbnailURL: artwork ?? song.thumbnailURL, songs: [song] )) } } } return groups } private static func replacingSongs( in collection: OfflineLibraryCollection, appending song: Song ) -> OfflineLibraryCollection { OfflineLibraryCollection( id: collection.id, kind: collection.kind, title: collection.title, subtitle: collection.subtitle, thumbnailURL: collection.thumbnailURL, songs: collection.songs + [song] ) } private static func normalized(_ value: String) -> String { value.trimmingCharacters(in: .whitespacesAndNewlines).folding( options: [.caseInsensitive, .diacriticInsensitive], locale: .current) } } extension String { fileprivate var nilIfEmpty: String? { isEmpty ? nil : self } }