// Code generated by gotdgen, DO NOT EDIT.

package tgtrace

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

// InvokeWithTraceRequest represents TL type `invokeWithTrace#104be9ab`.
type InvokeWithTraceRequest struct {
	// Flags field of InvokeWithTraceRequest.
	Flags bin.Fields
	// TraceParent field of InvokeWithTraceRequest.
	//
	// Use SetTraceParent and GetTraceParent helpers.
	TraceParent string
	// TraceState field of InvokeWithTraceRequest.
	//
	// Use SetTraceState and GetTraceState helpers.
	TraceState string
	// Baggage field of InvokeWithTraceRequest.
	//
	// Use SetBaggage and GetBaggage helpers.
	Baggage string
	// Query field of InvokeWithTraceRequest.
	Query bin.Object
}

// InvokeWithTraceRequestTypeID is TL type id of InvokeWithTraceRequest.
const InvokeWithTraceRequestTypeID = 0x104be9ab

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

func (i *InvokeWithTraceRequest) Zero() bool {
	if i == nil {
		return true
	}
	if !(i.Flags.Zero()) {
		return false
	}
	if !(i.TraceParent == "") {
		return false
	}
	if !(i.TraceState == "") {
		return false
	}
	if !(i.Baggage == "") {
		return false
	}
	if !(i.Query == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (i *InvokeWithTraceRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "invokeWithTrace",
		ID:   InvokeWithTraceRequestTypeID,
	}
	if i == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TraceParent",
			SchemaName: "traceParent",
			Null:       !i.Flags.Has(1),
		},
		{
			Name:       "TraceState",
			SchemaName: "traceState",
			Null:       !i.Flags.Has(2),
		},
		{
			Name:       "Baggage",
			SchemaName: "baggage",
			Null:       !i.Flags.Has(3),
		},
		{
			Name:       "Query",
			SchemaName: "query",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (i *InvokeWithTraceRequest) SetFlags() {
	if !(i.TraceParent == "") {
		i.Flags.Set(1)
	}
	if !(i.TraceState == "") {
		i.Flags.Set(2)
	}
	if !(i.Baggage == "") {
		i.Flags.Set(3)
	}
}

// Encode implements bin.Encoder.
func (i *InvokeWithTraceRequest) Encode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode invokeWithTrace#104be9ab as nil")
	}
	b.PutID(InvokeWithTraceRequestTypeID)
	return i.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (i *InvokeWithTraceRequest) EncodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode invokeWithTrace#104be9ab as nil")
	}
	i.SetFlags()
	if err := i.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode invokeWithTrace#104be9ab: field flags: %w", err)
	}
	if i.Flags.Has(1) {
		b.PutString(i.TraceParent)
	}
	if i.Flags.Has(2) {
		b.PutString(i.TraceState)
	}
	if i.Flags.Has(3) {
		b.PutString(i.Baggage)
	}
	if err := i.Query.Encode(b); err != nil {
		return fmt.Errorf("unable to encode invokeWithTrace#104be9ab: field query: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (i *InvokeWithTraceRequest) Decode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode invokeWithTrace#104be9ab to nil")
	}
	if err := b.ConsumeID(InvokeWithTraceRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode invokeWithTrace#104be9ab: %w", err)
	}
	return i.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (i *InvokeWithTraceRequest) DecodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode invokeWithTrace#104be9ab to nil")
	}
	{
		if err := i.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode invokeWithTrace#104be9ab: field flags: %w", err)
		}
	}
	if i.Flags.Has(1) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode invokeWithTrace#104be9ab: field traceParent: %w", err)
		}
		i.TraceParent = value
	}
	if i.Flags.Has(2) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode invokeWithTrace#104be9ab: field traceState: %w", err)
		}
		i.TraceState = value
	}
	if i.Flags.Has(3) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode invokeWithTrace#104be9ab: field baggage: %w", err)
		}
		i.Baggage = value
	}
	{
		if err := i.Query.Decode(b); err != nil {
			return fmt.Errorf("unable to decode invokeWithTrace#104be9ab: field query: %w", err)
		}
	}
	return nil
}

// SetTraceParent sets value of TraceParent conditional field.
func (i *InvokeWithTraceRequest) SetTraceParent(value string) {
	i.Flags.Set(1)
	i.TraceParent = value
}

// GetTraceParent returns value of TraceParent conditional field and
// boolean which is true if field was set.
func (i *InvokeWithTraceRequest) GetTraceParent() (value string, ok bool) {
	if i == nil {
		return
	}
	if !i.Flags.Has(1) {
		return value, false
	}
	return i.TraceParent, true
}

// SetTraceState sets value of TraceState conditional field.
func (i *InvokeWithTraceRequest) SetTraceState(value string) {
	i.Flags.Set(2)
	i.TraceState = value
}

// GetTraceState returns value of TraceState conditional field and
// boolean which is true if field was set.
func (i *InvokeWithTraceRequest) GetTraceState() (value string, ok bool) {
	if i == nil {
		return
	}
	if !i.Flags.Has(2) {
		return value, false
	}
	return i.TraceState, true
}

// SetBaggage sets value of Baggage conditional field.
func (i *InvokeWithTraceRequest) SetBaggage(value string) {
	i.Flags.Set(3)
	i.Baggage = value
}

// GetBaggage returns value of Baggage conditional field and
// boolean which is true if field was set.
func (i *InvokeWithTraceRequest) GetBaggage() (value string, ok bool) {
	if i == nil {
		return
	}
	if !i.Flags.Has(3) {
		return value, false
	}
	return i.Baggage, true
}

// GetQuery returns value of Query field.
func (i *InvokeWithTraceRequest) GetQuery() (value bin.Object) {
	if i == nil {
		return
	}
	return i.Query
}
