// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	
	
	
	
	

	

	
	
	
	
)

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

// MessagesSponsoredMessages represents TL type `messages.sponsoredMessages#c9ee1d87`.
// A set of sponsored messages associated to a channel
//
// See https://core.telegram.org/constructor/messages.sponsoredMessages for reference.
type MessagesSponsoredMessages struct {
	// Flags, see TL conditional fieldsĀ¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// If set, specifies the minimum number of messages between shown sponsored messages;
	// otherwise, only one sponsored message must be shown after all ordinary messages.
	//
	// Use SetPostsBetween and GetPostsBetween helpers.
	PostsBetween int
	// Sponsored messages
	Messages []SponsoredMessage
	// Chats mentioned in the sponsored messages
	Chats []ChatClass
	// Users mentioned in the sponsored messages
	Users []UserClass
}

// MessagesSponsoredMessagesTypeID is TL type id of MessagesSponsoredMessages.
const MessagesSponsoredMessagesTypeID = 0xc9ee1d87

// construct implements constructor of MessagesSponsoredMessagesClass.
func ( MessagesSponsoredMessages) () MessagesSponsoredMessagesClass { return & }

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

	_ MessagesSponsoredMessagesClass = &MessagesSponsoredMessages{}
)

func ( *MessagesSponsoredMessages) () bool {
	if  == nil {
		return true
	}
	if !(.Flags.Zero()) {
		return false
	}
	if !(.PostsBetween == 0) {
		return false
	}
	if !(.Messages == nil) {
		return false
	}
	if !(.Chats == nil) {
		return false
	}
	if !(.Users == nil) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesSponsoredMessages from given interface.
func ( *MessagesSponsoredMessages) ( interface {
	() ( int,  bool)
	() ( []SponsoredMessage)
	() ( []ChatClass)
	() ( []UserClass)
}) {
	if ,  := .();  {
		.PostsBetween = 
	}

	.Messages = .()
	.Chats = .()
	.Users = .()
}

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

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

// TypeInfo returns info about TL type.
func ( *MessagesSponsoredMessages) () tdp.Type {
	 := tdp.Type{
		Name: "messages.sponsoredMessages",
		ID:   MessagesSponsoredMessagesTypeID,
	}
	if  == nil {
		.Null = true
		return 
	}
	.Fields = []tdp.Field{
		{
			Name:       "PostsBetween",
			SchemaName: "posts_between",
			Null:       !.Flags.Has(0),
		},
		{
			Name:       "Messages",
			SchemaName: "messages",
		},
		{
			Name:       "Chats",
			SchemaName: "chats",
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
	}
	return 
}

// SetFlags sets flags for non-zero fields.
func ( *MessagesSponsoredMessages) () {
	if !(.PostsBetween == 0) {
		.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func ( *MessagesSponsoredMessages) ( *bin.Buffer) error {
	if  == nil {
		return fmt.Errorf("can't encode messages.sponsoredMessages#c9ee1d87 as nil")
	}
	.PutID(MessagesSponsoredMessagesTypeID)
	return .EncodeBare()
}

// EncodeBare implements bin.BareEncoder.
func ( *MessagesSponsoredMessages) ( *bin.Buffer) error {
	if  == nil {
		return fmt.Errorf("can't encode messages.sponsoredMessages#c9ee1d87 as nil")
	}
	.SetFlags()
	if  := .Flags.Encode();  != nil {
		return fmt.Errorf("unable to encode messages.sponsoredMessages#c9ee1d87: field flags: %w", )
	}
	if .Flags.Has(0) {
		.PutInt(.PostsBetween)
	}
	.PutVectorHeader(len(.Messages))
	for ,  := range .Messages {
		if  := .Encode();  != nil {
			return fmt.Errorf("unable to encode messages.sponsoredMessages#c9ee1d87: field messages element with index %d: %w", , )
		}
	}
	.PutVectorHeader(len(.Chats))
	for ,  := range .Chats {
		if  == nil {
			return fmt.Errorf("unable to encode messages.sponsoredMessages#c9ee1d87: field chats element with index %d is nil", )
		}
		if  := .Encode();  != nil {
			return fmt.Errorf("unable to encode messages.sponsoredMessages#c9ee1d87: field chats element with index %d: %w", , )
		}
	}
	.PutVectorHeader(len(.Users))
	for ,  := range .Users {
		if  == nil {
			return fmt.Errorf("unable to encode messages.sponsoredMessages#c9ee1d87: field users element with index %d is nil", )
		}
		if  := .Encode();  != nil {
			return fmt.Errorf("unable to encode messages.sponsoredMessages#c9ee1d87: field users element with index %d: %w", , )
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func ( *MessagesSponsoredMessages) ( *bin.Buffer) error {
	if  == nil {
		return fmt.Errorf("can't decode messages.sponsoredMessages#c9ee1d87 to nil")
	}
	if  := .ConsumeID(MessagesSponsoredMessagesTypeID);  != nil {
		return fmt.Errorf("unable to decode messages.sponsoredMessages#c9ee1d87: %w", )
	}
	return .DecodeBare()
}

// DecodeBare implements bin.BareDecoder.
func ( *MessagesSponsoredMessages) ( *bin.Buffer) error {
	if  == nil {
		return fmt.Errorf("can't decode messages.sponsoredMessages#c9ee1d87 to nil")
	}
	{
		if  := .Flags.Decode();  != nil {
			return fmt.Errorf("unable to decode messages.sponsoredMessages#c9ee1d87: field flags: %w", )
		}
	}
	if .Flags.Has(0) {
		,  := .Int()
		if  != nil {
			return fmt.Errorf("unable to decode messages.sponsoredMessages#c9ee1d87: field posts_between: %w", )
		}
		.PostsBetween = 
	}
	{
		,  := .VectorHeader()
		if  != nil {
			return fmt.Errorf("unable to decode messages.sponsoredMessages#c9ee1d87: field messages: %w", )
		}

		if  > 0 {
			.Messages = make([]SponsoredMessage, 0, %bin.PreallocateLimit)
		}
		for  := 0;  < ; ++ {
			var  SponsoredMessage
			if  := .Decode();  != nil {
				return fmt.Errorf("unable to decode messages.sponsoredMessages#c9ee1d87: field messages: %w", )
			}
			.Messages = append(.Messages, )
		}
	}
	{
		,  := .VectorHeader()
		if  != nil {
			return fmt.Errorf("unable to decode messages.sponsoredMessages#c9ee1d87: field chats: %w", )
		}

		if  > 0 {
			.Chats = make([]ChatClass, 0, %bin.PreallocateLimit)
		}
		for  := 0;  < ; ++ {
			,  := DecodeChat()
			if  != nil {
				return fmt.Errorf("unable to decode messages.sponsoredMessages#c9ee1d87: field chats: %w", )
			}
			.Chats = append(.Chats, )
		}
	}
	{
		,  := .VectorHeader()
		if  != nil {
			return fmt.Errorf("unable to decode messages.sponsoredMessages#c9ee1d87: field users: %w", )
		}

		if  > 0 {
			.Users = make([]UserClass, 0, %bin.PreallocateLimit)
		}
		for  := 0;  < ; ++ {
			,  := DecodeUser()
			if  != nil {
				return fmt.Errorf("unable to decode messages.sponsoredMessages#c9ee1d87: field users: %w", )
			}
			.Users = append(.Users, )
		}
	}
	return nil
}

// SetPostsBetween sets value of PostsBetween conditional field.
func ( *MessagesSponsoredMessages) ( int) {
	.Flags.Set(0)
	.PostsBetween = 
}

// GetPostsBetween returns value of PostsBetween conditional field and
// boolean which is true if field was set.
func ( *MessagesSponsoredMessages) () ( int,  bool) {
	if  == nil {
		return
	}
	if !.Flags.Has(0) {
		return , false
	}
	return .PostsBetween, true
}

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

// GetChats returns value of Chats field.
func ( *MessagesSponsoredMessages) () ( []ChatClass) {
	if  == nil {
		return
	}
	return .Chats
}

// GetUsers returns value of Users field.
func ( *MessagesSponsoredMessages) () ( []UserClass) {
	if  == nil {
		return
	}
	return .Users
}

// MapChats returns field Chats wrapped in ChatClassArray helper.
func ( *MessagesSponsoredMessages) () ( ChatClassArray) {
	return ChatClassArray(.Chats)
}

// MapUsers returns field Users wrapped in UserClassArray helper.
func ( *MessagesSponsoredMessages) () ( UserClassArray) {
	return UserClassArray(.Users)
}

// MessagesSponsoredMessagesEmpty represents TL type `messages.sponsoredMessagesEmpty#1839490f`.
// No sponsored messages are available.
//
// See https://core.telegram.org/constructor/messages.sponsoredMessagesEmpty for reference.
type MessagesSponsoredMessagesEmpty struct {
}

// MessagesSponsoredMessagesEmptyTypeID is TL type id of MessagesSponsoredMessagesEmpty.
const MessagesSponsoredMessagesEmptyTypeID = 0x1839490f

// construct implements constructor of MessagesSponsoredMessagesClass.
func ( MessagesSponsoredMessagesEmpty) () MessagesSponsoredMessagesClass { return & }

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

	_ MessagesSponsoredMessagesClass = &MessagesSponsoredMessagesEmpty{}
)

func ( *MessagesSponsoredMessagesEmpty) () bool {
	if  == nil {
		return true
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func ( *MessagesSponsoredMessagesEmpty) () tdp.Type {
	 := tdp.Type{
		Name: "messages.sponsoredMessagesEmpty",
		ID:   MessagesSponsoredMessagesEmptyTypeID,
	}
	if  == nil {
		.Null = true
		return 
	}
	.Fields = []tdp.Field{}
	return 
}

// Encode implements bin.Encoder.
func ( *MessagesSponsoredMessagesEmpty) ( *bin.Buffer) error {
	if  == nil {
		return fmt.Errorf("can't encode messages.sponsoredMessagesEmpty#1839490f as nil")
	}
	.PutID(MessagesSponsoredMessagesEmptyTypeID)
	return .EncodeBare()
}

// EncodeBare implements bin.BareEncoder.
func ( *MessagesSponsoredMessagesEmpty) ( *bin.Buffer) error {
	if  == nil {
		return fmt.Errorf("can't encode messages.sponsoredMessagesEmpty#1839490f as nil")
	}
	return nil
}

// Decode implements bin.Decoder.
func ( *MessagesSponsoredMessagesEmpty) ( *bin.Buffer) error {
	if  == nil {
		return fmt.Errorf("can't decode messages.sponsoredMessagesEmpty#1839490f to nil")
	}
	if  := .ConsumeID(MessagesSponsoredMessagesEmptyTypeID);  != nil {
		return fmt.Errorf("unable to decode messages.sponsoredMessagesEmpty#1839490f: %w", )
	}
	return .DecodeBare()
}

// DecodeBare implements bin.BareDecoder.
func ( *MessagesSponsoredMessagesEmpty) ( *bin.Buffer) error {
	if  == nil {
		return fmt.Errorf("can't decode messages.sponsoredMessagesEmpty#1839490f to nil")
	}
	return nil
}

// MessagesSponsoredMessagesClassName is schema name of MessagesSponsoredMessagesClass.
const MessagesSponsoredMessagesClassName = "messages.SponsoredMessages"

// MessagesSponsoredMessagesClass represents messages.SponsoredMessages generic type.
//
// See https://core.telegram.org/type/messages.SponsoredMessages for reference.
//
// Example:
//
//	g, err := tg.DecodeMessagesSponsoredMessages(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tg.MessagesSponsoredMessages: // messages.sponsoredMessages#c9ee1d87
//	case *tg.MessagesSponsoredMessagesEmpty: // messages.sponsoredMessagesEmpty#1839490f
//	default: panic(v)
//	}
type MessagesSponsoredMessagesClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() MessagesSponsoredMessagesClass

	// TypeID returns type id in TL schema.
	//
	// See https://core.telegram.org/mtproto/TL-tl#remarks.
	TypeID() uint32
	// TypeName returns name of type in TL schema.
	TypeName() string
	// String implements fmt.Stringer.
	String() string
	// Zero returns true if current object has a zero value.
	Zero() bool

	// AsNotEmpty tries to map MessagesSponsoredMessagesClass to MessagesSponsoredMessages.
	AsNotEmpty() (*MessagesSponsoredMessages, bool)
}

// AsNotEmpty tries to map MessagesSponsoredMessages to MessagesSponsoredMessages.
func ( *MessagesSponsoredMessages) () (*MessagesSponsoredMessages, bool) {
	return , true
}

// AsNotEmpty tries to map MessagesSponsoredMessagesEmpty to MessagesSponsoredMessages.
func ( *MessagesSponsoredMessagesEmpty) () (*MessagesSponsoredMessages, bool) {
	return nil, false
}

// DecodeMessagesSponsoredMessages implements binary de-serialization for MessagesSponsoredMessagesClass.
func ( *bin.Buffer) (MessagesSponsoredMessagesClass, error) {
	,  := .PeekID()
	if  != nil {
		return nil, 
	}
	switch  {
	case MessagesSponsoredMessagesTypeID:
		// Decoding messages.sponsoredMessages#c9ee1d87.
		 := MessagesSponsoredMessages{}
		if  := .Decode();  != nil {
			return nil, fmt.Errorf("unable to decode MessagesSponsoredMessagesClass: %w", )
		}
		return &, nil
	case MessagesSponsoredMessagesEmptyTypeID:
		// Decoding messages.sponsoredMessagesEmpty#1839490f.
		 := MessagesSponsoredMessagesEmpty{}
		if  := .Decode();  != nil {
			return nil, fmt.Errorf("unable to decode MessagesSponsoredMessagesClass: %w", )
		}
		return &, nil
	default:
		return nil, fmt.Errorf("unable to decode MessagesSponsoredMessagesClass: %w", bin.NewUnexpectedID())
	}
}

// MessagesSponsoredMessages boxes the MessagesSponsoredMessagesClass providing a helper.
type MessagesSponsoredMessagesBox struct {
	SponsoredMessages MessagesSponsoredMessagesClass
}

// Decode implements bin.Decoder for MessagesSponsoredMessagesBox.
func ( *MessagesSponsoredMessagesBox) ( *bin.Buffer) error {
	if  == nil {
		return fmt.Errorf("unable to decode MessagesSponsoredMessagesBox to nil")
	}
	,  := DecodeMessagesSponsoredMessages()
	if  != nil {
		return fmt.Errorf("unable to decode boxed value: %w", )
	}
	.SponsoredMessages = 
	return nil
}

// Encode implements bin.Encode for MessagesSponsoredMessagesBox.
func ( *MessagesSponsoredMessagesBox) ( *bin.Buffer) error {
	if  == nil || .SponsoredMessages == nil {
		return fmt.Errorf("unable to encode MessagesSponsoredMessagesClass as nil")
	}
	return .SponsoredMessages.Encode()
}