using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;
namespace Emby.Naming.Video;
///
/// Regex based rule for file stacking (eg. disc1, disc2).
///
public class FileStackRule
{
private readonly Regex _tokenRegex;
///
/// Initializes a new instance of the class.
///
/// Token.
/// Whether the file stack rule uses numerical or alphabetical numbering.
public FileStackRule(string token, bool isNumerical)
{
_tokenRegex = new Regex(token, RegexOptions.IgnoreCase | RegexOptions.Compiled);
IsNumerical = isNumerical;
}
///
/// Gets a value indicating whether the rule uses numerical or alphabetical numbering.
///
public bool IsNumerical { get; }
///
/// Match the input against the rule regex.
///
/// The input.
/// The part type and number or null.
/// A value indicating whether the input matched the rule.
public bool Match(string input, [NotNullWhen(true)] out (string StackName, string PartType, string PartNumber)? result)
{
result = null;
var match = _tokenRegex.Match(input);
if (!match.Success)
{
return false;
}
var partType = match.Groups["parttype"].Success ? match.Groups["parttype"].Value : "unknown";
result = (match.Groups["filename"].Value, partType, match.Groups["number"].Value);
return true;
}
}