| | | 1 | | using System.Text.Json; |
| | | 2 | | using Chronicis.Client.Models; |
| | | 3 | | |
| | | 4 | | namespace Chronicis.Client.Services; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Generates a starter RenderDefinition from a sample JSON record. |
| | | 8 | | /// Uses heuristics to group fields, detect ability scores, and choose render hints. |
| | | 9 | | /// The output is a starting point for manual refinement. |
| | | 10 | | /// </summary> |
| | | 11 | | public static class RenderDefinitionGeneratorService |
| | | 12 | | { |
| | | 13 | | public static RenderDefinition Generate(JsonElement sample) |
| | | 14 | | { |
| | 10 | 15 | | var dataSource = UnwrapFields(sample); |
| | 10 | 16 | | if (dataSource.ValueKind != JsonValueKind.Object) |
| | 1 | 17 | | return CreateMinimal(); |
| | | 18 | | |
| | 9 | 19 | | var (titleField, hidden, remaining) = FieldClassifier.Classify(dataSource); |
| | 9 | 20 | | var prefixGroups = PrefixGroupDetector.DetectGroups(remaining); |
| | 9 | 21 | | var sections = SectionBuilder.Build(remaining, prefixGroups); |
| | | 22 | | |
| | 9 | 23 | | return new RenderDefinition |
| | 9 | 24 | | { |
| | 9 | 25 | | TitleField = titleField, |
| | 9 | 26 | | CatchAll = true, |
| | 9 | 27 | | Hidden = hidden, |
| | 9 | 28 | | Sections = sections |
| | 9 | 29 | | }; |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | private static JsonElement UnwrapFields(JsonElement sample) |
| | | 33 | | { |
| | 10 | 34 | | if (sample.ValueKind == JsonValueKind.Object && |
| | 10 | 35 | | sample.TryGetProperty("fields", out var fields) && |
| | 10 | 36 | | fields.ValueKind == JsonValueKind.Object) |
| | | 37 | | { |
| | 1 | 38 | | return fields; |
| | | 39 | | } |
| | 9 | 40 | | return sample; |
| | | 41 | | } |
| | | 42 | | |
| | 1 | 43 | | private static RenderDefinition CreateMinimal() => new() |
| | 1 | 44 | | { |
| | 1 | 45 | | CatchAll = true, |
| | 1 | 46 | | Sections = new List<RenderSection>() |
| | 1 | 47 | | }; |
| | | 48 | | } |