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

// ChatMessageSenders represents TL type `chatMessageSenders#d80d336`.
type ChatMessageSenders struct {
	// List of available message senders
	Senders []ChatMessageSender
}

// ChatMessageSendersTypeID is TL type id of ChatMessageSenders.
const ChatMessageSendersTypeID = 0xd80d336

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

func (c *ChatMessageSenders) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Senders == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *ChatMessageSenders) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "chatMessageSenders",
		ID:   ChatMessageSendersTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Senders",
			SchemaName: "senders",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *ChatMessageSenders) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatMessageSenders#d80d336 as nil")
	}
	b.PutID(ChatMessageSendersTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ChatMessageSenders) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatMessageSenders#d80d336 as nil")
	}
	b.PutInt(len(c.Senders))
	for idx, v := range c.Senders {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare chatMessageSenders#d80d336: field senders element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (c *ChatMessageSenders) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatMessageSenders#d80d336 to nil")
	}
	if err := b.ConsumeID(ChatMessageSendersTypeID); err != nil {
		return fmt.Errorf("unable to decode chatMessageSenders#d80d336: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *ChatMessageSenders) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatMessageSenders#d80d336 to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode chatMessageSenders#d80d336: field senders: %w", err)
		}

		if headerLen > 0 {
			c.Senders = make([]ChatMessageSender, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value ChatMessageSender
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare chatMessageSenders#d80d336: field senders: %w", err)
			}
			c.Senders = append(c.Senders, value)
		}
	}
	return nil
}

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

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *ChatMessageSenders) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode chatMessageSenders#d80d336 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("chatMessageSenders"); err != nil {
				return fmt.Errorf("unable to decode chatMessageSenders#d80d336: %w", err)
			}
		case "senders":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value ChatMessageSender
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode chatMessageSenders#d80d336: field senders: %w", err)
				}
				c.Senders = append(c.Senders, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode chatMessageSenders#d80d336: field senders: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetSenders returns value of Senders field.
func (c *ChatMessageSenders) GetSenders() (value []ChatMessageSender) {
	if c == nil {
		return
	}
	return c.Senders
}
