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

// PaymentsStarGiftAuctionAcquiredGifts represents TL type `payments.starGiftAuctionAcquiredGifts#7d5bd1f0`.
//
// See https://core.telegram.org/constructor/payments.starGiftAuctionAcquiredGifts for reference.
type PaymentsStarGiftAuctionAcquiredGifts struct {
	// Gifts field of PaymentsStarGiftAuctionAcquiredGifts.
	Gifts []StarGiftAuctionAcquiredGift
	// Users field of PaymentsStarGiftAuctionAcquiredGifts.
	Users []UserClass
	// Chats field of PaymentsStarGiftAuctionAcquiredGifts.
	Chats []ChatClass
}

// PaymentsStarGiftAuctionAcquiredGiftsTypeID is TL type id of PaymentsStarGiftAuctionAcquiredGifts.
const PaymentsStarGiftAuctionAcquiredGiftsTypeID = 0x7d5bd1f0

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

func (s *PaymentsStarGiftAuctionAcquiredGifts) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Gifts == nil) {
		return false
	}
	if !(s.Users == nil) {
		return false
	}
	if !(s.Chats == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsStarGiftAuctionAcquiredGifts from given interface.
func (s *PaymentsStarGiftAuctionAcquiredGifts) FillFrom(from interface {
	GetGifts() (value []StarGiftAuctionAcquiredGift)
	GetUsers() (value []UserClass)
	GetChats() (value []ChatClass)
}) {
	s.Gifts = from.GetGifts()
	s.Users = from.GetUsers()
	s.Chats = from.GetChats()
}

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

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

// TypeInfo returns info about TL type.
func (s *PaymentsStarGiftAuctionAcquiredGifts) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.starGiftAuctionAcquiredGifts",
		ID:   PaymentsStarGiftAuctionAcquiredGiftsTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Gifts",
			SchemaName: "gifts",
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
		{
			Name:       "Chats",
			SchemaName: "chats",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *PaymentsStarGiftAuctionAcquiredGifts) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode payments.starGiftAuctionAcquiredGifts#7d5bd1f0 as nil")
	}
	b.PutID(PaymentsStarGiftAuctionAcquiredGiftsTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *PaymentsStarGiftAuctionAcquiredGifts) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode payments.starGiftAuctionAcquiredGifts#7d5bd1f0 as nil")
	}
	b.PutVectorHeader(len(s.Gifts))
	for idx, v := range s.Gifts {
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.starGiftAuctionAcquiredGifts#7d5bd1f0: field gifts 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.starGiftAuctionAcquiredGifts#7d5bd1f0: field users element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.starGiftAuctionAcquiredGifts#7d5bd1f0: field users element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(s.Chats))
	for idx, v := range s.Chats {
		if v == nil {
			return fmt.Errorf("unable to encode payments.starGiftAuctionAcquiredGifts#7d5bd1f0: field chats element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.starGiftAuctionAcquiredGifts#7d5bd1f0: field chats element with index %d: %w", idx, err)
		}
	}
	return nil
}

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

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

		if headerLen > 0 {
			s.Gifts = make([]StarGiftAuctionAcquiredGift, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value StarGiftAuctionAcquiredGift
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode payments.starGiftAuctionAcquiredGifts#7d5bd1f0: field gifts: %w", err)
			}
			s.Gifts = append(s.Gifts, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.starGiftAuctionAcquiredGifts#7d5bd1f0: 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.starGiftAuctionAcquiredGifts#7d5bd1f0: field users: %w", err)
			}
			s.Users = append(s.Users, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.starGiftAuctionAcquiredGifts#7d5bd1f0: field chats: %w", err)
		}

		if headerLen > 0 {
			s.Chats = make([]ChatClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeChat(b)
			if err != nil {
				return fmt.Errorf("unable to decode payments.starGiftAuctionAcquiredGifts#7d5bd1f0: field chats: %w", err)
			}
			s.Chats = append(s.Chats, value)
		}
	}
	return nil
}

// GetGifts returns value of Gifts field.
func (s *PaymentsStarGiftAuctionAcquiredGifts) GetGifts() (value []StarGiftAuctionAcquiredGift) {
	if s == nil {
		return
	}
	return s.Gifts
}

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

// GetChats returns value of Chats field.
func (s *PaymentsStarGiftAuctionAcquiredGifts) GetChats() (value []ChatClass) {
	if s == nil {
		return
	}
	return s.Chats
}

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

// MapChats returns field Chats wrapped in ChatClassArray helper.
func (s *PaymentsStarGiftAuctionAcquiredGifts) MapChats() (value ChatClassArray) {
	return ChatClassArray(s.Chats)
}
