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

// SearchPublicChatsRequest represents TL type `searchPublicChats#39d6e7b9`.
type SearchPublicChatsRequest struct {
	// Query to search for
	Query string
}

// SearchPublicChatsRequestTypeID is TL type id of SearchPublicChatsRequest.
const SearchPublicChatsRequestTypeID = 0x39d6e7b9

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

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (s *SearchPublicChatsRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchPublicChats#39d6e7b9 as nil")
	}
	b.PutID(SearchPublicChatsRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SearchPublicChatsRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchPublicChats#39d6e7b9 as nil")
	}
	b.PutString(s.Query)
	return nil
}

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

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

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SearchPublicChatsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode searchPublicChats#39d6e7b9 as nil")
	}
	b.ObjStart()
	b.PutID("searchPublicChats")
	b.Comma()
	b.FieldStart("query")
	b.PutString(s.Query)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

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

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

// SearchPublicChats invokes method searchPublicChats#39d6e7b9 returning error if any.
func (c *Client) SearchPublicChats(ctx context.Context, query string) (*Chats, error) {
	var result Chats

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