{{- /*gotype: go.mau.fi/mautrix-telegram/pkg/gotd/gen.structDef*/ -}}
{{ define "decode_tdlib_json" }}{{ $s := . }}
// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func ({{ $s.Receiver }} *{{ $s.Name }}) DecodeTDLibJSON({{ $s.BufArg }} tdjson.Decoder) error {
    if {{ $s.Receiver }} == nil {
        return fmt.Errorf("can't decode {{ $s.RawType }} to nil")
    }

    return {{ $s.BufArg }}.Obj(func({{ $s.BufArg }} tdjson.Decoder, key []byte) error {
        switch string(key) {
        case tdjson.TypeField:
            if err := {{ $s.BufArg }}.ConsumeID("{{ $s.RawName }}"); err != nil {
                return fmt.Errorf("unable to decode {{ $s.RawType }}: %w", err)
            }
{{- range $f := $s.Fields }}
        case "{{ $f.RawName }}":
        {{- if or $f.ConditionalBool $f.Conditional }}
            {{ $s.Receiver }}.{{ $f.ConditionalField }}.Set({{ $f.ConditionalIndex }})
        {{- end }}
        {{- if $f.Vector }}
            if err := {{ $s.BufArg }}.Arr(func({{ $s.BufArg }} tdjson.Decoder) error {

            {{- if $f.DoubleVector }}
                var row []{{ $f.Type }}
                if err := {{ $s.BufArg }}.Arr(func({{ $s.BufArg }} tdjson.Decoder) error {
            {{- end }}
            {{- if $f.Interface }}
                value, err := DecodeTDLibJSON{{ $f.InterfaceFunc }}({{ $s.BufArg }})
                if err != nil {
                    return fmt.Errorf("unable to decode {{ $s.RawType }}: field {{ $f.RawName }}: %w", err)
                }
            {{- else if $f.Encoder }}
                var value {{ $f.Type }}
                if err := value.DecodeTDLibJSON({{ $s.BufArg }}); err != nil {
                    return fmt.Errorf("unable to decode {{ $s.RawType }}: field {{ $f.RawName }}: %w", err)
                }
            {{- else}}
                value, err := {{ $s.BufArg }}.{{ $f.Func }}()
                if err != nil {
                    return fmt.Errorf("unable to decode {{ $s.RawType }}: field {{ $f.RawName }}: %w", err)
                }
            {{- end }}
                {{- if $f.DoubleVector }}
                        row = append(row, value)
                        return nil
                    }); err != nil {
                        return fmt.Errorf("unable to decode {{ $s.RawType }}: field {{ $f.RawName }}: %w", err)
                    }
                {{ $s.Receiver }}.{{ $f.Name }} = append({{ $s.Receiver }}.{{ $f.Name }}, row)
                {{- else }}
                {{ $s.Receiver }}.{{ $f.Name }} = append({{ $s.Receiver }}.{{ $f.Name }}, value)
                {{- end }}
                return nil
            }); err != nil {
                return fmt.Errorf("unable to decode {{ $s.RawType }}: field {{ $f.RawName }}: %w", err)
            }
        {{- else }}
        {{- if $f.Interface }}
            value, err := DecodeTDLibJSON{{ $f.InterfaceFunc }}({{ $s.BufArg }})
            if err != nil {
                return fmt.Errorf("unable to decode {{ $s.RawType }}: field {{ $f.RawName }}: %w", err)
            }
            {{ $s.Receiver }}.{{ $f.Name }} = value
        {{- else if $f.Encoder }}
            {{- if not (eq $f.Type "bin.Fields") }}
            if err := {{ $s.Receiver }}.{{ $f.Name }}.DecodeTDLibJSON({{ $s.BufArg }}); err != nil {
                return fmt.Errorf("unable to decode {{ $s.RawType }}: field {{ $f.RawName }}: %w", err)
            }
            {{- end }}
        {{- else }}
            value, err := {{ $s.BufArg }}.{{ $f.Func }}()
            if err != nil {
                return fmt.Errorf("unable to decode {{ $s.RawType }}: field {{ $f.RawName }}: %w", err)
            }
            {{ $s.Receiver }}.{{ $f.Name }} = value
        {{- end }}
        {{- end }}
    {{- end }}
        default:
            return {{ $s.BufArg }}.Skip()
        }
        return nil
    })
}
{{ end }}
