// Code generated by gotdgen, DO NOT EDIT.

package tdapi

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

// StarPaymentOptions represents TL type `starPaymentOptions#77c3bbad`.
type StarPaymentOptions struct {
	// The list of options
	Options []StarPaymentOption
}

// StarPaymentOptionsTypeID is TL type id of StarPaymentOptions.
const StarPaymentOptionsTypeID = 0x77c3bbad

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

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

	return true
}

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

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

// TypeName returns name of type in TL schema.
func (*StarPaymentOptions) TypeName() string {
	return "starPaymentOptions"
}

// TypeInfo returns info about TL type.
func (s *StarPaymentOptions) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "starPaymentOptions",
		ID:   StarPaymentOptionsTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Options",
			SchemaName: "options",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *StarPaymentOptions) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starPaymentOptions#77c3bbad as nil")
	}
	b.PutID(StarPaymentOptionsTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StarPaymentOptions) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starPaymentOptions#77c3bbad as nil")
	}
	b.PutInt(len(s.Options))
	for idx, v := range s.Options {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare starPaymentOptions#77c3bbad: field options element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *StarPaymentOptions) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode starPaymentOptions#77c3bbad to nil")
	}
	if err := b.ConsumeID(StarPaymentOptionsTypeID); err != nil {
		return fmt.Errorf("unable to decode starPaymentOptions#77c3bbad: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *StarPaymentOptions) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode starPaymentOptions#77c3bbad to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode starPaymentOptions#77c3bbad: field options: %w", err)
		}

		if headerLen > 0 {
			s.Options = make([]StarPaymentOption, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value StarPaymentOption
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare starPaymentOptions#77c3bbad: field options: %w", err)
			}
			s.Options = append(s.Options, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *StarPaymentOptions) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode starPaymentOptions#77c3bbad as nil")
	}
	b.ObjStart()
	b.PutID("starPaymentOptions")
	b.Comma()
	b.FieldStart("options")
	b.ArrStart()
	for idx, v := range s.Options {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode starPaymentOptions#77c3bbad: field options element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *StarPaymentOptions) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode starPaymentOptions#77c3bbad to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("starPaymentOptions"); err != nil {
				return fmt.Errorf("unable to decode starPaymentOptions#77c3bbad: %w", err)
			}
		case "options":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value StarPaymentOption
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode starPaymentOptions#77c3bbad: field options: %w", err)
				}
				s.Options = append(s.Options, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode starPaymentOptions#77c3bbad: field options: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetOptions returns value of Options field.
func (s *StarPaymentOptions) GetOptions() (value []StarPaymentOption) {
	if s == nil {
		return
	}
	return s.Options
}
