| | | 1 | | using System.Collections.Frozen; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | |
| | | 4 | | namespace Chronicis.Client.Services; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Shared formatting and classification helpers for render definition generation. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class RenderDefinitionHelpers |
| | | 10 | | { |
| | 1 | 11 | | private static readonly FrozenSet<string> HiddenFields = |
| | 1 | 12 | | new HashSet<string>(StringComparer.OrdinalIgnoreCase) |
| | 1 | 13 | | { |
| | 1 | 14 | | "pk", "model", "document", "illustration", "url", "key", "slug", |
| | 1 | 15 | | "hover", "v2_converted_path", "img_main", "document__slug", |
| | 1 | 16 | | "document__title", "document__license_url", "document__url", |
| | 1 | 17 | | "page_no", "spell_list", "environments" |
| | 1 | 18 | | }.ToFrozenSet(StringComparer.OrdinalIgnoreCase); |
| | | 19 | | |
| | 1 | 20 | | public static readonly string[] TitleCandidates = { "name", "title" }; |
| | | 21 | | |
| | 1 | 22 | | public static readonly string[] AbilitySuffixes = |
| | 1 | 23 | | { "strength", "dexterity", "constitution", "intelligence", "wisdom", "charisma" }; |
| | | 24 | | |
| | 1 | 25 | | public static readonly string[] AbilityLabels = |
| | 1 | 26 | | { "STR", "DEX", "CON", "INT", "WIS", "CHA" }; |
| | | 27 | | |
| | 56 | 28 | | public static bool IsHiddenField(string name) => HiddenFields.Contains(name); |
| | | 29 | | |
| | | 30 | | public static bool IsNullOrEmpty(JsonElement value) |
| | | 31 | | { |
| | 139 | 32 | | return value.ValueKind switch |
| | 139 | 33 | | { |
| | 2 | 34 | | JsonValueKind.Null or JsonValueKind.Undefined => true, |
| | 107 | 35 | | JsonValueKind.String => string.IsNullOrWhiteSpace(value.GetString()) || |
| | 107 | 36 | | value.GetString() == "—" || value.GetString() == "-", |
| | 6 | 37 | | JsonValueKind.Array => value.GetArrayLength() == 0, |
| | 24 | 38 | | _ => false |
| | 139 | 39 | | }; |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | public static bool IsDescriptionField(string name) => |
| | 31 | 43 | | name.Contains("description", StringComparison.OrdinalIgnoreCase) || |
| | 31 | 44 | | name.Contains("desc", StringComparison.OrdinalIgnoreCase); |
| | | 45 | | |
| | | 46 | | public static string FormatFieldName(string name) |
| | | 47 | | { |
| | 73 | 48 | | return string.Join(' ', name |
| | 73 | 49 | | .Split('_') |
| | 73 | 50 | | .Where(s => !string.IsNullOrEmpty(s)) |
| | 73 | 51 | | .Select(s => char.ToUpperInvariant(s[0]) + s[1..])); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | public static string FormatGroupLabel(string prefix) |
| | | 55 | | { |
| | 25 | 56 | | var label = FormatFieldName(prefix); |
| | 25 | 57 | | if (!label.EndsWith('s') && !label.EndsWith("es")) |
| | 22 | 58 | | label += "s"; |
| | 25 | 59 | | return label; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | public static string StripPrefix(string name, string prefix) |
| | | 63 | | { |
| | 54 | 64 | | if (name.StartsWith(prefix + "_", StringComparison.OrdinalIgnoreCase)) |
| | 53 | 65 | | return name[(prefix.Length + 1)..]; |
| | 1 | 66 | | return name; |
| | | 67 | | } |
| | | 68 | | } |