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

// TestCallVectorIntRequest represents TL type `testCallVectorInt#65ed7e72`.
type TestCallVectorIntRequest struct {
	// Vector of numbers to return
	X []int32
}

// TestCallVectorIntRequestTypeID is TL type id of TestCallVectorIntRequest.
const TestCallVectorIntRequestTypeID = 0x65ed7e72

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (t *TestCallVectorIntRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "testCallVectorInt",
		ID:   TestCallVectorIntRequestTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "X",
			SchemaName: "x",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *TestCallVectorIntRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode testCallVectorInt#65ed7e72 as nil")
	}
	b.PutID(TestCallVectorIntRequestTypeID)
	return t.EncodeBare(b)
}

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

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

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

		if headerLen > 0 {
			t.X = 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 testCallVectorInt#65ed7e72: field x: %w", err)
			}
			t.X = append(t.X, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (t *TestCallVectorIntRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if t == nil {
		return fmt.Errorf("can't encode testCallVectorInt#65ed7e72 as nil")
	}
	b.ObjStart()
	b.PutID("testCallVectorInt")
	b.Comma()
	b.FieldStart("x")
	b.ArrStart()
	for _, v := range t.X {
		b.PutInt32(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (t *TestCallVectorIntRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if t == nil {
		return fmt.Errorf("can't decode testCallVectorInt#65ed7e72 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("testCallVectorInt"); err != nil {
				return fmt.Errorf("unable to decode testCallVectorInt#65ed7e72: %w", err)
			}
		case "x":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int32()
				if err != nil {
					return fmt.Errorf("unable to decode testCallVectorInt#65ed7e72: field x: %w", err)
				}
				t.X = append(t.X, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode testCallVectorInt#65ed7e72: field x: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetX returns value of X field.
func (t *TestCallVectorIntRequest) GetX() (value []int32) {
	if t == nil {
		return
	}
	return t.X
}

// TestCallVectorInt invokes method testCallVectorInt#65ed7e72 returning error if any.
func (c *Client) TestCallVectorInt(ctx context.Context, x []int32) (*TestVectorInt, error) {
	var result TestVectorInt

	request := &TestCallVectorIntRequest{
		X: x,
	}
	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return &result, nil
}
