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

// EmojiCategories represents TL type `emojiCategories#c680f27a`.
type EmojiCategories struct {
	// List of categories
	Categories []EmojiCategory
}

// EmojiCategoriesTypeID is TL type id of EmojiCategories.
const EmojiCategoriesTypeID = 0xc680f27a

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

func (e *EmojiCategories) Zero() bool {
	if e == nil {
		return true
	}
	if !(e.Categories == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (e *EmojiCategories) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "emojiCategories",
		ID:   EmojiCategoriesTypeID,
	}
	if e == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Categories",
			SchemaName: "categories",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (e *EmojiCategories) Encode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode emojiCategories#c680f27a as nil")
	}
	b.PutID(EmojiCategoriesTypeID)
	return e.EncodeBare(b)
}

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

// Decode implements bin.Decoder.
func (e *EmojiCategories) Decode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode emojiCategories#c680f27a to nil")
	}
	if err := b.ConsumeID(EmojiCategoriesTypeID); err != nil {
		return fmt.Errorf("unable to decode emojiCategories#c680f27a: %w", err)
	}
	return e.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (e *EmojiCategories) DecodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode emojiCategories#c680f27a to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode emojiCategories#c680f27a: field categories: %w", err)
		}

		if headerLen > 0 {
			e.Categories = make([]EmojiCategory, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value EmojiCategory
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare emojiCategories#c680f27a: field categories: %w", err)
			}
			e.Categories = append(e.Categories, value)
		}
	}
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("emojiCategories"); err != nil {
				return fmt.Errorf("unable to decode emojiCategories#c680f27a: %w", err)
			}
		case "categories":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value EmojiCategory
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode emojiCategories#c680f27a: field categories: %w", err)
				}
				e.Categories = append(e.Categories, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode emojiCategories#c680f27a: field categories: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetCategories returns value of Categories field.
func (e *EmojiCategories) GetCategories() (value []EmojiCategory) {
	if e == nil {
		return
	}
	return e.Categories
}
