using System.Text.Json;
namespace Kbx.Shared.ExternalData;
public static class OpenDartCompanyProfileNormalizer
{
    public static KbxCompanyProfileSnapshot Normalize(ReadOnlySpan<byte> payload)
    {
        using var doc=JsonDocument.Parse(payload);
        var r=doc.RootElement;
        return new(
            Required(r,"corp_code"),
            Required(r,"corp_name"),
            Optional(r,"stock_code"),
            Optional(r,"bizr_no"));
    }
    private static string Required(JsonElement e,string name)=>e.TryGetProperty(name,out var p)&&!string.IsNullOrWhiteSpace(p.GetString())?p.GetString()!:throw new InvalidDataException($"OPENDART field missing: {name}");
    private static string? Optional(JsonElement e,string name)=>e.TryGetProperty(name,out var p)?p.GetString():null;
}
