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

// BusinessMessages represents TL type `businessMessages#355963fa`.
type BusinessMessages struct {
	// List of business messages
	Messages []BusinessMessage
}

// BusinessMessagesTypeID is TL type id of BusinessMessages.
const BusinessMessagesTypeID = 0x355963fa

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

func (b *BusinessMessages) Zero() bool {
	if b == nil {
		return true
	}
	if !(b.Messages == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (b *BusinessMessages) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "businessMessages",
		ID:   BusinessMessagesTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Messages",
			SchemaName: "messages",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (b *BusinessMessages) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode businessMessages#355963fa as nil")
	}
	buf.PutID(BusinessMessagesTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *BusinessMessages) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode businessMessages#355963fa as nil")
	}
	buf.PutInt(len(b.Messages))
	for idx, v := range b.Messages {
		if err := v.EncodeBare(buf); err != nil {
			return fmt.Errorf("unable to encode bare businessMessages#355963fa: field messages element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (b *BusinessMessages) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode businessMessages#355963fa to nil")
	}
	if err := buf.ConsumeID(BusinessMessagesTypeID); err != nil {
		return fmt.Errorf("unable to decode businessMessages#355963fa: %w", err)
	}
	return b.DecodeBare(buf)
}

// DecodeBare implements bin.BareDecoder.
func (b *BusinessMessages) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode businessMessages#355963fa to nil")
	}
	{
		headerLen, err := buf.Int()
		if err != nil {
			return fmt.Errorf("unable to decode businessMessages#355963fa: field messages: %w", err)
		}

		if headerLen > 0 {
			b.Messages = make([]BusinessMessage, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value BusinessMessage
			if err := value.DecodeBare(buf); err != nil {
				return fmt.Errorf("unable to decode bare businessMessages#355963fa: field messages: %w", err)
			}
			b.Messages = append(b.Messages, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (b *BusinessMessages) EncodeTDLibJSON(buf tdjson.Encoder) error {
	if b == nil {
		return fmt.Errorf("can't encode businessMessages#355963fa as nil")
	}
	buf.ObjStart()
	buf.PutID("businessMessages")
	buf.Comma()
	buf.FieldStart("messages")
	buf.ArrStart()
	for idx, v := range b.Messages {
		if err := v.EncodeTDLibJSON(buf); err != nil {
			return fmt.Errorf("unable to encode businessMessages#355963fa: field messages element with index %d: %w", idx, err)
		}
		buf.Comma()
	}
	buf.StripComma()
	buf.ArrEnd()
	buf.Comma()
	buf.StripComma()
	buf.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (b *BusinessMessages) DecodeTDLibJSON(buf tdjson.Decoder) error {
	if b == nil {
		return fmt.Errorf("can't decode businessMessages#355963fa to nil")
	}

	return buf.Obj(func(buf tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := buf.ConsumeID("businessMessages"); err != nil {
				return fmt.Errorf("unable to decode businessMessages#355963fa: %w", err)
			}
		case "messages":
			if err := buf.Arr(func(buf tdjson.Decoder) error {
				var value BusinessMessage
				if err := value.DecodeTDLibJSON(buf); err != nil {
					return fmt.Errorf("unable to decode businessMessages#355963fa: field messages: %w", err)
				}
				b.Messages = append(b.Messages, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode businessMessages#355963fa: field messages: %w", err)
			}
		default:
			return buf.Skip()
		}
		return nil
	})
}

// GetMessages returns value of Messages field.
func (b *BusinessMessages) GetMessages() (value []BusinessMessage) {
	if b == nil {
		return
	}
	return b.Messages
}
