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

// PaymentsStarGiftActiveAuctionsNotModified represents TL type `payments.starGiftActiveAuctionsNotModified#db33dad0`.
//
// See https://core.telegram.org/constructor/payments.starGiftActiveAuctionsNotModified for reference.
type PaymentsStarGiftActiveAuctionsNotModified struct {
}

// PaymentsStarGiftActiveAuctionsNotModifiedTypeID is TL type id of PaymentsStarGiftActiveAuctionsNotModified.
const PaymentsStarGiftActiveAuctionsNotModifiedTypeID = 0xdb33dad0

// construct implements constructor of PaymentsStarGiftActiveAuctionsClass.
func (s PaymentsStarGiftActiveAuctionsNotModified) construct() PaymentsStarGiftActiveAuctionsClass {
	return &s
}

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

	_ PaymentsStarGiftActiveAuctionsClass = &PaymentsStarGiftActiveAuctionsNotModified{}
)

func (s *PaymentsStarGiftActiveAuctionsNotModified) Zero() bool {
	if s == nil {
		return true
	}

	return true
}

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

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

// TypeName returns name of type in TL schema.
func (*PaymentsStarGiftActiveAuctionsNotModified) TypeName() string {
	return "payments.starGiftActiveAuctionsNotModified"
}

// TypeInfo returns info about TL type.
func (s *PaymentsStarGiftActiveAuctionsNotModified) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.starGiftActiveAuctionsNotModified",
		ID:   PaymentsStarGiftActiveAuctionsNotModifiedTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{}
	return typ
}

// Encode implements bin.Encoder.
func (s *PaymentsStarGiftActiveAuctionsNotModified) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode payments.starGiftActiveAuctionsNotModified#db33dad0 as nil")
	}
	b.PutID(PaymentsStarGiftActiveAuctionsNotModifiedTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *PaymentsStarGiftActiveAuctionsNotModified) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode payments.starGiftActiveAuctionsNotModified#db33dad0 as nil")
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *PaymentsStarGiftActiveAuctionsNotModified) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode payments.starGiftActiveAuctionsNotModified#db33dad0 to nil")
	}
	if err := b.ConsumeID(PaymentsStarGiftActiveAuctionsNotModifiedTypeID); err != nil {
		return fmt.Errorf("unable to decode payments.starGiftActiveAuctionsNotModified#db33dad0: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *PaymentsStarGiftActiveAuctionsNotModified) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode payments.starGiftActiveAuctionsNotModified#db33dad0 to nil")
	}
	return nil
}

// PaymentsStarGiftActiveAuctions represents TL type `payments.starGiftActiveAuctions#97f187d8`.
//
// See https://core.telegram.org/constructor/payments.starGiftActiveAuctions for reference.
type PaymentsStarGiftActiveAuctions struct {
	// Auctions field of PaymentsStarGiftActiveAuctions.
	Auctions []StarGiftActiveAuctionState
	// Users field of PaymentsStarGiftActiveAuctions.
	Users []UserClass
}

// PaymentsStarGiftActiveAuctionsTypeID is TL type id of PaymentsStarGiftActiveAuctions.
const PaymentsStarGiftActiveAuctionsTypeID = 0x97f187d8

// construct implements constructor of PaymentsStarGiftActiveAuctionsClass.
func (s PaymentsStarGiftActiveAuctions) construct() PaymentsStarGiftActiveAuctionsClass { return &s }

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

	_ PaymentsStarGiftActiveAuctionsClass = &PaymentsStarGiftActiveAuctions{}
)

