// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// MessagesSavedReactionTagsNotModified represents TL type `messages.savedReactionTagsNotModified#889b59ef`.
// The list of reaction tag »¹ names assigned by the user hasn't changed.
//
// Links:
//  1. https://core.telegram.org/api/saved-messages#tags
//
// See https://core.telegram.org/constructor/messages.savedReactionTagsNotModified for reference.
type MessagesSavedReactionTagsNotModified struct {
}

// MessagesSavedReactionTagsNotModifiedTypeID is TL type id of MessagesSavedReactionTagsNotModified.
const MessagesSavedReactionTagsNotModifiedTypeID = 0x889b59ef

// construct implements constructor of MessagesSavedReactionTagsClass.
func (s MessagesSavedReactionTagsNotModified) construct() MessagesSavedReactionTagsClass { return &s }

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

	_ MessagesSavedReactionTagsClass = &MessagesSavedReactionTagsNotModified{}
)

func (s *MessagesSavedReactionTagsNotModified) Zero() bool {
	if s == nil {
		return true
	}

	return true
}

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

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

// TypeName returns name of type in TL schema.
func (*MessagesSavedReactionTagsNotModified) TypeName() string {
	return "messages.savedReactionTagsNotModified"
}

// TypeInfo returns info about TL type.
func (s *MessagesSavedReactionTagsNotModified) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.savedReactionTagsNotModified",
		ID:   MessagesSavedReactionTagsNotModifiedTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{}
	return typ
}

// Encode implements bin.Encoder.
func (s *MessagesSavedReactionTagsNotModified) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode messages.savedReactionTagsNotModified#889b59ef as nil")
	}
	b.PutID(MessagesSavedReactionTagsNotModifiedTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *MessagesSavedReactionTagsNotModified) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode messages.savedReactionTagsNotModified#889b59ef as nil")
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *MessagesSavedReactionTagsNotModified) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode messages.savedReactionTagsNotModified#889b59ef to nil")
	}
	if err := b.ConsumeID(MessagesSavedReactionTagsNotModifiedTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.savedReactionTagsNotModified#889b59ef: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *MessagesSavedReactionTagsNotModified) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode messages.savedReactionTagsNotModified#889b59ef to nil")
	}
	return nil
}

// MessagesSavedReactionTags represents TL type `messages.savedReactionTags#3259950a`.
// List of reaction tag »¹ names assigned by the user.
//
// Links:
//  1. https://core.telegram.org/api/saved-messages#tags
//
// See https://core.telegram.org/constructor/messages.savedReactionTags for reference.
type MessagesSavedReactionTags struct {
	// Saved reaction tags.
	Tags []SavedReactionTag
	// Hash used for caching, for more info click here¹. Can also be manually regenerated,
	// if needed, using the custom algorithm specified here »².
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets#hash-generation
	//  2) https://core.telegram.org/api/saved-messages#tags
	Hash int64
}

// MessagesSavedReactionTagsTypeID is TL type id of MessagesSavedReactionTags.
const MessagesSavedReactionTagsTypeID = 0x3259950a

// construct implements constructor of MessagesSavedReactionTagsClass.
func (s MessagesSavedReactionTags) construct() MessagesSavedReactionTagsClass { return &s }

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

	_ MessagesSavedReactionTagsClass = &MessagesSavedReactionTags{}
)

func (s *MessagesSavedReactionTags) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Tags == nil) {
		return false
	}
	if !(s.Hash == 0) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesSavedReactionTags from given interface.
func (s *MessagesSavedReactionTags) FillFrom(from interface {
	GetTags() (value []SavedReactionTag)
	GetHash() (value int64)
}) {
	s.Tags = from.GetTags()
	s.Hash = from.GetHash()
}

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

// TypeName returns name of type in TL schema.
func (*MessagesSavedReactionTags) TypeName() string {
	return "messages.savedReactionTags"
}

