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

// SearchStringsByPrefixRequest represents TL type `searchStringsByPrefix#a8145813`.
type SearchStringsByPrefixRequest struct {
	// The strings to search in for the query
	Strings []string
	// Query to search for
	Query string
	// The maximum number of objects to return
	Limit int32
	// Pass true to receive no results for an empty query
	ReturnNoneForEmptyQuery bool
}

// SearchStringsByPrefixRequestTypeID is TL type id of SearchStringsByPrefixRequest.
const SearchStringsByPrefixRequestTypeID = 0xa8145813

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SearchStringsByPrefixRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "searchStringsByPrefix",
		ID:   SearchStringsByPrefixRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Strings",
			SchemaName: "strings",
		},
		{
			Name:       "Query",
			SchemaName: "query",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
		{
			Name:       "ReturnNoneForEmptyQuery",
			SchemaName: "return_none_for_empty_query",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SearchStringsByPrefixRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchStringsByPrefix#a8145813 as nil")
	}
	b.PutID(SearchStringsByPrefixRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SearchStringsByPrefixRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchStringsByPrefix#a8145813 as nil")
	}
	b.PutInt(len(s.Strings))
	for _, v := range s.Strings {
		b.PutString(v)
	}
	b.PutString(s.Query)
	b.PutInt32(s.Limit)
	b.PutBool(s.ReturnNoneForEmptyQuery)
	return nil
}

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

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

		if headerLen > 0 {
			s.Strings = 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 searchStringsByPrefix#a8145813: field strings: %w", err)
			}
			s.Strings = append(s.Strings, value)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode searchStringsByPrefix#a8145813: field query: %w", err)
		}
		s.Query = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode searchStringsByPrefix#a8145813: field limit: %w", err)
		}
		s.Limit = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode searchStringsByPrefix#a8145813: field return_none_for_empty_query: %w", err)
		}
		s.ReturnNoneForEmptyQuery = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SearchStringsByPrefixRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode searchStringsByPrefix#a8145813 as nil")
	}
	b.ObjStart()
	b.PutID("searchStringsByPrefix")
	b.Comma()
	b.FieldStart("strings")
	b.ArrStart()
	for _, v := range s.Strings {
		b.PutString(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("query")
	b.PutString(s.Query)
	b.Comma()
	b.FieldStart("limit")
	b.PutInt32(s.Limit)
	b.Comma()
	b.FieldStart("return_none_for_empty_query")
	b.PutBool(s.ReturnNoneForEmptyQuery)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

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

// GetStrings returns value of Strings field.
func (s *SearchStringsByPrefixRequest) GetStrings() (value []string) {
	if s == nil {
		return
	}
	return s.Strings
}

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

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

// GetReturnNoneForEmptyQuery returns value of ReturnNoneForEmptyQuery field.
func (s *SearchStringsByPrefixRequest) GetReturnNoneForEmptyQuery() (value bool) {
	if s == nil {
		return
	}
	return s.ReturnNoneForEmptyQuery
}

// SearchStringsByPrefix invokes method searchStringsByPrefix#a8145813 returning error if any.
func (c *Client) SearchStringsByPrefix(ctx context.Context, request *SearchStringsByPrefixRequest) (*FoundPositions, error) {
	var result FoundPositions

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