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

// StatsBroadcastRevenueTransactions represents TL type `stats.broadcastRevenueTransactions#87158466`.
// Channel ad revenue transactions »¹.
//
// Links:
//  1. https://core.telegram.org/api/revenue
//
// See https://core.telegram.org/constructor/stats.broadcastRevenueTransactions for reference.
type StatsBroadcastRevenueTransactions struct {
	// Total number of transactions.
	Count int
	// Transactions
	Transactions []BroadcastRevenueTransactionClass
}

// StatsBroadcastRevenueTransactionsTypeID is TL type id of StatsBroadcastRevenueTransactions.
const StatsBroadcastRevenueTransactionsTypeID = 0x87158466

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

func (b *StatsBroadcastRevenueTransactions) Zero() bool {
	if b == nil {
		return true
	}
	if !(b.Count == 0) {
		return false
	}
	if !(b.Transactions == nil) {
		return false
	}

	return true
}

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

// FillFrom fills StatsBroadcastRevenueTransactions from given interface.
func (b *StatsBroadcastRevenueTransactions) FillFrom(from interface {
	GetCount() (value int)
	GetTransactions() (value []BroadcastRevenueTransactionClass)
}) {
	b.Count = from.GetCount()
	b.Transactions = from.GetTransactions()
}

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

// TypeName returns name of type in TL schema.
func (*StatsBroadcastRevenueTransactions) TypeName() string {
	return "stats.broadcastRevenueTransactions"
}

// TypeInfo returns info about TL type.
func (b *StatsBroadcastRevenueTransactions) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "stats.broadcastRevenueTransactions",
		ID:   StatsBroadcastRevenueTransactionsTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Count",
			SchemaName: "count",
		},
		{
			Name:       "Transactions",
			SchemaName: "transactions",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (b *StatsBroadcastRevenueTransactions) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode stats.broadcastRevenueTransactions#87158466 as nil")
	}
	buf.PutID(StatsBroadcastRevenueTransactionsTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *StatsBroadcastRevenueTransactions) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode stats.broadcastRevenueTransactions#87158466 as nil")
	}
	buf.PutInt(b.Count)
	buf.PutVectorHeader(len(b.Transactions))
	for idx, v := range b.Transactions {
		if v == nil {
			return fmt.Errorf("unable to encode stats.broadcastRevenueTransactions#87158466: field transactions element with index %d is nil", idx)
		}
		if err := v.Encode(buf); err != nil {
			return fmt.Errorf("unable to encode stats.broadcastRevenueTransactions#87158466: field transactions element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (b *StatsBroadcastRevenueTransactions) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode stats.broadcastRevenueTransactions#87158466 to nil")
	}
	if err := buf.ConsumeID(StatsBroadcastRevenueTransactionsTypeID); err != nil {
		return fmt.Errorf("unable to decode stats.broadcastRevenueTransactions#87158466: %w", err)
	}
	return b.DecodeBare(buf)
}

// DecodeBare implements bin.BareDecoder.
func (b *StatsBroadcastRevenueTransactions) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode stats.broadcastRevenueTransactions#87158466 to nil")
	}
	{
		value, err := buf.Int()
		if err != nil {
			return fmt.Errorf("unable to decode stats.broadcastRevenueTransactions#87158466: field count: %w", err)
		}
		b.Count = value
	}
	{
		headerLen, err := buf.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode stats.broadcastRevenueTransactions#87158466: field transactions: %w", err)
		}

		if headerLen > 0 {
			b.Transactions = make([]BroadcastRevenueTransactionClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeBroadcastRevenueTransaction(buf)
			if err != nil {
				return fmt.Errorf("unable to decode stats.broadcastRevenueTransactions#87158466: field transactions: %w", err)
			}
			b.Transactions = append(b.Transactions, value)
		}
	}
	return nil
}

// GetCount returns value of Count field.
func (b *StatsBroadcastRevenueTransactions) GetCount() (value int) {
	if b == nil {
		return
	}
	return b.Count
}

// GetTransactions returns value of Transactions field.
func (b *StatsBroadcastRevenueTransactions) GetTransactions() (value []BroadcastRevenueTransactionClass) {
	if b == nil {
		return
	}
	return b.Transactions
}

// MapTransactions returns field Transactions wrapped in BroadcastRevenueTransactionClassArray helper.
func (b *StatsBroadcastRevenueTransactions) MapTransactions() (value BroadcastRevenueTransactionClassArray) {
	return BroadcastRevenueTransactionClassArray(b.Transactions)
}
