using System;
using System.Collections.Generic;
using Jellyfin.Extensions;
namespace MediaBrowser.Model.Extensions;
/// 
/// Defines the  class.
/// 
public static class ContainerHelper
{
    /// 
    /// Compares two containers, returning true if an item in  exists
    /// in .
    /// 
    /// The comma-delimited string being searched.
    /// If the parameter begins with the - character, the operation is reversed.
    /// If the parameter is empty or null, all containers in  will be accepted.
    /// The comma-delimited string being matched.
    /// The result of the operation.
    public static bool ContainsContainer(string? profileContainers, string? inputContainer)
    {
        var isNegativeList = false;
        if (profileContainers is not null && profileContainers.StartsWith('-'))
        {
            isNegativeList = true;
            profileContainers = profileContainers[1..];
        }
        return ContainsContainer(profileContainers, isNegativeList, inputContainer);
    }
    /// 
    /// Compares two containers, returning true if an item in  exists
    /// in .
    /// 
    /// The comma-delimited string being searched.
    /// If the parameter begins with the - character, the operation is reversed.
    /// If the parameter is empty or null, all containers in  will be accepted.
    /// The comma-delimited string being matched.
    /// The result of the operation.
    public static bool ContainsContainer(string? profileContainers, ReadOnlySpan inputContainer)
    {
        var isNegativeList = false;
        if (profileContainers is not null && profileContainers.StartsWith('-'))
        {
            isNegativeList = true;
            profileContainers = profileContainers[1..];
        }
        return ContainsContainer(profileContainers, isNegativeList, inputContainer);
    }
    /// 
    /// Compares two containers, returning  if an item in 
    /// does not exist in .
    /// 
    /// The comma-delimited string being searched.
    /// If the parameter is empty or null, all containers in  will be accepted.
    /// The boolean result to return if a match is not found.
    /// The comma-delimited string being matched.
    /// The result of the operation.
    public static bool ContainsContainer(string? profileContainers, bool isNegativeList, string? inputContainer)
    {
        if (string.IsNullOrEmpty(inputContainer))
        {
            return isNegativeList;
        }
        return ContainsContainer(profileContainers, isNegativeList, inputContainer.AsSpan());
    }
    /// 
    /// Compares two containers, returning  if an item in 
    /// does not exist in .
    /// 
    /// The comma-delimited string being searched.
    /// If the parameter is empty or null, all containers in  will be accepted.
    /// The boolean result to return if a match is not found.
    /// The comma-delimited string being matched.
    /// The result of the operation.
    public static bool ContainsContainer(string? profileContainers, bool isNegativeList, ReadOnlySpan inputContainer)
    {
        if (string.IsNullOrEmpty(profileContainers))
        {
            // Empty profiles always support all containers/codecs.
            return true;
        }
        var allInputContainers = inputContainer.Split(',');
        var allProfileContainers = profileContainers.SpanSplit(',');
        foreach (var container in allInputContainers)
        {
            if (!container.IsEmpty)
            {
                foreach (var profile in allProfileContainers)
                {
                    if (!profile.IsEmpty && container.Equals(profile, StringComparison.OrdinalIgnoreCase))
                    {
                        return !isNegativeList;
                    }
                }
            }
        }
        return isNegativeList;
    }
    /// 
    /// Compares two containers, returning  if an item in 
    /// does not exist in .
    /// 
    /// The profile containers being matched searched.
    /// If the parameter is empty or null, all containers in  will be accepted.
    /// The boolean result to return if a match is not found.
    /// The comma-delimited string being matched.
    /// The result of the operation.
    public static bool ContainsContainer(IReadOnlyList? profileContainers, bool isNegativeList, string inputContainer)
    {
        if (profileContainers is null)
        {
            // Empty profiles always support all containers/codecs.
            return true;
        }
        var allInputContainers = Split(inputContainer);
        foreach (var container in allInputContainers)
        {
            foreach (var profile in profileContainers)
            {
                if (string.Equals(profile, container, StringComparison.OrdinalIgnoreCase))
                {
                    return !isNegativeList;
                }
            }
        }
        return isNegativeList;
    }
    /// 
    /// Splits and input string.
    /// 
    /// The input string.
    /// The result of the operation.
    public static string[] Split(string? input)
    {
        return input?.Split(',', StringSplitOptions.RemoveEmptyEntries) ?? [];
    }
}