using System.Text;
using AIStudio.Components;
namespace AIStudio.Tools;
///
/// Routes process step enums to their corresponding text representations, when possible.
///
public static class ProcessStepTextRouter
{
///
/// Gets the text representation of a given process step enum.
///
///
/// Gets the text representation of a given process step enum.
/// When the enum type has a specific extension method for text retrieval, it uses that;
/// otherwise, it derives a name based on the enum value.
///
/// The process step enum value.
/// The enum type representing the process steps.
/// The text representation of the process step.
public static string GetText(T step) where T : struct, Enum => step switch
{
ReadWebContentSteps x => x.GetText(),
_ => DeriveName(step)
};
///
/// Derives a name from the enum value by converting it to a more human-readable format.
/// It handles both single-word and multi-word enum values (separated by underscores).
///
/// The enum value to derive the name from.
/// The enum type.
/// A human-readable name derived from the enum value.
private static string DeriveName(T value) where T : struct, Enum
{
var text = value.ToString();
if (!text.Contains('_'))
{
text = text.ToLowerInvariant();
text = char.ToUpperInvariant(text[0]) + text[1..];
}
else
{
var parts = text.Split('_');
var sb = new StringBuilder();
foreach (var part in parts)
{
sb.Append(char.ToUpperInvariant(part[0]));
sb.Append(part[1..].ToLowerInvariant());
}
text = sb.ToString();
}
return text;
}
}