namespace Common.Extensions;

/// <summary>
/// Extensions for the string object type
/// </summary>
public static class StringExtensions
{
  /// <summary>
  /// Determines whether this string contains the specified string. Not case sensitive.
  /// </summary>
  /// <param name="source">   The source.</param>
  /// <param name="contained">The contained.</param>
  public static bool ContainsIgnoreCase(this string source, string contained)
  {
    return source?.IndexOf(contained, StringComparison.InvariantCultureIgnoreCase) >= 0;
  }

  /// <summary>
  /// Expands a path containing a wildcard pattern
  /// </summary>
  /// <param name="self"></param>
  /// <returns></returns>
  public static IEnumerable<string> ExpandPath(this string self)
  {
    var parts = self.Split(Path.DirectorySeparatorChar);

    var fileName = parts.Last();
    if (fileName.Contains('*') || fileName.Contains('?'))
    {
      // Path contains file pattern

      var path = Path.Combine(parts.SkipLast(1).ToArray());
      return Directory.EnumerateFiles(path, fileName);
    }

    // Path contains no pattern
    return new[] { self };
  }
}
