// <auto-generated>
// This file was generated by BoltFFI. Do not edit.
// </auto-generated>
#nullable enable

using System;
{%- if enumeration.has_string_fields() %}
using System.Text;
{%- endif %}

namespace {{ namespace }}
{
{{ self::summary_doc_block(enumeration.summary_doc, "    ") }}    public abstract record {{ enumeration.class_name }}
    {
        private {{ enumeration.class_name }}() { }
{%- for variant in enumeration.variants %}
{%- if variant.is_unit() %}
{{ self::summary_doc_block(variant.summary_doc, "        ") }}{{ self::param_doc_block(variant.fields, "        ") }}        public sealed record {{ variant.name }}() : {{ enumeration.class_name }};
{%- else %}
{{ self::summary_doc_block(variant.summary_doc, "        ") }}{{ self::param_doc_block(variant.fields, "        ") }}        public sealed record {{ variant.name }}(
{%- for field in variant.fields %}
            {{ field.csharp_type }} {{ field.name }}{% if !loop.last %},{% endif %}
{%- endfor %}
        ) : {{ enumeration.class_name }};
{%- endif %}
{%- endfor %}

        internal static {{ enumeration.class_name }} Decode(WireReader reader) =>
            reader.ReadI32() switch
            {
{%- for variant in enumeration.variants %}
{%- if variant.is_unit() %}
                {{ variant.tag }} => new {{ variant.name }}(),
{%- else %}
                {{ variant.tag }} => new {{ variant.name }}(
{%- for field in variant.fields %}
                    {{ field.wire_decode_expr }}{% if !loop.last %},{% endif %}
{%- endfor %}
                ),
{%- endif %}
{%- endfor %}
                int tag => throw new InvalidOperationException($"Unknown {{ enumeration.class_name }} tag: {tag}"),
            };

        internal int WireEncodedSize() => 4 + this switch
        {
{%- for variant in enumeration.variants %}
{%- if variant.is_unit() %}
            {{ variant.name }} => 0,
{%- else %}
            {{ variant.name }} _v => {% for field in variant.fields %}{{ field.wire_size_expr }}{% if !loop.last %} + {% endif %}{% endfor %},
{%- endif %}
{%- endfor %}
            _ => throw new InvalidOperationException($"Unknown {{ enumeration.class_name }} variant"),
        };

        internal void WireEncodeTo(WireWriter wire)
        {
            switch (this)
            {
{%- for variant in enumeration.variants %}
{%- if variant.is_unit() %}
                case {{ variant.name }}:
                    wire.WriteI32({{ variant.tag }});
                    break;
{%- else %}
                case {{ variant.name }} _v:
                    wire.WriteI32({{ variant.tag }});
{%- for field in variant.fields %}
{%- for stmt in field.wire_encode_stmts %}
                    {{ stmt }};
{%- endfor %}
{%- endfor %}
                    break;
{%- endif %}
{%- endfor %}
            }
        }
{%- for method in enumeration.methods %}
{% include "render_csharp/value_type_method.txt" %}
{%- endfor %}
    }
{%- if enumeration.is_error %}

{%- let wrapped_name = enumeration.class_name %}
{%- let message_expr = enumeration.exception_message_expr() %}
{% include "render_csharp/typed_exception.txt" %}
{%- endif %}
}
