#region ========================================================================= USING ===================================================================================== using Nemera.Contracts.DTO.Common; using System; using System.Collections.Generic; #endregion namespace Nemera.Infrastructure.Common.Metadata; /// /// Provides the shared field-level merge operations used when aggregating the metadata returned by multiple metadata providers. /// internal static class MetadataMerge { /// /// Gets the first non-empty value, otherwise the second value unchanged. /// /// The type of the values. /// The value with priority. /// The fallback value. /// The first value when it is not empty, otherwise the second value unchanged. public static T? First(T? first, T? second) { return first is not null && !IsEmpty(first) ? first : second; } /// /// Returns the union of the two collections, de-duplicated by the key, keeping the items of the first collection on key conflicts. /// /// The type of the collection items. /// The first collection, whose items have priority. /// The second collection. /// The function used to extract the de-duplication key of an item. /// The union of the two collections, or when both are . public static List? Union(List? first, List? second, Func keySelector) where T : class { if (first is null && second is null) return null; Dictionary seen = new(StringComparer.OrdinalIgnoreCase); List result = []; void AddRange(List? items) { foreach (T item in items ?? []) if (seen.TryAdd(keySelector(item) ?? string.Empty, item)) result.Add(item); } AddRange(first); AddRange(second); return result; } /// /// Merges the release information of the two metadata, keeping the first non-null value of each field. /// /// The first release information, whose values have priority. /// The second release information, filling the fields that the first one lacks. /// The merged release information, or when both are . public static ReleaseInfoDto? MergeReleaseInfo(ReleaseInfoDto? first, ReleaseInfoDto? second) { if (first is null && second is null) return null; DateOnly? originalReleaseDate = First(first?.OriginalReleaseDate, second?.OriginalReleaseDate); int? originalReleaseYear = First(first?.OriginalReleaseYear, second?.OriginalReleaseYear); DateOnly? reReleaseDate = First(first?.ReReleaseDate, second?.ReReleaseDate); int? reReleaseYear = First(first?.ReReleaseYear, second?.ReReleaseYear); // A release date and its year describe the same point in time, so the year is derived from the date whenever a date is known. Without this, // a year from one provider could be combined with a date from another (for example a release year from the tags with the original release // date from MusicBrainz), and the resulting inconsistent pair would be rejected when the metadata is applied. if (originalReleaseDate is not null) originalReleaseYear = originalReleaseDate.Value.Year; if (reReleaseDate is not null) reReleaseYear = reReleaseDate.Value.Year; return new ReleaseInfoDto( originalReleaseDate, originalReleaseYear, reReleaseDate, reReleaseYear, First(first?.ReleaseCountry, second?.ReleaseCountry), First(first?.ReleaseVersion, second?.ReleaseVersion)); } /// /// Determines whether the provided value is an empty string. /// /// The type of the value. /// The value to check. /// when the value is an empty string, otherwise . private static bool IsEmpty(T? value) { return value is string text && string.IsNullOrWhiteSpace(text); } }