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

// GetSearchedForTagsRequest represents TL type `getSearchedForTags#9b1b30cd`.
type GetSearchedForTagsRequest struct {
	// Prefix of hashtags or cashtags to return
	TagPrefix string
	// The maximum number of items to be returned
	Limit int32
}

// GetSearchedForTagsRequestTypeID is TL type id of GetSearchedForTagsRequest.
const GetSearchedForTagsRequestTypeID = 0x9b1b30cd

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

func (g *GetSearchedForTagsRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.TagPrefix == "") {
		return false
	}
	if !(g.Limit == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetSearchedForTagsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getSearchedForTags",
		ID:   GetSearchedForTagsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TagPrefix",
			SchemaName: "tag_prefix",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetSearchedForTagsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getSearchedForTags#9b1b30cd as nil")
	}
	b.PutID(GetSearchedForTagsRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetSearchedForTagsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getSearchedForTags#9b1b30cd as nil")
	}
	b.PutString(g.TagPrefix)
	b.PutInt32(g.Limit)
	return nil
}

// Decode implements bin.Decoder.
func (g *GetSearchedForTagsRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getSearchedForTags#9b1b30cd to nil")
	}
	if err := b.ConsumeID(GetSearchedForTagsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getSearchedForTags#9b1b30cd: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetSearchedForTagsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getSearchedForTags#9b1b30cd to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getSearchedForTags#9b1b30cd: field tag_prefix: %w", err)
		}
		g.TagPrefix = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getSearchedForTags#9b1b30cd: field limit: %w", err)
		}
		g.Limit = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetSearchedForTagsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getSearchedForTags#9b1b30cd as nil")
	}
	b.ObjStart()
	b.PutID("getSearchedForTags")
	b.Comma()
	b.FieldStart("tag_prefix")
	b.PutString(g.TagPrefix)
	b.Comma()
	b.FieldStart("limit")
	b.PutInt32(g.Limit)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetSearchedForTagsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getSearchedForTags#9b1b30cd to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getSearchedForTags"); err != nil {
				return fmt.Errorf("unable to decode getSearchedForTags#9b1b30cd: %w", err)
			}
		case "tag_prefix":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode getSearchedForTags#9b1b30cd: field tag_prefix: %w", err)
			}
			g.TagPrefix = value
		case "limit":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode getSearchedForTags#9b1b30cd: field limit: %w", err)
			}
			g.Limit = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetTagPrefix returns value of TagPrefix field.
func (g *GetSearchedForTagsRequest) GetTagPrefix() (value string) {
	if g == nil {
		return
	}
	return g.TagPrefix
}

// GetLimit returns value of Limit field.
func (g *GetSearchedForTagsRequest) GetLimit() (value int32) {
	if g == nil {
		return
	}
	return g.Limit
}

// GetSearchedForTags invokes method getSearchedForTags#9b1b30cd returning error if any.
func (c *Client) GetSearchedForTags(ctx context.Context, request *GetSearchedForTagsRequest) (*Hashtags, error) {
	var result Hashtags

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return &result, nil
}
