{{- /*gotype: go.mau.fi/mautrix-telegram/pkg/gotd/gen.structDef*/ -}}
{{ define "decode" }}{{ $s := . }}
// Decode implements bin.Decoder.
func ({{ $s.Receiver }} *{{ $s.Name }}) Decode({{ $s.BufArg }} *bin.Buffer) error {
    if {{ $s.Receiver }} == nil {
        return fmt.Errorf("can't decode {{ $s.RawType }} to nil")
    }
{{ if not $s.Vector -}}
    if err := {{ $s.BufArg }}.ConsumeID({{ $s.Name }}TypeID); err != nil {
        return fmt.Errorf("unable to decode {{ $s.RawType }}: %w", err)
    }
{{- end }}
    return {{ $s.Receiver }}.DecodeBare({{ $s.BufArg }})
}

// DecodeBare implements bin.BareDecoder.
func ({{ $s.Receiver }} *{{ $s.Name }}) DecodeBare({{ $s.BufArg }} *bin.Buffer) error {
    if {{ $s.Receiver }} == nil {
        return fmt.Errorf("can't decode {{ $s.RawType }} to nil")
    }
{{- range $f := $s.Fields }}
    {{- if $f.ConditionalBool }}
        {{ $s.Receiver }}.{{ $f.Name }} = {{ $s.Receiver }}.{{ $f.ConditionalField }}.Has({{ $f.ConditionalIndex }})
    {{- else }}
        {{- if $f.Conditional}}
            if {{ $s.Receiver }}.{{ $f.ConditionalField }}.Has({{ $f.ConditionalIndex }}) {
        {{- else}}
            {
        {{- end }}
        {{- if $f.Vector }}
            headerLen, err := {{ if $f.BareVector }}{{ $s.BufArg }}.Int(){{ else }}{{ $s.BufArg }}.VectorHeader(){{ end }}
            if err != nil {
                return fmt.Errorf("unable to decode {{ $s.RawType }}: field {{ $f.RawName }}: %w", err)
            }

            if headerLen > 0 {
                {{ $s.Receiver }}.{{ $f.Name }} = make({{ template "print_type" $f }}, 0, headerLen % bin.PreallocateLimit)
            }
            for idx := 0; idx < headerLen; idx++ {

            {{- if $f.DoubleVector }}
                innerLen, err := {{ $s.BufArg }}.VectorHeader()
                if err != nil {
                    return fmt.Errorf("unable to decode {{ $s.RawType }}: field {{ $f.RawName }}: %w", err)
                }

                var row []{{ $f.Type }}
                if innerLen > 0 {
                    row = make([]{{ $f.Type }}, 0, innerLen % bin.PreallocateLimit)
                }
                for innerIndex := 0; innerIndex < innerLen; innerLen++ {
            {{- end }}
            {{- if $f.Interface }}
                value, err := Decode{{ $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.Decode{{ if and $f.BareEncoder $f.BareVector }}Bare{{ end }}({{ $s.BufArg }}); err != nil {
                    return fmt.Errorf("unable to decode{{ if and $f.BareEncoder $f.BareVector }} bare{{ end }} {{ $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)
                }
                {{ $s.Receiver }}.{{ $f.Name }} = append({{ $s.Receiver }}.{{ $f.Name }}, row)
                {{- else }}
                {{ $s.Receiver }}.{{ $f.Name }} = append({{ $s.Receiver }}.{{ $f.Name }}, value)
                {{- end }}
            }
        {{- else }}
        {{- if $f.Interface }}
            value, err := Decode{{ $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 err := {{ $s.Receiver }}.{{ $f.Name }}.Decode({{ $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)
            }
            {{ $s.Receiver }}.{{ $f.Name }} = value
        {{- end }}
        {{- end }}
    }
    {{- end }}
{{- end }}
    return nil
}
{{ end }}
