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

// EmojiCategorySourceSearch represents TL type `emojiCategorySourceSearch#5e126d32`.
type EmojiCategorySourceSearch struct {
	// List of emojis to search for
	Emojis []string
}

// EmojiCategorySourceSearchTypeID is TL type id of EmojiCategorySourceSearch.
const EmojiCategorySourceSearchTypeID = 0x5e126d32

// construct implements constructor of EmojiCategorySourceClass.
func (e EmojiCategorySourceSearch) construct() EmojiCategorySourceClass { return &e }

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

	_ EmojiCategorySourceClass = &EmojiCategorySourceSearch{}
)

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (e *EmojiCategorySourceSearch) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "emojiCategorySourceSearch",
		ID:   EmojiCategorySourceSearchTypeID,
	}
	if e == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Emojis",
			SchemaName: "emojis",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (e *EmojiCategorySourceSearch) Encode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode emojiCategorySourceSearch#5e126d32 as nil")
	}
	b.PutID(EmojiCategorySourceSearchTypeID)
	return e.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (e *EmojiCategorySourceSearch) EncodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode emojiCategorySourceSearch#5e126d32 as nil")
	}
	b.PutInt(len(e.Emojis))
	for _, v := range e.Emojis {
		b.PutString(v)
	}
	return nil
}

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

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

		if headerLen > 0 {
			e.Emojis = make([]string, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode emojiCategorySourceSearch#5e126d32: field emojis: %w", err)
			}
			e.Emojis = append(e.Emojis, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (e *EmojiCategorySourceSearch) EncodeTDLibJSON(b tdjson.Encoder) error {
	if e == nil {
		return fmt.Errorf("can't encode emojiCategorySourceSearch#5e126d32 as nil")
	}
	b.ObjStart()
	b.PutID("emojiCategorySourceSearch")
	b.Comma()
	b.FieldStart("emojis")
	b.ArrStart()
	for _, v := range e.Emojis {
		b.PutString(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (e *EmojiCategorySourceSearch) DecodeTDLibJSON(b tdjson.Decoder) error {
	if e == nil {
		return fmt.Errorf("can't decode emojiCategorySourceSearch#5e126d32 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("emojiCategorySourceSearch"); err != nil {
				return fmt.Errorf("unable to decode emojiCategorySourceSearch#5e126d32: %w", err)
			}
		case "emojis":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.String()
				if err != nil {
					return fmt.Errorf("unable to decode emojiCategorySourceSearch#5e126d32: field emojis: %w", err)
				}
				e.Emojis = append(e.Emojis, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode emojiCategorySourceSearch#5e126d32: field emojis: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetEmojis returns value of Emojis field.
func (e *EmojiCategorySourceSearch) GetEmojis() (value []string) {
	if e == nil {
		return
	}
	return e.Emojis
}

// EmojiCategorySourcePremium represents TL type `emojiCategorySourcePremium#8cd28d0c`.
type EmojiCategorySourcePremium struct {
}

// EmojiCategorySourcePremiumTypeID is TL type id of EmojiCategorySourcePremium.
const EmojiCategorySourcePremiumTypeID = 0x8cd28d0c

// construct implements constructor of EmojiCategorySourceClass.
func (e EmojiCategorySourcePremium) construct() EmojiCategorySourceClass { return &e }

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

	_ EmojiCategorySourceClass = &EmojiCategorySourcePremium{}
)

func (e *EmojiCategorySourcePremium) Zero() bool {
	if e == nil {
		return true
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (e *EmojiCategorySourcePremium) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "emojiCategorySourcePremium",
		ID:   EmojiCategorySourcePremiumTypeID,
	}
	if e == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{}
	return typ
}

// Encode implements bin.Encoder.
func (e *EmojiCategorySourcePremium) Encode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode emojiCategorySourcePremium#8cd28d0c as nil")
	}
	b.PutID(EmojiCategorySourcePremiumTypeID)
	return e.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (e *EmojiCategorySourcePremium) EncodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode emojiCategorySourcePremium#8cd28d0c as nil")
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (e *EmojiCategorySourcePremium) DecodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode emojiCategorySourcePremium#8cd28d0c to nil")
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (e *EmojiCategorySourcePremium) EncodeTDLibJSON(b tdjson.Encoder) error {
	if e == nil {
		return fmt.Errorf("can't encode emojiCategorySourcePremium#8cd28d0c as nil")
	}
	b.ObjStart()
	b.PutID("emojiCategorySourcePremium")
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (e *EmojiCategorySourcePremium) DecodeTDLibJSON(b tdjson.Decoder) error {
	if e == nil {
		return fmt.Errorf("can't decode emojiCategorySourcePremium#8cd28d0c to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("emojiCategorySourcePremium"); err != nil {
				return fmt.Errorf("unable to decode emojiCategorySourcePremium#8cd28d0c: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// EmojiCategorySourceClassName is schema name of EmojiCategorySourceClass.
const EmojiCategorySourceClassName = "EmojiCategorySource"

// EmojiCategorySourceClass represents EmojiCategorySource generic type.
//
// Example:
//
//	g, err := tdapi.DecodeEmojiCategorySource(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tdapi.EmojiCategorySourceSearch: // emojiCategorySourceSearch#5e126d32
//	case *tdapi.EmojiCategorySourcePremium: // emojiCategorySourcePremium#8cd28d0c
//	default: panic(v)
//	}
type EmojiCategorySourceClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() EmojiCategorySourceClass

	// TypeID returns type id in TL schema.
	//
	// See https://core.telegram.org/mtproto/TL-tl#remarks.
	TypeID() uint32
	// TypeName returns name of type in TL schema.
	TypeName() string
	// String implements fmt.Stringer.
	String() string
	// Zero returns true if current object has a zero value.
	Zero() bool

	EncodeTDLibJSON(b tdjson.Encoder) error
	DecodeTDLibJSON(b tdjson.Decoder) error
}

// DecodeEmojiCategorySource implements binary de-serialization for EmojiCategorySourceClass.
func DecodeEmojiCategorySource(buf *bin.Buffer) (EmojiCategorySourceClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case EmojiCategorySourceSearchTypeID:
		// Decoding emojiCategorySourceSearch#5e126d32.
		v := EmojiCategorySourceSearch{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode EmojiCategorySourceClass: %w", err)
		}
		return &v, nil
	case EmojiCategorySourcePremiumTypeID:
		// Decoding emojiCategorySourcePremium#8cd28d0c.
		v := EmojiCategorySourcePremium{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode EmojiCategorySourceClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode EmojiCategorySourceClass: %w", bin.NewUnexpectedID(id))
	}
}

// DecodeTDLibJSONEmojiCategorySource implements binary de-serialization for EmojiCategorySourceClass.
func DecodeTDLibJSONEmojiCategorySource(buf tdjson.Decoder) (EmojiCategorySourceClass, error) {
	id, err := buf.FindTypeID()
	if err != nil {
		return nil, err
	}
	switch id {
	case "emojiCategorySourceSearch":
		// Decoding emojiCategorySourceSearch#5e126d32.
		v := EmojiCategorySourceSearch{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode EmojiCategorySourceClass: %w", err)
		}
		return &v, nil
	case "emojiCategorySourcePremium":
		// Decoding emojiCategorySourcePremium#8cd28d0c.
		v := EmojiCategorySourcePremium{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode EmojiCategorySourceClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode EmojiCategorySourceClass: %w", tdjson.NewUnexpectedID(id))
	}
}

// EmojiCategorySource boxes the EmojiCategorySourceClass providing a helper.
type EmojiCategorySourceBox struct {
	EmojiCategorySource EmojiCategorySourceClass
}

// Decode implements bin.Decoder for EmojiCategorySourceBox.
func (b *EmojiCategorySourceBox) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("unable to decode EmojiCategorySourceBox to nil")
	}
	v, err := DecodeEmojiCategorySource(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.EmojiCategorySource = v
	return nil
}

// Encode implements bin.Encode for EmojiCategorySourceBox.
func (b *EmojiCategorySourceBox) Encode(buf *bin.Buffer) error {
	if b == nil || b.EmojiCategorySource == nil {
		return fmt.Errorf("unable to encode EmojiCategorySourceClass as nil")
	}
	return b.EmojiCategorySource.Encode(buf)
}

// DecodeTDLibJSON implements bin.Decoder for EmojiCategorySourceBox.
func (b *EmojiCategorySourceBox) DecodeTDLibJSON(buf tdjson.Decoder) error {
	if b == nil {
		return fmt.Errorf("unable to decode EmojiCategorySourceBox to nil")
	}
	v, err := DecodeTDLibJSONEmojiCategorySource(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.EmojiCategorySource = v
	return nil
}

// EncodeTDLibJSON implements bin.Encode for EmojiCategorySourceBox.
func (b *EmojiCategorySourceBox) EncodeTDLibJSON(buf tdjson.Encoder) error {
	if b == nil || b.EmojiCategorySource == nil {
		return fmt.Errorf("unable to encode EmojiCategorySourceClass as nil")
	}
	return b.EmojiCategorySource.EncodeTDLibJSON(buf)
}
