// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// MessagesTranslateResult represents TL type `messages.translateResult#33db32f8`.
// Translated text with entities¹
//
// Links:
//  1. https://core.telegram.org/api/entities
//
// See https://core.telegram.org/constructor/messages.translateResult for reference.
type MessagesTranslateResult struct {
	// Text+entities¹, for each input message.
	//
	// Links:
	//  1) https://core.telegram.org/api/entities
	Result []TextWithEntities
}

// MessagesTranslateResultTypeID is TL type id of MessagesTranslateResult.
const MessagesTranslateResultTypeID = 0x33db32f8

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

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

	return true
}

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

// FillFrom fills MessagesTranslateResult from given interface.
func (t *MessagesTranslateResult) FillFrom(from interface {
	GetResult() (value []TextWithEntities)
}) {
	t.Result = from.GetResult()
}

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

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

// TypeInfo returns info about TL type.
func (t *MessagesTranslateResult) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.translateResult",
		ID:   MessagesTranslateResultTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Result",
			SchemaName: "result",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *MessagesTranslateResult) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode messages.translateResult#33db32f8 as nil")
	}
	b.PutID(MessagesTranslateResultTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *MessagesTranslateResult) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode messages.translateResult#33db32f8 as nil")
	}
	b.PutVectorHeader(len(t.Result))
	for idx, v := range t.Result {
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.translateResult#33db32f8: field result element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (t *MessagesTranslateResult) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode messages.translateResult#33db32f8 to nil")
	}
	if err := b.ConsumeID(MessagesTranslateResultTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.translateResult#33db32f8: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *MessagesTranslateResult) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode messages.translateResult#33db32f8 to nil")
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.translateResult#33db32f8: field result: %w", err)
		}

		if headerLen > 0 {
			t.Result = make([]TextWithEntities, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value TextWithEntities
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode messages.translateResult#33db32f8: field result: %w", err)
			}
			t.Result = append(t.Result, value)
		}
	}
	return nil
}

// GetResult returns value of Result field.
func (t *MessagesTranslateResult) GetResult() (value []TextWithEntities) {
	if t == nil {
		return
	}
	return t.Result
}
