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

// ContactsGetSponsoredPeersRequest represents TL type `contacts.getSponsoredPeers#b6c8c393`.
// Obtain a list of sponsored peer search results for a given query
//
// See https://core.telegram.org/method/contacts.getSponsoredPeers for reference.
type ContactsGetSponsoredPeersRequest struct {
	// The query
	Q string
}

// ContactsGetSponsoredPeersRequestTypeID is TL type id of ContactsGetSponsoredPeersRequest.
const ContactsGetSponsoredPeersRequestTypeID = 0xb6c8c393

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

func (g *ContactsGetSponsoredPeersRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Q == "") {
		return false
	}

	return true
}

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

// FillFrom fills ContactsGetSponsoredPeersRequest from given interface.
func (g *ContactsGetSponsoredPeersRequest) FillFrom(from interface {
	GetQ() (value string)
}) {
	g.Q = from.GetQ()
}

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

// TypeName returns name of type in TL schema.
func (*ContactsGetSponsoredPeersRequest) TypeName() string {
	return "contacts.getSponsoredPeers"
}

// TypeInfo returns info about TL type.
func (g *ContactsGetSponsoredPeersRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "contacts.getSponsoredPeers",
		ID:   ContactsGetSponsoredPeersRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Q",
			SchemaName: "q",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *ContactsGetSponsoredPeersRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode contacts.getSponsoredPeers#b6c8c393 as nil")
	}
	b.PutID(ContactsGetSponsoredPeersRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *ContactsGetSponsoredPeersRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode contacts.getSponsoredPeers#b6c8c393 as nil")
	}
	b.PutString(g.Q)
	return nil
}

// Decode implements bin.Decoder.
func (g *ContactsGetSponsoredPeersRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode contacts.getSponsoredPeers#b6c8c393 to nil")
	}
	if err := b.ConsumeID(ContactsGetSponsoredPeersRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode contacts.getSponsoredPeers#b6c8c393: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *ContactsGetSponsoredPeersRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode contacts.getSponsoredPeers#b6c8c393 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode contacts.getSponsoredPeers#b6c8c393: field q: %w", err)
		}
		g.Q = value
	}
	return nil
}

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

// ContactsGetSponsoredPeers invokes method contacts.getSponsoredPeers#b6c8c393 returning error if any.
// Obtain a list of sponsored peer search results for a given query
//
// Possible errors:
//
//	400 SEARCH_QUERY_EMPTY: The search query is empty.
//
// See https://core.telegram.org/method/contacts.getSponsoredPeers for reference.
func (c *Client) ContactsGetSponsoredPeers(ctx context.Context, q string) (ContactsSponsoredPeersClass, error) {
	var result ContactsSponsoredPeersBox

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