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

// Hashtags represents TL type `hashtags#6c2c0ae1`.
type Hashtags struct {
	// A list of hashtags
	Hashtags []string
}

// HashtagsTypeID is TL type id of Hashtags.
const HashtagsTypeID = 0x6c2c0ae1

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

func (h *Hashtags) Zero() bool {
	if h == nil {
		return true
	}
	if !(h.Hashtags == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (h *Hashtags) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "hashtags",
		ID:   HashtagsTypeID,
	}
	if h == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Hashtags",
			SchemaName: "hashtags",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (h *Hashtags) Encode(b *bin.Buffer) error {
	if h == nil {
		return fmt.Errorf("can't encode hashtags#6c2c0ae1 as nil")
	}
	b.PutID(HashtagsTypeID)
	return h.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (h *Hashtags) EncodeBare(b *bin.Buffer) error {
	if h == nil {
		return fmt.Errorf("can't encode hashtags#6c2c0ae1 as nil")
	}
	b.PutInt(len(h.Hashtags))
	for _, v := range h.Hashtags {
		b.PutString(v)
	}
	return nil
}

// Decode implements bin.Decoder.
func (h *Hashtags) Decode(b *bin.Buffer) error {
	if h == nil {
		return fmt.Errorf("can't decode hashtags#6c2c0ae1 to nil")
	}
	if err := b.ConsumeID(HashtagsTypeID); err != nil {
		return fmt.Errorf("unable to decode hashtags#6c2c0ae1: %w", err)
	}
	return h.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (h *Hashtags) DecodeBare(b *bin.Buffer) error {
	if h == nil {
		return fmt.Errorf("can't decode hashtags#6c2c0ae1 to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode hashtags#6c2c0ae1: field hashtags: %w", err)
		}

		if headerLen > 0 {
			h.Hashtags = 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 hashtags#6c2c0ae1: field hashtags: %w", err)
			}
			h.Hashtags = append(h.Hashtags, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (h *Hashtags) EncodeTDLibJSON(b tdjson.Encoder) error {
	if h == nil {
		return fmt.Errorf("can't encode hashtags#6c2c0ae1 as nil")
	}
	b.ObjStart()
	b.PutID("hashtags")
	b.Comma()
	b.FieldStart("hashtags")
	b.ArrStart()
	for _, v := range h.Hashtags {
		b.PutString(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (h *Hashtags) DecodeTDLibJSON(b tdjson.Decoder) error {
	if h == nil {
		return fmt.Errorf("can't decode hashtags#6c2c0ae1 to nil")
	}

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

// GetHashtags returns value of Hashtags field.
func (h *Hashtags) GetHashtags() (value []string) {
	if h == nil {
		return
	}
	return h.Hashtags
}