func (s *PaymentsStarGiftActiveAuctions) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Auctions == nil) {
		return false
	}
	if !(s.Users == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsStarGiftActiveAuctions from given interface.
func (s *PaymentsStarGiftActiveAuctions) FillFrom(from interface {
	GetAuctions() (value []StarGiftActiveAuctionState)
	GetUsers() (value []UserClass)
}) {
	s.Auctions = from.GetAuctions()
	s.Users = from.GetUsers()
}

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

// TypeName returns name of type in TL schema.
func (*PaymentsStarGiftActiveAuctions) TypeName() string {
	return "payments.starGiftActiveAuctions"
}

// TypeInfo returns info about TL type.
func (s *PaymentsStarGiftActiveAuctions) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.starGiftActiveAuctions",
		ID:   PaymentsStarGiftActiveAuctionsTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Auctions",
			SchemaName: "auctions",
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *PaymentsStarGiftActiveAuctions) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode payments.starGiftActiveAuctions#97f187d8 as nil")
	}
	b.PutID(PaymentsStarGiftActiveAuctionsTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *PaymentsStarGiftActiveAuctions) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode payments.starGiftActiveAuctions#97f187d8 as nil")
	}
	b.PutVectorHeader(len(s.Auctions))
	for idx, v := range s.Auctions {
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.starGiftActiveAuctions#97f187d8: field auctions element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(s.Users))
	for idx, v := range s.Users {
		if v == nil {
			return fmt.Errorf("unable to encode payments.starGiftActiveAuctions#97f187d8: field users element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.starGiftActiveAuctions#97f187d8: field users element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *PaymentsStarGiftActiveAuctions) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode payments.starGiftActiveAuctions#97f187d8 to nil")
	}
	if err := b.ConsumeID(PaymentsStarGiftActiveAuctionsTypeID); err != nil {
		return fmt.Errorf("unable to decode payments.starGiftActiveAuctions#97f187d8: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *PaymentsStarGiftActiveAuctions) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode payments.starGiftActiveAuctions#97f187d8 to nil")
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.starGiftActiveAuctions#97f187d8: field auctions: %w", err)
		}

		if headerLen > 0 {
			s.Auctions = make([]StarGiftActiveAuctionState, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value StarGiftActiveAuctionState
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode payments.starGiftActiveAuctions#97f187d8: field auctions: %w", err)
			}
			s.Auctions = append(s.Auctions, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.starGiftActiveAuctions#97f187d8: field users: %w", err)
		}

		if headerLen > 0 {
			s.Users = make([]UserClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeUser(b)
			if err != nil {
				return fmt.Errorf("unable to decode payments.starGiftActiveAuctions#97f187d8: field users: %w", err)
			}
			s.Users = append(s.Users, value)
		}
	}
	return nil
}

// GetAuctions returns value of Auctions field.
func (s *PaymentsStarGiftActiveAuctions) GetAuctions() (value []StarGiftActiveAuctionState) {
	if s == nil {
		return
	}
	return s.Auctions
}

// GetUsers returns value of Users field.
func (s *PaymentsStarGiftActiveAuctions) GetUsers() (value []UserClass) {
	if s == nil {
		return
	}
	return s.Users
}

// MapUsers returns field Users wrapped in UserClassArray helper.
func (s *PaymentsStarGiftActiveAuctions) MapUsers() (value UserClassArray) {
	return UserClassArray(s.Users)
}

// PaymentsStarGiftActiveAuctionsClassName is schema name of PaymentsStarGiftActiveAuctionsClass.
const PaymentsStarGiftActiveAuctionsClassName = "payments.StarGiftActiveAuctions"

// PaymentsStarGiftActiveAuctionsClass represents payments.StarGiftActiveAuctions generic type.
//
// See https://core.telegram.org/type/payments.StarGiftActiveAuctions for reference.
//
// Example:
//
//	g, err := tg.DecodePaymentsStarGiftActiveAuctions(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tg.PaymentsStarGiftActiveAuctionsNotModified: // payments.starGiftActiveAuctionsNotModified#db33dad0
//	case *tg.PaymentsStarGiftActiveAuctions: // payments.starGiftActiveAuctions#97f187d8
//	default: panic(v)
//	}
type PaymentsStarGiftActiveAuctionsClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() PaymentsStarGiftActiveAuctionsClass

	// TypeID returns type id in TL schema.
	//
	// See https://core.telegram.org/mtproto/TL-tl#remarks.
	TypeID() uint32
	// TypeName returns name of type in TL schema.
	TypeName() string
	// String implements fmt.Stringer.
	String() string
	// Zero returns true if current object has a zero value.
	Zero() bool

	// AsModified tries to map PaymentsStarGiftActiveAuctionsClass to PaymentsStarGiftActiveAuctions.
	AsModified() (*PaymentsStarGiftActiveAuctions, bool)
}

// AsModified tries to map PaymentsStarGiftActiveAuctionsNotModified to PaymentsStarGiftActiveAuctions.
func (s *PaymentsStarGiftActiveAuctionsNotModified) AsModified() (*PaymentsStarGiftActiveAuctions, bool) {
	return nil, false
}

// AsModified tries to map PaymentsStarGiftActiveAuctions to PaymentsStarGiftActiveAuctions.
func (s *PaymentsStarGiftActiveAuctions) AsModified() (*PaymentsStarGiftActiveAuctions, bool) {
	return s, true
}

// DecodePaymentsStarGiftActiveAuctions implements binary de-serialization for PaymentsStarGiftActiveAuctionsClass.
func DecodePaymentsStarGiftActiveAuctions(buf *bin.Buffer) (PaymentsStarGiftActiveAuctionsClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case PaymentsStarGiftActiveAuctionsNotModifiedTypeID:
		// Decoding payments.starGiftActiveAuctionsNotModified#db33dad0.
		v := PaymentsStarGiftActiveAuctionsNotModified{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode PaymentsStarGiftActiveAuctionsClass: %w", err)
		}
		return &v, nil
	case PaymentsStarGiftActiveAuctionsTypeID:
		// Decoding payments.starGiftActiveAuctions#97f187d8.
		v := PaymentsStarGiftActiveAuctions{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode PaymentsStarGiftActiveAuctionsClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode PaymentsStarGiftActiveAuctionsClass: %w", bin.NewUnexpectedID(id))
	}
}

// PaymentsStarGiftActiveAuctions boxes the PaymentsStarGiftActiveAuctionsClass providing a helper.
type PaymentsStarGiftActiveAuctionsBox struct {
	StarGiftActiveAuctions PaymentsStarGiftActiveAuctionsClass
}

// Decode implements bin.Decoder for PaymentsStarGiftActiveAuctionsBox.
func (b *PaymentsStarGiftActiveAuctionsBox) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("unable to decode PaymentsStarGiftActiveAuctionsBox to nil")
	}
	v, err := DecodePaymentsStarGiftActiveAuctions(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.StarGiftActiveAuctions = v
	return nil
}

// Encode implements bin.Encode for PaymentsStarGiftActiveAuctionsBox.
func (b *PaymentsStarGiftActiveAuctionsBox) Encode(buf *bin.Buffer) error {
	if b == nil || b.StarGiftActiveAuctions == nil {
		return fmt.Errorf("unable to encode PaymentsStarGiftActiveAuctionsClass as nil")
	}
	return b.StarGiftActiveAuctions.Encode(buf)
}
