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

// RecommendedChatFolders represents TL type `recommendedChatFolders#32208553`.
type RecommendedChatFolders struct {
	// List of recommended chat folders
	ChatFolders []RecommendedChatFolder
}

// RecommendedChatFoldersTypeID is TL type id of RecommendedChatFolders.
const RecommendedChatFoldersTypeID = 0x32208553

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

func (r *RecommendedChatFolders) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.ChatFolders == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (r *RecommendedChatFolders) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "recommendedChatFolders",
		ID:   RecommendedChatFoldersTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatFolders",
			SchemaName: "chat_folders",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (r *RecommendedChatFolders) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode recommendedChatFolders#32208553 as nil")
	}
	b.PutID(RecommendedChatFoldersTypeID)
	return r.EncodeBare(b)
}

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

// Decode implements bin.Decoder.
func (r *RecommendedChatFolders) Decode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode recommendedChatFolders#32208553 to nil")
	}
	if err := b.ConsumeID(RecommendedChatFoldersTypeID); err != nil {
		return fmt.Errorf("unable to decode recommendedChatFolders#32208553: %w", err)
	}
	return r.DecodeBare(b)
}

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

		if headerLen > 0 {
			r.ChatFolders = make([]RecommendedChatFolder, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value RecommendedChatFolder
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare recommendedChatFolders#32208553: field chat_folders: %w", err)
			}
			r.ChatFolders = append(r.ChatFolders, value)
		}
	}
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("recommendedChatFolders"); err != nil {
				return fmt.Errorf("unable to decode recommendedChatFolders#32208553: %w", err)
			}
		case "chat_folders":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value RecommendedChatFolder
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode recommendedChatFolders#32208553: field chat_folders: %w", err)
				}
				r.ChatFolders = append(r.ChatFolders, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode recommendedChatFolders#32208553: field chat_folders: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetChatFolders returns value of ChatFolders field.
func (r *RecommendedChatFolders) GetChatFolders() (value []RecommendedChatFolder) {
	if r == nil {
		return
	}
	return r.ChatFolders
}
