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

// PaymentsStarGiftUpgradeAttributes represents TL type `payments.starGiftUpgradeAttributes#46c6e36f`.
//
// See https://core.telegram.org/constructor/payments.starGiftUpgradeAttributes for reference.
type PaymentsStarGiftUpgradeAttributes struct {
	// Attributes field of PaymentsStarGiftUpgradeAttributes.
	Attributes []StarGiftAttributeClass
}

// PaymentsStarGiftUpgradeAttributesTypeID is TL type id of PaymentsStarGiftUpgradeAttributes.
const PaymentsStarGiftUpgradeAttributesTypeID = 0x46c6e36f

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

func (s *PaymentsStarGiftUpgradeAttributes) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Attributes == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsStarGiftUpgradeAttributes from given interface.
func (s *PaymentsStarGiftUpgradeAttributes) FillFrom(from interface {
	GetAttributes() (value []StarGiftAttributeClass)
}) {
	s.Attributes = from.GetAttributes()
}

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

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

// TypeInfo returns info about TL type.
func (s *PaymentsStarGiftUpgradeAttributes) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.starGiftUpgradeAttributes",
		ID:   PaymentsStarGiftUpgradeAttributesTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Attributes",
			SchemaName: "attributes",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *PaymentsStarGiftUpgradeAttributes) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode payments.starGiftUpgradeAttributes#46c6e36f as nil")
	}
	b.PutID(PaymentsStarGiftUpgradeAttributesTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *PaymentsStarGiftUpgradeAttributes) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode payments.starGiftUpgradeAttributes#46c6e36f as nil")
	}
	b.PutVectorHeader(len(s.Attributes))
	for idx, v := range s.Attributes {
		if v == nil {
			return fmt.Errorf("unable to encode payments.starGiftUpgradeAttributes#46c6e36f: field attributes element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.starGiftUpgradeAttributes#46c6e36f: field attributes element with index %d: %w", idx, err)
		}
	}
	return nil
}

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

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

		if headerLen > 0 {
			s.Attributes = make([]StarGiftAttributeClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeStarGiftAttribute(b)
			if err != nil {
				return fmt.Errorf("unable to decode payments.starGiftUpgradeAttributes#46c6e36f: field attributes: %w", err)
			}
			s.Attributes = append(s.Attributes, value)
		}
	}
	return nil
}

// GetAttributes returns value of Attributes field.
func (s *PaymentsStarGiftUpgradeAttributes) GetAttributes() (value []StarGiftAttributeClass) {
	if s == nil {
		return
	}
	return s.Attributes
}

// MapAttributes returns field Attributes wrapped in StarGiftAttributeClassArray helper.
func (s *PaymentsStarGiftUpgradeAttributes) MapAttributes() (value StarGiftAttributeClassArray) {
	return StarGiftAttributeClassArray(s.Attributes)
}
