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

// PaymentsStarGiftUpgradePreview represents TL type `payments.starGiftUpgradePreview#167bd90b`.
//
// See https://core.telegram.org/constructor/payments.starGiftUpgradePreview for reference.
type PaymentsStarGiftUpgradePreview struct {
	// SampleAttributes field of PaymentsStarGiftUpgradePreview.
	SampleAttributes []StarGiftAttributeClass
}

// PaymentsStarGiftUpgradePreviewTypeID is TL type id of PaymentsStarGiftUpgradePreview.
const PaymentsStarGiftUpgradePreviewTypeID = 0x167bd90b

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

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

	return true
}

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

// FillFrom fills PaymentsStarGiftUpgradePreview from given interface.
func (s *PaymentsStarGiftUpgradePreview) FillFrom(from interface {
	GetSampleAttributes() (value []StarGiftAttributeClass)
}) {
	s.SampleAttributes = from.GetSampleAttributes()
}

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

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

// TypeInfo returns info about TL type.
func (s *PaymentsStarGiftUpgradePreview) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.starGiftUpgradePreview",
		ID:   PaymentsStarGiftUpgradePreviewTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "SampleAttributes",
			SchemaName: "sample_attributes",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *PaymentsStarGiftUpgradePreview) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode payments.starGiftUpgradePreview#167bd90b as nil")
	}
	b.PutID(PaymentsStarGiftUpgradePreviewTypeID)
	return s.EncodeBare(b)
}

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

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

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

		if headerLen > 0 {
			s.SampleAttributes = 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.starGiftUpgradePreview#167bd90b: field sample_attributes: %w", err)
			}
			s.SampleAttributes = append(s.SampleAttributes, value)
		}
	}
	return nil
}

// GetSampleAttributes returns value of SampleAttributes field.
func (s *PaymentsStarGiftUpgradePreview) GetSampleAttributes() (value []StarGiftAttributeClass) {
	if s == nil {
		return
	}
	return s.SampleAttributes
}

// MapSampleAttributes returns field SampleAttributes wrapped in StarGiftAttributeClassArray helper.
func (s *PaymentsStarGiftUpgradePreview) MapSampleAttributes() (value StarGiftAttributeClassArray) {
	return StarGiftAttributeClassArray(s.SampleAttributes)
}
