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

// SearchRecentlyFoundChatsRequest represents TL type `searchRecentlyFoundChats#62320591`.
type SearchRecentlyFoundChatsRequest struct {
	// Query to search for
	Query string
	// The maximum number of chats to be returned
	Limit int32
}

// SearchRecentlyFoundChatsRequestTypeID is TL type id of SearchRecentlyFoundChatsRequest.
const SearchRecentlyFoundChatsRequestTypeID = 0x62320591

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

func (s *SearchRecentlyFoundChatsRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Query == "") {
		return false
	}
	if !(s.Limit == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SearchRecentlyFoundChatsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "searchRecentlyFoundChats",
		ID:   SearchRecentlyFoundChatsRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Query",
			SchemaName: "query",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SearchRecentlyFoundChatsRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchRecentlyFoundChats#62320591 as nil")
	}
	b.PutID(SearchRecentlyFoundChatsRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SearchRecentlyFoundChatsRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchRecentlyFoundChats#62320591 as nil")
	}
	b.PutString(s.Query)
	b.PutInt32(s.Limit)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *SearchRecentlyFoundChatsRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode searchRecentlyFoundChats#62320591 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode searchRecentlyFoundChats#62320591: field query: %w", err)
		}
		s.Query = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode searchRecentlyFoundChats#62320591: field limit: %w", err)
		}
		s.Limit = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SearchRecentlyFoundChatsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode searchRecentlyFoundChats#62320591 as nil")
	}
	b.ObjStart()
	b.PutID("searchRecentlyFoundChats")
	b.Comma()
	b.FieldStart("query")
	b.PutString(s.Query)
	b.Comma()
	b.FieldStart("limit")
	b.PutInt32(s.Limit)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SearchRecentlyFoundChatsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode searchRecentlyFoundChats#62320591 to nil")
	}

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

// GetQuery returns value of Query field.
func (s *SearchRecentlyFoundChatsRequest) GetQuery() (value string) {
	if s == nil {
		return
	}
	return s.Query
}

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

// SearchRecentlyFoundChats invokes method searchRecentlyFoundChats#62320591 returning error if any.
func (c *Client) SearchRecentlyFoundChats(ctx context.Context, request *SearchRecentlyFoundChatsRequest) (*Chats, error) {
	var result Chats

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