// Code generated by gotdgen, DO NOT EDIT.

package td

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

// TestVectorInt represents TL type `testVectorInt#df9eb113`.
//
// See https://localhost:80/doc/constructor/testVectorInt for reference.
type TestVectorInt struct {
	// Vector of numbers
	Value []int32
}

// TestVectorIntTypeID is TL type id of TestVectorInt.
const TestVectorIntTypeID = 0xdf9eb113

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

func (t *TestVectorInt) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.Value == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (t *TestVectorInt) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "testVectorInt",
		ID:   TestVectorIntTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Value",
			SchemaName: "value",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *TestVectorInt) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode testVectorInt#df9eb113 as nil")
	}
	b.PutID(TestVectorIntTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *TestVectorInt) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode testVectorInt#df9eb113 as nil")
	}
	b.PutInt(len(t.Value))
	for _, v := range t.Value {
		b.PutInt32(v)
	}
	return nil
}

// Decode implements bin.Decoder.
func (t *TestVectorInt) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode testVectorInt#df9eb113 to nil")
	}
	if err := b.ConsumeID(TestVectorIntTypeID); err != nil {
		return fmt.Errorf("unable to decode testVectorInt#df9eb113: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *TestVectorInt) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode testVectorInt#df9eb113 to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode testVectorInt#df9eb113: field value: %w", err)
		}

		if headerLen > 0 {
			t.Value = make([]int32, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode testVectorInt#df9eb113: field value: %w", err)
			}
			t.Value = append(t.Value, value)
		}
	}
	return nil
}

// GetValue returns value of Value field.
func (t *TestVectorInt) GetValue() (value []int32) {
	if t == nil {
		return
	}
	return t.Value
}
