// Code generated by gotdgen, DO NOT EDIT.

package tdapi

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"go.mau.fi/mautrix-telegram/pkg/gotd/bin"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tdjson"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tdp"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tgerr"
)

// No-op definition for keeping imports.
var (
	_ = bin.Buffer{}
	_ = context.Background()
	_ = fmt.Stringer(nil)
	_ = strings.Builder{}
	_ = errors.Is
	_ = multierr.AppendInto
	_ = sort.Ints
	_ = tdp.Format
	_ = tgerr.Error{}
	_ = tdjson.Encoder{}
)

// Updates represents TL type `updates#b9829222`.
type Updates struct {
	// List of updates
	Updates []UpdateClass
}

// UpdatesTypeID is TL type id of Updates.
const UpdatesTypeID = 0xb9829222

// Ensuring interfaces in compile-time for Updates.
var (
	_ bin.Encoder     = &Updates{}
	_ bin.Decoder     = &Updates{}
	_ bin.BareEncoder = &Updates{}
	_ bin.BareDecoder = &Updates{}
)

func (u *Updates) Zero() bool {
	if u == nil {
		return true
	}
	if !(u.Updates == nil) {
		return false
	}

	return true
}

// String implements fmt.Stringer.
func (u *Updates) String() string {
	if u == nil {
		return "Updates(nil)"
	}
	type Alias Updates
	return fmt.Sprintf("Updates%+v", Alias(*u))
}

// TypeID returns type id in TL schema.
//
// See https://core.telegram.org/mtproto/TL-tl#remarks.
func (*Updates) TypeID() uint32 {
	return UpdatesTypeID
}

// TypeName returns name of type in TL schema.
func (*Updates) TypeName() string {
	return "updates"
}

// TypeInfo returns info about TL type.
func (u *Updates) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "updates",
		ID:   UpdatesTypeID,
	}
	if u == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Updates",
			SchemaName: "updates",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (u *Updates) Encode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode updates#b9829222 as nil")
	}
	b.PutID(UpdatesTypeID)
	return u.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (u *Updates) EncodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode updates#b9829222 as nil")
	}
	b.PutInt(len(u.Updates))
	for idx, v := range u.Updates {
		if v == nil {
			return fmt.Errorf("unable to encode updates#b9829222: field updates element with index %d is nil", idx)
		}
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare updates#b9829222: field updates element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (u *Updates) Decode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode updates#b9829222 to nil")
	}
	if err := b.ConsumeID(UpdatesTypeID); err != nil {
		return fmt.Errorf("unable to decode updates#b9829222: %w", err)
	}
	return u.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (u *Updates) DecodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode updates#b9829222 to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode updates#b9829222: field updates: %w", err)
		}

		if headerLen > 0 {
			u.Updates = make([]UpdateClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeUpdate(b)
			if err != nil {
				return fmt.Errorf("unable to decode updates#b9829222: field updates: %w", err)
			}
			u.Updates = append(u.Updates, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (u *Updates) EncodeTDLibJSON(b tdjson.Encoder) error {
	if u == nil {
		return fmt.Errorf("can't encode updates#b9829222 as nil")
	}
	b.ObjStart()
	b.PutID("updates")
	b.Comma()
	b.FieldStart("updates")
	b.ArrStart()
	for idx, v := range u.Updates {
		if v == nil {
			return fmt.Errorf("unable to encode updates#b9829222: field updates element with index %d is nil", idx)
		}
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode updates#b9829222: field updates element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (u *Updates) DecodeTDLibJSON(b tdjson.Decoder) error {
	if u == nil {
		return fmt.Errorf("can't decode updates#b9829222 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("updates"); err != nil {
				return fmt.Errorf("unable to decode updates#b9829222: %w", err)
			}
		case "updates":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := DecodeTDLibJSONUpdate(b)
				if err != nil {
					return fmt.Errorf("unable to decode updates#b9829222: field updates: %w", err)
				}
				u.Updates = append(u.Updates, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode updates#b9829222: field updates: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetUpdates returns value of Updates field.
func (u *Updates) GetUpdates() (value []UpdateClass) {
	if u == nil {
		return
	}
	return u.Updates
}
