using System.ComponentModel;

namespace Connected.Extensions;

public static class EnumExtensions
{
	public static string? ToDescription(this Enum value)
	{
		if (value is null)
			return null;

		var stringValue = value.ToString();

		var attributes = 
			value
			?.GetType()
			?.GetField(stringValue)
			?.GetCustomAttributes(typeof(DescriptionAttribute), false)
			as DescriptionAttribute[];

		attributes ??= Array.Empty<DescriptionAttribute>();

		return attributes.Length > 0
			? attributes[0].Description
			: stringValue.ToLower();
	}
}
