using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
namespace MediaBrowser.Model.Entities
{
///
/// Class ProviderIdsExtensions.
///
public static class ProviderIdsExtensions
{
///
/// Determines whether [has provider identifier] [the specified instance].
///
/// The instance.
/// The provider.
/// true if [has provider identifier] [the specified instance]; otherwise, false.
public static bool HasProviderId(this IHasProviderIds instance, MetadataProvider provider)
{
return !string.IsNullOrEmpty(instance.GetProviderId(provider.ToString()));
}
///
/// Gets a provider id.
///
/// The instance.
/// The provider.
/// System.String.
public static string? GetProviderId(this IHasProviderIds instance, MetadataProvider provider)
{
return instance.GetProviderId(provider.ToString());
}
///
/// Gets a provider id.
///
/// The instance.
/// The name.
/// The provider id.
/// true if a provider id with the given name was found; otherwise false.
public static bool TryGetProviderId(this IHasProviderIds instance, string name, [MaybeNullWhen(false)] out string id)
{
if (instance == null)
{
throw new ArgumentNullException(nameof(instance));
}
if (instance.ProviderIds == null)
{
id = string.Empty;
return false;
}
return instance.ProviderIds.TryGetValue(name, out id);
}
///
/// Gets a provider id.
///
/// The instance.
/// The provider.
/// The provider id.
/// true if a provider id with the given name was found; otherwise false.
public static bool TryGetProviderId(this IHasProviderIds instance, MetadataProvider provider, [MaybeNullWhen(false)] out string id)
{
return instance.TryGetProviderId(provider.ToString(), out id);
}
///
/// Gets a provider id.
///
/// The instance.
/// The name.
/// System.String.
public static string? GetProviderId(this IHasProviderIds instance, string name)
{
instance.TryGetProviderId(name, out string? id);
return id;
}
///
/// Sets a provider id.
///
/// The instance.
/// The name.
/// The value.
public static void SetProviderId(this IHasProviderIds instance, string name, string value)
{
if (instance == null)
{
throw new ArgumentNullException(nameof(instance));
}
// If it's null remove the key from the dictionary
if (string.IsNullOrEmpty(value))
{
if (instance.ProviderIds != null)
{
if (instance.ProviderIds.ContainsKey(name))
{
instance.ProviderIds.Remove(name);
}
}
}
else
{
// Ensure it exists
if (instance.ProviderIds == null)
{
instance.ProviderIds = new Dictionary(StringComparer.OrdinalIgnoreCase);
}
instance.ProviderIds[name] = value;
}
}
///
/// Sets a provider id.
///
/// The instance.
/// The provider.
/// The value.
public static void SetProviderId(this IHasProviderIds instance, MetadataProvider provider, string value)
{
instance.SetProviderId(provider.ToString(), value);
}
}
}