// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// ContactsSponsoredPeersEmpty represents TL type `contacts.sponsoredPeersEmpty#ea32b4b1`.
// There are no sponsored peers for this query.
//
// See https://core.telegram.org/constructor/contacts.sponsoredPeersEmpty for reference.
type ContactsSponsoredPeersEmpty struct {
}

// ContactsSponsoredPeersEmptyTypeID is TL type id of ContactsSponsoredPeersEmpty.
const ContactsSponsoredPeersEmptyTypeID = 0xea32b4b1

// construct implements constructor of ContactsSponsoredPeersClass.
func (s ContactsSponsoredPeersEmpty) construct() ContactsSponsoredPeersClass { return &s }

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

	_ ContactsSponsoredPeersClass = &ContactsSponsoredPeersEmpty{}
)

func (s *ContactsSponsoredPeersEmpty) Zero() bool {
	if s == nil {
		return true
	}

	return true
}

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

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

// TypeName returns name of type in TL schema.
func (*ContactsSponsoredPeersEmpty) TypeName() string {
	return "contacts.sponsoredPeersEmpty"
}

// TypeInfo returns info about TL type.
func (s *ContactsSponsoredPeersEmpty) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "contacts.sponsoredPeersEmpty",
		ID:   ContactsSponsoredPeersEmptyTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{}
	return typ
}

// Encode implements bin.Encoder.
func (s *ContactsSponsoredPeersEmpty) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode contacts.sponsoredPeersEmpty#ea32b4b1 as nil")
	}
	b.PutID(ContactsSponsoredPeersEmptyTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *ContactsSponsoredPeersEmpty) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode contacts.sponsoredPeersEmpty#ea32b4b1 as nil")
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *ContactsSponsoredPeersEmpty) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode contacts.sponsoredPeersEmpty#ea32b4b1 to nil")
	}
	if err := b.ConsumeID(ContactsSponsoredPeersEmptyTypeID); err != nil {
		return fmt.Errorf("unable to decode contacts.sponsoredPeersEmpty#ea32b4b1: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *ContactsSponsoredPeersEmpty) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode contacts.sponsoredPeersEmpty#ea32b4b1 to nil")
	}
	return nil
}

// ContactsSponsoredPeers represents TL type `contacts.sponsoredPeers#eb032884`.
// Sponsored peers.
//
// See https://core.telegram.org/constructor/contacts.sponsoredPeers for reference.
type ContactsSponsoredPeers struct {
	// Sponsored peers.
	Peers []SponsoredPeer
	// Info about sponsored chats and channels
	Chats []ChatClass
	// Info about sponsored users
	Users []UserClass
}

// ContactsSponsoredPeersTypeID is TL type id of ContactsSponsoredPeers.
const ContactsSponsoredPeersTypeID = 0xeb032884

// construct implements constructor of ContactsSponsoredPeersClass.
func (s ContactsSponsoredPeers) construct() ContactsSponsoredPeersClass { return &s }

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

	_ ContactsSponsoredPeersClass = &ContactsSponsoredPeers{}
)

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

	return true
}

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

// FillFrom fills ContactsSponsoredPeers from given interface.
func (s *ContactsSponsoredPeers) FillFrom(from interface {
	GetPeers() (value []SponsoredPeer)
	GetChats() (value []ChatClass)
	GetUsers() (value []UserClass)
}) {
	s.Peers = from.GetPeers()
	s.Chats = from.GetChats()
	s.Users = from.GetUsers()
}

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

// TypeName returns name of type in TL schema.
func (*ContactsSponsoredPeers) TypeName() string {
	return "contacts.sponsoredPeers"
}

