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

// MessagesSearchEmojiStickerSetsRequest represents TL type `messages.searchEmojiStickerSets#92b4494c`.
// Search for custom emoji stickersets »¹
//
// Links:
//  1. https://core.telegram.org/api/custom-emoji
//
// See https://core.telegram.org/method/messages.searchEmojiStickerSets for reference.
type MessagesSearchEmojiStickerSetsRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Exclude featured stickersets from results
	ExcludeFeatured bool
	// Query string
	Q string
	// Hash used for caching, for more info click here¹.
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets#hash-generation
	Hash int64
}

// MessagesSearchEmojiStickerSetsRequestTypeID is TL type id of MessagesSearchEmojiStickerSetsRequest.
const MessagesSearchEmojiStickerSetsRequestTypeID = 0x92b4494c

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

func (s *MessagesSearchEmojiStickerSetsRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.ExcludeFeatured == false) {
		return false
	}
	if !(s.Q == "") {
		return false
	}
	if !(s.Hash == 0) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesSearchEmojiStickerSetsRequest from given interface.
func (s *MessagesSearchEmojiStickerSetsRequest) FillFrom(from interface {
	GetExcludeFeatured() (value bool)
	GetQ() (value string)
	GetHash() (value int64)
}) {
	s.ExcludeFeatured = from.GetExcludeFeatured()
	s.Q = from.GetQ()
	s.Hash = from.GetHash()
}

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

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

// TypeInfo returns info about TL type.
func (s *MessagesSearchEmojiStickerSetsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.searchEmojiStickerSets",
		ID:   MessagesSearchEmojiStickerSetsRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ExcludeFeatured",
			SchemaName: "exclude_featured",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "Q",
			SchemaName: "q",
		},
		{
			Name:       "Hash",
			SchemaName: "hash",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *MessagesSearchEmojiStickerSetsRequest) SetFlags() {
	if !(s.ExcludeFeatured == false) {
		s.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (s *MessagesSearchEmojiStickerSetsRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode messages.searchEmojiStickerSets#92b4494c as nil")
	}
	b.PutID(MessagesSearchEmojiStickerSetsRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *MessagesSearchEmojiStickerSetsRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode messages.searchEmojiStickerSets#92b4494c as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.searchEmojiStickerSets#92b4494c: field flags: %w", err)
	}
	b.PutString(s.Q)
	b.PutLong(s.Hash)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *MessagesSearchEmojiStickerSetsRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode messages.searchEmojiStickerSets#92b4494c to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.searchEmojiStickerSets#92b4494c: field flags: %w", err)
		}
	}
	s.ExcludeFeatured = s.Flags.Has(0)
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode messages.searchEmojiStickerSets#92b4494c: field q: %w", err)
		}
		s.Q = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode messages.searchEmojiStickerSets#92b4494c: field hash: %w", err)
		}
		s.Hash = value
	}
	return nil
}

// SetExcludeFeatured sets value of ExcludeFeatured conditional field.
func (s *MessagesSearchEmojiStickerSetsRequest) SetExcludeFeatured(value bool) {
	if value {
		s.Flags.Set(0)
		s.ExcludeFeatured = true
	} else {
		s.Flags.Unset(0)
		s.ExcludeFeatured = false
	}
}

// GetExcludeFeatured returns value of ExcludeFeatured conditional field.
func (s *MessagesSearchEmojiStickerSetsRequest) GetExcludeFeatured() (value bool) {
	if s == nil {
		return
	}
	return s.Flags.Has(0)
}

// GetQ returns value of Q field.
func (s *MessagesSearchEmojiStickerSetsRequest) GetQ() (value string) {
	if s == nil {
		return
	}
	return s.Q
}

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

// MessagesSearchEmojiStickerSets invokes method messages.searchEmojiStickerSets#92b4494c returning error if any.
// Search for custom emoji stickersets »¹
//
// Links:
//  1. https://core.telegram.org/api/custom-emoji
//
// See https://core.telegram.org/method/messages.searchEmojiStickerSets for reference.
func (c *Client) MessagesSearchEmojiStickerSets(ctx context.Context, request *MessagesSearchEmojiStickerSetsRequest) (MessagesFoundStickerSetsClass, error) {
	var result MessagesFoundStickerSetsBox

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