using System;
using System.Collections.Generic;
using Jellyfin.Data.Enums;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Model.Dto;
namespace Jellyfin.Server.Implementations.Item;
///
/// Turns per-type counts into an .
///
internal static class ItemCountBuilder
{
///
/// Builds the counts of one by-name item.
///
/// The item type lookup.
/// The counted items, by type name. A type may repeat.
/// The counts.
public static ItemCounts Build(IItemTypeLookup itemTypeLookup, IEnumerable<(string Type, int Count)> counts)
{
ArgumentNullException.ThrowIfNull(itemTypeLookup);
ArgumentNullException.ThrowIfNull(counts);
var lookup = itemTypeLookup.BaseItemKindNames;
var result = new ItemCounts();
foreach (var (type, count) in counts)
{
// Accumulated rather than assigned: a caller may group by something finer than the
// type and hand the same type over more than once.
if (string.Equals(type, lookup[BaseItemKind.MusicAlbum], StringComparison.Ordinal))
{
result.AlbumCount += count;
}
else if (string.Equals(type, lookup[BaseItemKind.MusicArtist], StringComparison.Ordinal))
{
result.ArtistCount += count;
}
else if (string.Equals(type, lookup[BaseItemKind.Episode], StringComparison.Ordinal))
{
result.EpisodeCount += count;
}
else if (string.Equals(type, lookup[BaseItemKind.Movie], StringComparison.Ordinal))
{
result.MovieCount += count;
}
else if (string.Equals(type, lookup[BaseItemKind.MusicVideo], StringComparison.Ordinal))
{
result.MusicVideoCount += count;
}
else if (string.Equals(type, lookup[BaseItemKind.LiveTvProgram], StringComparison.Ordinal))
{
result.ProgramCount += count;
}
else if (string.Equals(type, lookup[BaseItemKind.Series], StringComparison.Ordinal))
{
result.SeriesCount += count;
}
else if (string.Equals(type, lookup[BaseItemKind.Audio], StringComparison.Ordinal))
{
result.SongCount += count;
}
else if (string.Equals(type, lookup[BaseItemKind.Trailer], StringComparison.Ordinal))
{
result.TrailerCount += count;
}
else if (string.Equals(type, lookup[BaseItemKind.BoxSet], StringComparison.Ordinal))
{
result.BoxSetCount += count;
}
else if (string.Equals(type, lookup[BaseItemKind.Book], StringComparison.Ordinal))
{
result.BookCount += count;
}
}
result.ItemCount = result.TotalItemCount();
return result;
}
///
/// Replaces the episode count, which both by-name paths decide separately from the other
/// types because a genre or studio is usually written on the series rather than its episodes.
///
/// The counts to update.
/// The episode count.
public static void SetEpisodeCount(ItemCounts counts, int episodeCount)
{
ArgumentNullException.ThrowIfNull(counts);
counts.EpisodeCount = episodeCount;
counts.ItemCount = counts.TotalItemCount();
}
}