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

// IntVector is a box for Vector<int>
type IntVector struct {
	// Elements of Vector<int>
	Elems []int
}

// IntVectorTypeID is TL type id of IntVector.
const IntVectorTypeID = bin.TypeVector

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

func (vec *IntVector) Zero() bool {
	if vec == nil {
		return true
	}
	if !(vec.Elems == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (vec *IntVector) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "",
		ID:   IntVectorTypeID,
	}
	if vec == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Elems",
			SchemaName: "Elems",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (vec *IntVector) Encode(b *bin.Buffer) error {
	if vec == nil {
		return fmt.Errorf("can't encode Vector<int> as nil")
	}

	return vec.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (vec *IntVector) EncodeBare(b *bin.Buffer) error {
	if vec == nil {
		return fmt.Errorf("can't encode Vector<int> as nil")
	}
	b.PutVectorHeader(len(vec.Elems))
	for _, v := range vec.Elems {
		b.PutInt(v)
	}
	return nil
}

// Decode implements bin.Decoder.
func (vec *IntVector) Decode(b *bin.Buffer) error {
	if vec == nil {
		return fmt.Errorf("can't decode Vector<int> to nil")
	}

	return vec.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (vec *IntVector) DecodeBare(b *bin.Buffer) error {
	if vec == nil {
		return fmt.Errorf("can't decode Vector<int> to nil")
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode Vector<int>: field Elems: %w", err)
		}

		if headerLen > 0 {
			vec.Elems = make([]int, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int()
			if err != nil {
				return fmt.Errorf("unable to decode Vector<int>: field Elems: %w", err)
			}
			vec.Elems = append(vec.Elems, value)
		}
	}
	return nil
}

// GetElems returns value of Elems field.
func (vec *IntVector) GetElems() (value []int) {
	if vec == nil {
		return
	}
	return vec.Elems
}
