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

// SavedMessagesTags represents TL type `savedMessagesTags#ca94a1b3`.
type SavedMessagesTags struct {
	// List of tags
	Tags []SavedMessagesTag
}

// SavedMessagesTagsTypeID is TL type id of SavedMessagesTags.
const SavedMessagesTagsTypeID = 0xca94a1b3

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

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (s *SavedMessagesTags) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode savedMessagesTags#ca94a1b3 as nil")
	}
	b.PutID(SavedMessagesTagsTypeID)
	return s.EncodeBare(b)
}

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

// Decode implements bin.Decoder.
func (s *SavedMessagesTags) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode savedMessagesTags#ca94a1b3 to nil")
	}
	if err := b.ConsumeID(SavedMessagesTagsTypeID); err != nil {
		return fmt.Errorf("unable to decode savedMessagesTags#ca94a1b3: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SavedMessagesTags) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode savedMessagesTags#ca94a1b3 to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode savedMessagesTags#ca94a1b3: field tags: %w", err)
		}

		if headerLen > 0 {
			s.Tags = make([]SavedMessagesTag, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value SavedMessagesTag
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare savedMessagesTags#ca94a1b3: field tags: %w", err)
			}
			s.Tags = append(s.Tags, value)
		}
	}
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("savedMessagesTags"); err != nil {
				return fmt.Errorf("unable to decode savedMessagesTags#ca94a1b3: %w", err)
			}
		case "tags":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value SavedMessagesTag
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode savedMessagesTags#ca94a1b3: field tags: %w", err)
				}
				s.Tags = append(s.Tags, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode savedMessagesTags#ca94a1b3: field tags: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

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