using AIStudio.Tools.PluginSystem.Assistants.Icons;
namespace AIStudio.Tools.PluginSystem.Assistants.DataModel;
internal static class AssistantComponentPropHelper
{
///
/// The prefix with which an assistant plugin names a Lucide icon.
///
private const string LUCIDE_PREFIX = "lucide:";
public static string ReadString(Dictionary props, string key)
{
if (props.TryGetValue(key, out var value))
return value.ToString() ?? string.Empty;
return string.Empty;
}
public static void WriteString(Dictionary props, string key, string value) => props[key] = value;
public static int ReadInt(Dictionary props, string key, int fallback = 0)
{
return props.TryGetValue(key, out var value) && int.TryParse(value.ToString(), out var i) ? i : fallback;
}
public static void WriteInt(Dictionary props, string key, int value) => props[key] = value;
public static int? ReadNullableInt(Dictionary props, string key)
{
return props.TryGetValue(key, out var value) && int.TryParse(value.ToString(), out var i) ? i : null;
}
public static void WriteNullableInt(Dictionary props, string key, int? value)
{
if (value.HasValue)
props[key] = value.Value;
else
props.Remove(key);
}
public static bool ReadBool(Dictionary props, string key, bool fallback = false)
{
return props.TryGetValue(key, out var value) && bool.TryParse(value.ToString(), out var b) ? b : fallback;
}
public static void WriteBool(Dictionary props, string key, bool value) => props[key] = value;
public static void WriteObject(Dictionary props, string key, object? value)
{
if (value is null)
props.Remove(key);
else
props[key] = value;
}
public static Color GetColor(string value, Color fallback) => Enum.TryParse(value, out var color) ? color : fallback;
public static Variant GetVariant(string value, Variant fallback) => Enum.TryParse(value, out var variant) ? variant : fallback;
public static Adornment GetAdornment(string value, Adornment fallback) => Enum.TryParse(value, out var adornment) ? adornment : fallback;
///
/// Finds the icon an assistant plugin names: a Lucide icon as "lucide:" followed by its name on lucide.dev,
/// such as "lucide:house", or an icon of MudBlazor, such as "Icons.Material.Filled.Home".
///
/// The icon as the plugin names it.
/// The icon in the form MudBlazor renders, or an empty string for an unknown name.
public static string GetIcon(string value)
{
var name = value.TrimStart('@');
if (name.StartsWith(LUCIDE_PREFIX, StringComparison.Ordinal))
return LucideIconRegistry.TryGet(name[LUCIDE_PREFIX.Length..], out var lucideIcon) ? lucideIcon : string.Empty;
return MudBlazorIconRegistry.TryGetSvg(name, out var svg) ? svg : string.Empty;
}
public static Size GetComponentSize(string value, Size fallback) => Enum.TryParse(value, out var size) ? size : fallback;
public static Justify? GetJustify(string value) => Enum.TryParse(value, out var justify) ? justify : null;
public static AlignItems? GetItemsAlignment(string value) => Enum.TryParse(value, out var alignment) ? alignment : null;
public static Align GetAlignment(string value, Align fallback = Align.Inherit) => Enum.TryParse(value, out var alignment) ? alignment : fallback;
public static Typo GetTypography(string value, Typo fallback = Typo.body1) => Enum.TryParse(value, out var typo) ? typo : fallback;
public static Wrap? GetWrap(string value) => Enum.TryParse(value, out var wrap) ? wrap : null;
public static StretchItems? GetStretching(string value) => Enum.TryParse(value, out var stretch) ? stretch : null;
public static Breakpoint GetBreakpoint(string value, Breakpoint fallback) => Enum.TryParse(value, out var breakpoint) ? breakpoint : fallback;
public static PickerVariant GetPickerVariant(string pickerValue, PickerVariant fallback) => Enum.TryParse(pickerValue, out var variant) ? variant : fallback;
}