// Code generated by gotdgen, DO NOT EDIT.

package tg

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

	"go.uber.org/multierr"

	"github.com/gotd/td/bin"
	"github.com/gotd/td/tdjson"
	"github.com/gotd/td/tdp"
	"github.com/gotd/td/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{}
)

// ChatlistsChatlistUpdates represents TL type `chatlists.chatlistUpdates#93bd878d`.
// Updated information about a chat folder deep link »¹.
//
// Links:
//  1. https://core.telegram.org/api/links#chat-folder-links
//
// See https://core.telegram.org/constructor/chatlists.chatlistUpdates for reference.
type ChatlistsChatlistUpdates struct {
	// New peers to join
	MissingPeers []PeerClass
	// Related chat information
	Chats []ChatClass
	// Related user information
	Users []UserClass
}

// ChatlistsChatlistUpdatesTypeID is TL type id of ChatlistsChatlistUpdates.
const ChatlistsChatlistUpdatesTypeID = 0x93bd878d

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

func (c *ChatlistsChatlistUpdates) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.MissingPeers == nil) {
		return false
	}
	if !(c.Chats == nil) {
		return false
	}
	if !(c.Users == nil) {
		return false
	}

	return true
}

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

// FillFrom fills ChatlistsChatlistUpdates from given interface.
func (c *ChatlistsChatlistUpdates) FillFrom(from interface {
	GetMissingPeers() (value []PeerClass)
	GetChats() (value []ChatClass)
	GetUsers() (value []UserClass)
}) {
	c.MissingPeers = from.GetMissingPeers()
	c.Chats = from.GetChats()
	c.Users = from.GetUsers()
}

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

// TypeName returns name of type in TL schema.
func (*ChatlistsChatlistUpdates) TypeName() string {
	return "chatlists.chatlistUpdates"
}

// TypeInfo returns info about TL type.
func (c *ChatlistsChatlistUpdates) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "chatlists.chatlistUpdates",
		ID:   ChatlistsChatlistUpdatesTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "MissingPeers",
			SchemaName: "missing_peers",
		},
		{
			Name:       "Chats",
			SchemaName: "chats",
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *ChatlistsChatlistUpdates) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatlists.chatlistUpdates#93bd878d as nil")
	}
	b.PutID(ChatlistsChatlistUpdatesTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ChatlistsChatlistUpdates) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatlists.chatlistUpdates#93bd878d as nil")
	}
	b.PutVectorHeader(len(c.MissingPeers))
	for idx, v := range c.MissingPeers {
		if v == nil {
			return fmt.Errorf("unable to encode chatlists.chatlistUpdates#93bd878d: field missing_peers element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode chatlists.chatlistUpdates#93bd878d: field missing_peers element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(c.Chats))
	for idx, v := range c.Chats {
		if v == nil {
			return fmt.Errorf("unable to encode chatlists.chatlistUpdates#93bd878d: field chats element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode chatlists.chatlistUpdates#93bd878d: field chats element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(c.Users))
	for idx, v := range c.Users {
		if v == nil {
			return fmt.Errorf("unable to encode chatlists.chatlistUpdates#93bd878d: field users element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode chatlists.chatlistUpdates#93bd878d: field users element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (c *ChatlistsChatlistUpdates) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatlists.chatlistUpdates#93bd878d to nil")
	}
	if err := b.ConsumeID(ChatlistsChatlistUpdatesTypeID); err != nil {
		return fmt.Errorf("unable to decode chatlists.chatlistUpdates#93bd878d: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *ChatlistsChatlistUpdates) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatlists.chatlistUpdates#93bd878d to nil")
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode chatlists.chatlistUpdates#93bd878d: field missing_peers: %w", err)
		}

		if headerLen > 0 {
			c.MissingPeers = make([]PeerClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodePeer(b)
			if err != nil {
				return fmt.Errorf("unable to decode chatlists.chatlistUpdates#93bd878d: field missing_peers: %w", err)
			}
			c.MissingPeers = append(c.MissingPeers, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode chatlists.chatlistUpdates#93bd878d: field chats: %w", err)
		}

		if headerLen > 0 {
			c.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 chatlists.chatlistUpdates#93bd878d: field chats: %w", err)
			}
			c.Chats = append(c.Chats, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode chatlists.chatlistUpdates#93bd878d: field users: %w", err)
		}

		if headerLen > 0 {
			c.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 chatlists.chatlistUpdates#93bd878d: field users: %w", err)
			}
			c.Users = append(c.Users, value)
		}
	}
	return nil
}

// GetMissingPeers returns value of MissingPeers field.
func (c *ChatlistsChatlistUpdates) GetMissingPeers() (value []PeerClass) {
	if c == nil {
		return
	}
	return c.MissingPeers
}

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

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

// MapMissingPeers returns field MissingPeers wrapped in PeerClassArray helper.
func (c *ChatlistsChatlistUpdates) MapMissingPeers() (value PeerClassArray) {
	return PeerClassArray(c.MissingPeers)
}

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

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