namespace MyOffice.Core.Extensions;

using Microsoft.Extensions.Logging;

public static class StringExtensions
{
	public static bool IsMissing(this string? str)
	{
		return str == null || string.IsNullOrEmpty(str) || string.IsNullOrEmpty(str.Trim());
	}

	public static bool IsPresent(this string? str)
	{
		return !IsMissing(str);
	}

	public static string? NullIfEmpty(this string? str)
	{
		return IsMissing(str) ? null : str;
	}

	public static bool EqualsIgnoreCase(this string? str, string? value)
	{
		return str != null && str.Equals(value, StringComparison.OrdinalIgnoreCase);
	}

	public static bool AsBool(this string? str, bool defaultValue = false)
	{
		return true.ToString().Equals(str, StringComparison.OrdinalIgnoreCase) || defaultValue;
	}

	public static Guid AsGuid(this string str)
	{
		/*str = str
			.Replace("_", "/")
			.Replace("-", "+");
		byte[] buffer = Convert.FromBase64String(str + "==");
		return new Guid(buffer);*/
		return Guid.Parse(str);
	}

	public static Guid? AsGuidNull(this string str)
	{
		/*str = str
			.Replace("_", "/")
			.Replace("-", "+");
		byte[] buffer = Convert.FromBase64String(str + "==");
		return new Guid(buffer);*/
		if (Guid.TryParse(str, out var guid))
		{
			return guid;
		}
		return null;
	}
}