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

// ChannelsCheckSearchPostsFloodRequest represents TL type `channels.checkSearchPostsFlood#22567115`.
// Check if the specified global post search »¹ requires payment.
//
// Links:
//  1. https://core.telegram.org/api/search#posts-tab
//
// See https://core.telegram.org/method/channels.checkSearchPostsFlood for reference.
type ChannelsCheckSearchPostsFloodRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// The query.
	//
	// Use SetQuery and GetQuery helpers.
	Query string
}

// ChannelsCheckSearchPostsFloodRequestTypeID is TL type id of ChannelsCheckSearchPostsFloodRequest.
const ChannelsCheckSearchPostsFloodRequestTypeID = 0x22567115

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

func (c *ChannelsCheckSearchPostsFloodRequest) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Flags.Zero()) {
		return false
	}
	if !(c.Query == "") {
		return false
	}

	return true
}

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

// FillFrom fills ChannelsCheckSearchPostsFloodRequest from given interface.
func (c *ChannelsCheckSearchPostsFloodRequest) FillFrom(from interface {
	GetQuery() (value string, ok bool)
}) {
	if val, ok := from.GetQuery(); ok {
		c.Query = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*ChannelsCheckSearchPostsFloodRequest) TypeName() string {
	return "channels.checkSearchPostsFlood"
}

// TypeInfo returns info about TL type.
func (c *ChannelsCheckSearchPostsFloodRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "channels.checkSearchPostsFlood",
		ID:   ChannelsCheckSearchPostsFloodRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Query",
			SchemaName: "query",
			Null:       !c.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (c *ChannelsCheckSearchPostsFloodRequest) SetFlags() {
	if !(c.Query == "") {
		c.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (c *ChannelsCheckSearchPostsFloodRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode channels.checkSearchPostsFlood#22567115 as nil")
	}
	b.PutID(ChannelsCheckSearchPostsFloodRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ChannelsCheckSearchPostsFloodRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode channels.checkSearchPostsFlood#22567115 as nil")
	}
	c.SetFlags()
	if err := c.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode channels.checkSearchPostsFlood#22567115: field flags: %w", err)
	}
	if c.Flags.Has(0) {
		b.PutString(c.Query)
	}
	return nil
}

// Decode implements bin.Decoder.
func (c *ChannelsCheckSearchPostsFloodRequest) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode channels.checkSearchPostsFlood#22567115 to nil")
	}
	if err := b.ConsumeID(ChannelsCheckSearchPostsFloodRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode channels.checkSearchPostsFlood#22567115: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *ChannelsCheckSearchPostsFloodRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode channels.checkSearchPostsFlood#22567115 to nil")
	}
	{
		if err := c.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode channels.checkSearchPostsFlood#22567115: field flags: %w", err)
		}
	}
	if c.Flags.Has(0) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode channels.checkSearchPostsFlood#22567115: field query: %w", err)
		}
		c.Query = value
	}
	return nil
}

// SetQuery sets value of Query conditional field.
func (c *ChannelsCheckSearchPostsFloodRequest) SetQuery(value string) {
	c.Flags.Set(0)
	c.Query = value
}

// GetQuery returns value of Query conditional field and
// boolean which is true if field was set.
func (c *ChannelsCheckSearchPostsFloodRequest) GetQuery() (value string, ok bool) {
	if c == nil {
		return
	}
	if !c.Flags.Has(0) {
		return value, false
	}
	return c.Query, true
}

// ChannelsCheckSearchPostsFlood invokes method channels.checkSearchPostsFlood#22567115 returning error if any.
// Check if the specified global post search »¹ requires payment.
//
// Links:
//  1. https://core.telegram.org/api/search#posts-tab
//
// See https://core.telegram.org/method/channels.checkSearchPostsFlood for reference.
func (c *Client) ChannelsCheckSearchPostsFlood(ctx context.Context, request *ChannelsCheckSearchPostsFloodRequest) (*SearchPostsFlood, error) {
	var result SearchPostsFlood

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