namespace adas_core.Domain.Utils;

/// <summary>
/// Provides static extension methods for the <see cref="string"/> type.
/// </summary>
public static class StringEx
{
    /// <summary>
    /// Determines whether the specified string is <c>null</c> or an empty string.
    /// </summary>
    /// <param name="source">The string to evaluate.</param>
    /// <returns><c>true</c> if <paramref name="source"/> is <c>null</c> or an empty string; otherwise, <c>false</c>.</returns>
    public static bool IsEmpty(this string source)
        {
            return string.IsNullOrEmpty(source);
        }

    /// <summary>
    /// Determines whether the specified string is null, empty, or consists only of white-space characters.
    /// </summary>
    /// <param name="source">The string to evaluate.</param>
    /// <returns>true if the string is null, empty, or contains only white space; otherwise, false.</returns>
    public static bool IsEmptyOrWhiteSpace(this string source)
        {
            return string.IsNullOrWhiteSpace(source);
        }

    /// <summary>
    /// Returns the portion of the source string that follows the first occurrence of the specified value, using ordinal (case-sensitive, culture-insensitive) comparison. If the source or value is null or empty, or the value is not found within the source, the original source is returned unchanged.
    /// </summary>
    /// <param name="source">The string to search within.</param>
    /// <param name="value">The delimiter whose first occurrence marks the start of the returned substring.</param>
    /// <returns>The substring after the first occurrence of <paramref name="value"/>; otherwise, the original <paramref name="source"/>.</returns>
    public static string SubstringAfter(this string source, string value)
        {
            if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(value)) return source;
    
            var index = source.IndexOf(value, StringComparison.Ordinal);
            return index >= 0
                ? source.Substring(index + value.Length)
                : source;
        }

    /// <summary>
    /// Returns the portion of <paramref name="source"/> that precedes the first occurrence of <paramref name="value"/>, using ordinal comparison.
    /// If either <paramref name="source"/> or <paramref name="value"/> is null or empty, or if <paramref name="value"/> is not found within <paramref name="source"/>, the original <paramref name="source"/> is returned unchanged.
    /// </summary>
    /// <param name="source">The string to extract the substring from.</param>
    /// <param name="value">The delimiter whose first occurrence marks the end of the returned substring.</param>
    /// <returns>The substring of <paramref name="source"/> before the first occurrence of <paramref name="value"/>, or the original <paramref name="source"/> when no match is found or when either input is null or empty.</returns>
    public static string SubstringBefore(this string source, string value)
        {
            if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(value)) return source;
    
            var index = source.IndexOf(value, StringComparison.Ordinal);
            return index >= 0
                ? source.Substring(0, index)
                : source;
        }
}