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

// AvailableGifts represents TL type `availableGifts#5ce74501`.
type AvailableGifts struct {
	// The list of gifts
	Gifts []AvailableGift
}

// AvailableGiftsTypeID is TL type id of AvailableGifts.
const AvailableGiftsTypeID = 0x5ce74501

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

func (a *AvailableGifts) Zero() bool {
	if a == nil {
		return true
	}
	if !(a.Gifts == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (a *AvailableGifts) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "availableGifts",
		ID:   AvailableGiftsTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Gifts",
			SchemaName: "gifts",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (a *AvailableGifts) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode availableGifts#5ce74501 as nil")
	}
	b.PutID(AvailableGiftsTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *AvailableGifts) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode availableGifts#5ce74501 as nil")
	}
	b.PutInt(len(a.Gifts))
	for idx, v := range a.Gifts {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare availableGifts#5ce74501: field gifts element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (a *AvailableGifts) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode availableGifts#5ce74501 to nil")
	}
	if err := b.ConsumeID(AvailableGiftsTypeID); err != nil {
		return fmt.Errorf("unable to decode availableGifts#5ce74501: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *AvailableGifts) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode availableGifts#5ce74501 to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode availableGifts#5ce74501: field gifts: %w", err)
		}

		if headerLen > 0 {
			a.Gifts = make([]AvailableGift, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value AvailableGift
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare availableGifts#5ce74501: field gifts: %w", err)
			}
			a.Gifts = append(a.Gifts, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (a *AvailableGifts) EncodeTDLibJSON(b tdjson.Encoder) error {
	if a == nil {
		return fmt.Errorf("can't encode availableGifts#5ce74501 as nil")
	}
	b.ObjStart()
	b.PutID("availableGifts")
	b.Comma()
	b.FieldStart("gifts")
	b.ArrStart()
	for idx, v := range a.Gifts {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode availableGifts#5ce74501: field gifts 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 (a *AvailableGifts) DecodeTDLibJSON(b tdjson.Decoder) error {
	if a == nil {
		return fmt.Errorf("can't decode availableGifts#5ce74501 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("availableGifts"); err != nil {
				return fmt.Errorf("unable to decode availableGifts#5ce74501: %w", err)
			}
		case "gifts":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value AvailableGift
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode availableGifts#5ce74501: field gifts: %w", err)
				}
				a.Gifts = append(a.Gifts, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode availableGifts#5ce74501: field gifts: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetGifts returns value of Gifts field.
func (a *AvailableGifts) GetGifts() (value []AvailableGift) {
	if a == nil {
		return
	}
	return a.Gifts
}
