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

// TestCallVectorStringRequest represents TL type `testCallVectorString#8e5a6ed8`.
type TestCallVectorStringRequest struct {
	// Vector of strings to return
	X []string
}

// TestCallVectorStringRequestTypeID is TL type id of TestCallVectorStringRequest.
const TestCallVectorStringRequestTypeID = 0x8e5a6ed8

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

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (t *TestCallVectorStringRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode testCallVectorString#8e5a6ed8 as nil")
	}
	b.PutID(TestCallVectorStringRequestTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *TestCallVectorStringRequest) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode testCallVectorString#8e5a6ed8 as nil")
	}
	b.PutInt(len(t.X))
	for _, v := range t.X {
		b.PutString(v)
	}
	return nil
}

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

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

		if headerLen > 0 {
			t.X = make([]string, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode testCallVectorString#8e5a6ed8: field x: %w", err)
			}
			t.X = append(t.X, value)
		}
	}
	return nil
}

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

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (t *TestCallVectorStringRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if t == nil {
		return fmt.Errorf("can't decode testCallVectorString#8e5a6ed8 to nil")
	}

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

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

// TestCallVectorString invokes method testCallVectorString#8e5a6ed8 returning error if any.
func (c *Client) TestCallVectorString(ctx context.Context, x []string) (*TestVectorString, error) {
	var result TestVectorString

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