using System.Globalization;

namespace salesbook.Shared.Core.Utility;

public static class UtilityString
{
    public static string ExtractInitials(string? fullname)
    {
        if (fullname == null) return "";
        
        return string.Concat(fullname
            .Split(' ', StringSplitOptions.RemoveEmptyEntries)
            .Take(3)
            .Select(word => char.ToUpper(word[0])));
    }

    public static string FirstCharToUpper(this string input) =>
        input switch
        {
            null => throw new ArgumentNullException(nameof(input)),
            "" => throw new ArgumentException($"{nameof(input)} cannot be empty", nameof(input)),
            _ => input[0].ToString().ToUpper() + input[1..]
        };

    public static (string Upper, string Lower, string SentenceCase, string TitleCase) FormatString(string input)
    {
        if (string.IsNullOrWhiteSpace(input))
            return (string.Empty, string.Empty, string.Empty, string.Empty);

        var upper = input.ToUpper();
        var lower = input.ToLower();
        
        var sentenceCase = char.ToUpper(lower[0]) + lower[1..];
        
        var textInfo = CultureInfo.CurrentCulture.TextInfo;
        var titleCase = textInfo.ToTitleCase(lower);

        return (upper, lower, sentenceCase, titleCase);
    }
}