using System;
using System.Linq;
namespace LunaWolfStudiosEditor.ScriptableSheets.Shared
{
public static class EnumUtility
{
///
/// Returns the first set flag from a valid flagged enum. Returns the default value if no flags are set.
///
public static T FirstFlagOrDefault(this T flaggedEnum) where T : Enum
{
if (!Attribute.IsDefined(typeof(T), typeof(FlagsAttribute)))
{
throw new ArgumentException($"{typeof(T).FullName} is not a flagged enum.");
}
return Enum.GetValues(typeof(T)).Cast().FirstOrDefault(f => (int) (object) f != 0 && flaggedEnum.HasFlag(f));
}
}
}