// 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{}
)

// SponsoredChats represents TL type `sponsoredChats#8cf31e36`.
type SponsoredChats struct {
	// List of sponsored chats
	Chats []SponsoredChat
}

// SponsoredChatsTypeID is TL type id of SponsoredChats.
const SponsoredChatsTypeID = 0x8cf31e36

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

func (s *SponsoredChats) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Chats == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SponsoredChats) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "sponsoredChats",
		ID:   SponsoredChatsTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Chats",
			SchemaName: "chats",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SponsoredChats) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode sponsoredChats#8cf31e36 as nil")
	}
	b.PutID(SponsoredChatsTypeID)
	return s.EncodeBare(b)
}

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

// Decode implements bin.Decoder.
func (s *SponsoredChats) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode sponsoredChats#8cf31e36 to nil")
	}
	if err := b.ConsumeID(SponsoredChatsTypeID); err != nil {
		return fmt.Errorf("unable to decode sponsoredChats#8cf31e36: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SponsoredChats) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode sponsoredChats#8cf31e36 to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode sponsoredChats#8cf31e36: field chats: %w", err)
		}

		if headerLen > 0 {
			s.Chats = make([]SponsoredChat, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value SponsoredChat
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare sponsoredChats#8cf31e36: field chats: %w", err)
			}
			s.Chats = append(s.Chats, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SponsoredChats) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode sponsoredChats#8cf31e36 as nil")
	}
	b.ObjStart()
	b.PutID("sponsoredChats")
	b.Comma()
	b.FieldStart("chats")
	b.ArrStart()
	for idx, v := range s.Chats {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode sponsoredChats#8cf31e36: field chats 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 (s *SponsoredChats) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode sponsoredChats#8cf31e36 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("sponsoredChats"); err != nil {
				return fmt.Errorf("unable to decode sponsoredChats#8cf31e36: %w", err)
			}
		case "chats":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value SponsoredChat
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode sponsoredChats#8cf31e36: field chats: %w", err)
				}
				s.Chats = append(s.Chats, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode sponsoredChats#8cf31e36: field chats: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetChats returns value of Chats field.
func (s *SponsoredChats) GetChats() (value []SponsoredChat) {
	if s == nil {
		return
	}
	return s.Chats
}
