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

// ChatFolderInviteLinkInfo represents TL type `chatFolderInviteLinkInfo#776fe405`.
type ChatFolderInviteLinkInfo struct {
	// Basic information about the chat folder; chat folder identifier will be 0 if the user
	// didn't have the chat folder yet
	ChatFolderInfo ChatFolderInfo
	// Identifiers of the chats from the link, which aren't added to the folder yet
	MissingChatIDs []int64
	// Identifiers of the chats from the link, which are added to the folder already
	AddedChatIDs []int64
}

// ChatFolderInviteLinkInfoTypeID is TL type id of ChatFolderInviteLinkInfo.
const ChatFolderInviteLinkInfoTypeID = 0x776fe405

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

func (c *ChatFolderInviteLinkInfo) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.ChatFolderInfo.Zero()) {
		return false
	}
	if !(c.MissingChatIDs == nil) {
		return false
	}
	if !(c.AddedChatIDs == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *ChatFolderInviteLinkInfo) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "chatFolderInviteLinkInfo",
		ID:   ChatFolderInviteLinkInfoTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatFolderInfo",
			SchemaName: "chat_folder_info",
		},
		{
			Name:       "MissingChatIDs",
			SchemaName: "missing_chat_ids",
		},
		{
			Name:       "AddedChatIDs",
			SchemaName: "added_chat_ids",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *ChatFolderInviteLinkInfo) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatFolderInviteLinkInfo#776fe405 as nil")
	}
	b.PutID(ChatFolderInviteLinkInfoTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ChatFolderInviteLinkInfo) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatFolderInviteLinkInfo#776fe405 as nil")
	}
	if err := c.ChatFolderInfo.Encode(b); err != nil {
		return fmt.Errorf("unable to encode chatFolderInviteLinkInfo#776fe405: field chat_folder_info: %w", err)
	}
	b.PutInt(len(c.MissingChatIDs))
	for _, v := range c.MissingChatIDs {
		b.PutInt53(v)
	}
	b.PutInt(len(c.AddedChatIDs))
	for _, v := range c.AddedChatIDs {
		b.PutInt53(v)
	}
	return nil
}

// Decode implements bin.Decoder.
func (c *ChatFolderInviteLinkInfo) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatFolderInviteLinkInfo#776fe405 to nil")
	}
	if err := b.ConsumeID(ChatFolderInviteLinkInfoTypeID); err != nil {
		return fmt.Errorf("unable to decode chatFolderInviteLinkInfo#776fe405: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *ChatFolderInviteLinkInfo) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatFolderInviteLinkInfo#776fe405 to nil")
	}
	{
		if err := c.ChatFolderInfo.Decode(b); err != nil {
			return fmt.Errorf("unable to decode chatFolderInviteLinkInfo#776fe405: field chat_folder_info: %w", err)
		}
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode chatFolderInviteLinkInfo#776fe405: field missing_chat_ids: %w", err)
		}

		if headerLen > 0 {
			c.MissingChatIDs = make([]int64, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode chatFolderInviteLinkInfo#776fe405: field missing_chat_ids: %w", err)
			}
			c.MissingChatIDs = append(c.MissingChatIDs, value)
		}
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode chatFolderInviteLinkInfo#776fe405: field added_chat_ids: %w", err)
		}

		if headerLen > 0 {
			c.AddedChatIDs = make([]int64, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode chatFolderInviteLinkInfo#776fe405: field added_chat_ids: %w", err)
			}
			c.AddedChatIDs = append(c.AddedChatIDs, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *ChatFolderInviteLinkInfo) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode chatFolderInviteLinkInfo#776fe405 as nil")
	}
	b.ObjStart()
	b.PutID("chatFolderInviteLinkInfo")
	b.Comma()
	b.FieldStart("chat_folder_info")
	if err := c.ChatFolderInfo.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode chatFolderInviteLinkInfo#776fe405: field chat_folder_info: %w", err)
	}
	b.Comma()
	b.FieldStart("missing_chat_ids")
	b.ArrStart()
	for _, v := range c.MissingChatIDs {
		b.PutInt53(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("added_chat_ids")
	b.ArrStart()
	for _, v := range c.AddedChatIDs {
		b.PutInt53(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *ChatFolderInviteLinkInfo) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode chatFolderInviteLinkInfo#776fe405 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("chatFolderInviteLinkInfo"); err != nil {
				return fmt.Errorf("unable to decode chatFolderInviteLinkInfo#776fe405: %w", err)
			}
		case "chat_folder_info":
			if err := c.ChatFolderInfo.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode chatFolderInviteLinkInfo#776fe405: field chat_folder_info: %w", err)
			}
		case "missing_chat_ids":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int53()
				if err != nil {
					return fmt.Errorf("unable to decode chatFolderInviteLinkInfo#776fe405: field missing_chat_ids: %w", err)
				}
				c.MissingChatIDs = append(c.MissingChatIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode chatFolderInviteLinkInfo#776fe405: field missing_chat_ids: %w", err)
			}
		case "added_chat_ids":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int53()
				if err != nil {
					return fmt.Errorf("unable to decode chatFolderInviteLinkInfo#776fe405: field added_chat_ids: %w", err)
				}
				c.AddedChatIDs = append(c.AddedChatIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode chatFolderInviteLinkInfo#776fe405: field added_chat_ids: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetChatFolderInfo returns value of ChatFolderInfo field.
func (c *ChatFolderInviteLinkInfo) GetChatFolderInfo() (value ChatFolderInfo) {
	if c == nil {
		return
	}
	return c.ChatFolderInfo
}

// GetMissingChatIDs returns value of MissingChatIDs field.
func (c *ChatFolderInviteLinkInfo) GetMissingChatIDs() (value []int64) {
	if c == nil {
		return
	}
	return c.MissingChatIDs
}

// GetAddedChatIDs returns value of AddedChatIDs field.
func (c *ChatFolderInviteLinkInfo) GetAddedChatIDs() (value []int64) {
	if c == nil {
		return
	}
	return c.AddedChatIDs
}