// TypeInfo returns info about TL type.
func (s *ContactsSponsoredPeers) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "contacts.sponsoredPeers",
		ID:   ContactsSponsoredPeersTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peers",
			SchemaName: "peers",
		},
		{
			Name:       "Chats",
			SchemaName: "chats",
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *ContactsSponsoredPeers) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode contacts.sponsoredPeers#eb032884 as nil")
	}
	b.PutID(ContactsSponsoredPeersTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *ContactsSponsoredPeers) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode contacts.sponsoredPeers#eb032884 as nil")
	}
	b.PutVectorHeader(len(s.Peers))
	for idx, v := range s.Peers {
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode contacts.sponsoredPeers#eb032884: field peers element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(s.Chats))
	for idx, v := range s.Chats {
		if v == nil {
			return fmt.Errorf("unable to encode contacts.sponsoredPeers#eb032884: field chats element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode contacts.sponsoredPeers#eb032884: field chats element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(s.Users))
	for idx, v := range s.Users {
		if v == nil {
			return fmt.Errorf("unable to encode contacts.sponsoredPeers#eb032884: field users element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode contacts.sponsoredPeers#eb032884: field users element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *ContactsSponsoredPeers) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode contacts.sponsoredPeers#eb032884 to nil")
	}
	if err := b.ConsumeID(ContactsSponsoredPeersTypeID); err != nil {
		return fmt.Errorf("unable to decode contacts.sponsoredPeers#eb032884: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *ContactsSponsoredPeers) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode contacts.sponsoredPeers#eb032884 to nil")
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode contacts.sponsoredPeers#eb032884: field peers: %w", err)
		}

		if headerLen > 0 {
			s.Peers = make([]SponsoredPeer, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value SponsoredPeer
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode contacts.sponsoredPeers#eb032884: field peers: %w", err)
			}
			s.Peers = append(s.Peers, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode contacts.sponsoredPeers#eb032884: field chats: %w", err)
		}

		if headerLen > 0 {
			s.Chats = make([]ChatClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeChat(b)
			if err != nil {
				return fmt.Errorf("unable to decode contacts.sponsoredPeers#eb032884: field chats: %w", err)
			}
			s.Chats = append(s.Chats, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode contacts.sponsoredPeers#eb032884: field users: %w", err)
		}

		if headerLen > 0 {
			s.Users = make([]UserClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeUser(b)
			if err != nil {
				return fmt.Errorf("unable to decode contacts.sponsoredPeers#eb032884: field users: %w", err)
			}
			s.Users = append(s.Users, value)
		}
	}
	return nil
}

// GetPeers returns value of Peers field.
func (s *ContactsSponsoredPeers) GetPeers() (value []SponsoredPeer) {
	if s == nil {
		return
	}
	return s.Peers
}

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

// GetUsers returns value of Users field.
func (s *ContactsSponsoredPeers) GetUsers() (value []UserClass) {
	if s == nil {
		return
	}
	return s.Users
}

// MapChats returns field Chats wrapped in ChatClassArray helper.
func (s *ContactsSponsoredPeers) MapChats() (value ChatClassArray) {
	return ChatClassArray(s.Chats)
}

// MapUsers returns field Users wrapped in UserClassArray helper.
func (s *ContactsSponsoredPeers) MapUsers() (value UserClassArray) {
	return UserClassArray(s.Users)
}

// ContactsSponsoredPeersClassName is schema name of ContactsSponsoredPeersClass.
const ContactsSponsoredPeersClassName = "contacts.SponsoredPeers"

// ContactsSponsoredPeersClass represents contacts.SponsoredPeers generic type.
//
// See https://core.telegram.org/type/contacts.SponsoredPeers for reference.
//
// Example:
//
//	g, err := tg.DecodeContactsSponsoredPeers(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tg.ContactsSponsoredPeersEmpty: // contacts.sponsoredPeersEmpty#ea32b4b1
//	case *tg.ContactsSponsoredPeers: // contacts.sponsoredPeers#eb032884
//	default: panic(v)
//	}
type ContactsSponsoredPeersClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() ContactsSponsoredPeersClass

	// TypeID returns type id in TL schema.
	//
	// See https://core.telegram.org/mtproto/TL-tl#remarks.
	TypeID() uint32
	// TypeName returns name of type in TL schema.
	TypeName() string
	// String implements fmt.Stringer.
	String() string
	// Zero returns true if current object has a zero value.
	Zero() bool

	// AsNotEmpty tries to map ContactsSponsoredPeersClass to ContactsSponsoredPeers.
	AsNotEmpty() (*ContactsSponsoredPeers, bool)
}

// AsNotEmpty tries to map ContactsSponsoredPeersEmpty to ContactsSponsoredPeers.
func (s *ContactsSponsoredPeersEmpty) AsNotEmpty() (*ContactsSponsoredPeers, bool) {
	return nil, false
}

// AsNotEmpty tries to map ContactsSponsoredPeers to ContactsSponsoredPeers.
func (s *ContactsSponsoredPeers) AsNotEmpty() (*ContactsSponsoredPeers, bool) {
	return s, true
}

// DecodeContactsSponsoredPeers implements binary de-serialization for ContactsSponsoredPeersClass.
func DecodeContactsSponsoredPeers(buf *bin.Buffer) (ContactsSponsoredPeersClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case ContactsSponsoredPeersEmptyTypeID:
		// Decoding contacts.sponsoredPeersEmpty#ea32b4b1.
		v := ContactsSponsoredPeersEmpty{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode ContactsSponsoredPeersClass: %w", err)
		}
		return &v, nil
	case ContactsSponsoredPeersTypeID:
		// Decoding contacts.sponsoredPeers#eb032884.
		v := ContactsSponsoredPeers{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode ContactsSponsoredPeersClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode ContactsSponsoredPeersClass: %w", bin.NewUnexpectedID(id))
	}
}

// ContactsSponsoredPeers boxes the ContactsSponsoredPeersClass providing a helper.
type ContactsSponsoredPeersBox struct {
	SponsoredPeers ContactsSponsoredPeersClass
}

// Decode implements bin.Decoder for ContactsSponsoredPeersBox.
func (b *ContactsSponsoredPeersBox) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("unable to decode ContactsSponsoredPeersBox to nil")
	}
	v, err := DecodeContactsSponsoredPeers(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.SponsoredPeers = v
	return nil
}

// Encode implements bin.Encode for ContactsSponsoredPeersBox.
func (b *ContactsSponsoredPeersBox) Encode(buf *bin.Buffer) error {
	if b == nil || b.SponsoredPeers == nil {
		return fmt.Errorf("unable to encode ContactsSponsoredPeersClass as nil")
	}
	return b.SponsoredPeers.Encode(buf)
}
