// Code generated by gotdgen, DO NOT EDIT.

package tdapi

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

// RecommendedChatFilters represents TL type `recommendedChatFilters#3f7b7573`.
type RecommendedChatFilters struct {
	// List of recommended chat filters
	ChatFilters []RecommendedChatFilter
}

// RecommendedChatFiltersTypeID is TL type id of RecommendedChatFilters.
const RecommendedChatFiltersTypeID = 0x3f7b7573

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (r *RecommendedChatFilters) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "recommendedChatFilters",
		ID:   RecommendedChatFiltersTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatFilters",
			SchemaName: "chat_filters",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (r *RecommendedChatFilters) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode recommendedChatFilters#3f7b7573 as nil")
	}
	b.PutID(RecommendedChatFiltersTypeID)
	return r.EncodeBare(b)
}

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

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

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

		if headerLen > 0 {
			r.ChatFilters = make([]RecommendedChatFilter, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value RecommendedChatFilter
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare recommendedChatFilters#3f7b7573: field chat_filters: %w", err)
			}
			r.ChatFilters = append(r.ChatFilters, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (r *RecommendedChatFilters) EncodeTDLibJSON(b tdjson.Encoder) error {
	if r == nil {
		return fmt.Errorf("can't encode recommendedChatFilters#3f7b7573 as nil")
	}
	b.ObjStart()
	b.PutID("recommendedChatFilters")
	b.FieldStart("chat_filters")
	b.ArrStart()
	for idx, v := range r.ChatFilters {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode recommendedChatFilters#3f7b7573: field chat_filters element with index %d: %w", idx, err)
		}
	}
	b.ArrEnd()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (r *RecommendedChatFilters) DecodeTDLibJSON(b tdjson.Decoder) error {
	if r == nil {
		return fmt.Errorf("can't decode recommendedChatFilters#3f7b7573 to nil")
	}

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

// GetChatFilters returns value of ChatFilters field.
func (r *RecommendedChatFilters) GetChatFilters() (value []RecommendedChatFilter) {
	return r.ChatFilters
}