// TypeInfo returns info about TL type.
func (s *MessagesSavedReactionTags) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.savedReactionTags",
		ID:   MessagesSavedReactionTagsTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Tags",
			SchemaName: "tags",
		},
		{
			Name:       "Hash",
			SchemaName: "hash",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *MessagesSavedReactionTags) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode messages.savedReactionTags#3259950a as nil")
	}
	b.PutID(MessagesSavedReactionTagsTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *MessagesSavedReactionTags) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode messages.savedReactionTags#3259950a as nil")
	}
	b.PutVectorHeader(len(s.Tags))
	for idx, v := range s.Tags {
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.savedReactionTags#3259950a: field tags element with index %d: %w", idx, err)
		}
	}
	b.PutLong(s.Hash)
	return nil
}

// Decode implements bin.Decoder.
func (s *MessagesSavedReactionTags) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode messages.savedReactionTags#3259950a to nil")
	}
	if err := b.ConsumeID(MessagesSavedReactionTagsTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.savedReactionTags#3259950a: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *MessagesSavedReactionTags) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode messages.savedReactionTags#3259950a to nil")
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.savedReactionTags#3259950a: field tags: %w", err)
		}

		if headerLen > 0 {
			s.Tags = make([]SavedReactionTag, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value SavedReactionTag
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode messages.savedReactionTags#3259950a: field tags: %w", err)
			}
			s.Tags = append(s.Tags, value)
		}
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode messages.savedReactionTags#3259950a: field hash: %w", err)
		}
		s.Hash = value
	}
	return nil
}

// GetTags returns value of Tags field.
func (s *MessagesSavedReactionTags) GetTags() (value []SavedReactionTag) {
	if s == nil {
		return
	}
	return s.Tags
}

// GetHash returns value of Hash field.
func (s *MessagesSavedReactionTags) GetHash() (value int64) {
	if s == nil {
		return
	}
	return s.Hash
}

// MessagesSavedReactionTagsClassName is schema name of MessagesSavedReactionTagsClass.
const MessagesSavedReactionTagsClassName = "messages.SavedReactionTags"

// MessagesSavedReactionTagsClass represents messages.SavedReactionTags generic type.
//
// See https://core.telegram.org/type/messages.SavedReactionTags for reference.
//
// Example:
//
//	g, err := tg.DecodeMessagesSavedReactionTags(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tg.MessagesSavedReactionTagsNotModified: // messages.savedReactionTagsNotModified#889b59ef
//	case *tg.MessagesSavedReactionTags: // messages.savedReactionTags#3259950a
//	default: panic(v)
//	}
type MessagesSavedReactionTagsClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() MessagesSavedReactionTagsClass

	// 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

	// AsModified tries to map MessagesSavedReactionTagsClass to MessagesSavedReactionTags.
	AsModified() (*MessagesSavedReactionTags, bool)
}

// AsModified tries to map MessagesSavedReactionTagsNotModified to MessagesSavedReactionTags.
func (s *MessagesSavedReactionTagsNotModified) AsModified() (*MessagesSavedReactionTags, bool) {
	return nil, false
}

// AsModified tries to map MessagesSavedReactionTags to MessagesSavedReactionTags.
func (s *MessagesSavedReactionTags) AsModified() (*MessagesSavedReactionTags, bool) {
	return s, true
}

// DecodeMessagesSavedReactionTags implements binary de-serialization for MessagesSavedReactionTagsClass.
func DecodeMessagesSavedReactionTags(buf *bin.Buffer) (MessagesSavedReactionTagsClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case MessagesSavedReactionTagsNotModifiedTypeID:
		// Decoding messages.savedReactionTagsNotModified#889b59ef.
		v := MessagesSavedReactionTagsNotModified{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode MessagesSavedReactionTagsClass: %w", err)
		}
		return &v, nil
	case MessagesSavedReactionTagsTypeID:
		// Decoding messages.savedReactionTags#3259950a.
		v := MessagesSavedReactionTags{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode MessagesSavedReactionTagsClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode MessagesSavedReactionTagsClass: %w", bin.NewUnexpectedID(id))
	}
}

// MessagesSavedReactionTags boxes the MessagesSavedReactionTagsClass providing a helper.
type MessagesSavedReactionTagsBox struct {
	SavedReactionTags MessagesSavedReactionTagsClass
}

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

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