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

// AccountBusinessChatLinks represents TL type `account.businessChatLinks#ec43a2d1`.
// Contains info about business chat deep links »¹ created by the current account.
//
// Links:
//  1. https://core.telegram.org/api/business#business-chat-links
//
// See https://core.telegram.org/constructor/account.businessChatLinks for reference.
type AccountBusinessChatLinks struct {
	// Links
	Links []BusinessChatLink
	// Mentioned chats
	Chats []ChatClass
	// Mentioned users
	Users []UserClass
}

// AccountBusinessChatLinksTypeID is TL type id of AccountBusinessChatLinks.
const AccountBusinessChatLinksTypeID = 0xec43a2d1

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

func (b *AccountBusinessChatLinks) Zero() bool {
	if b == nil {
		return true
	}
	if !(b.Links == nil) {
		return false
	}
	if !(b.Chats == nil) {
		return false
	}
	if !(b.Users == nil) {
		return false
	}

	return true
}

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

// FillFrom fills AccountBusinessChatLinks from given interface.
func (b *AccountBusinessChatLinks) FillFrom(from interface {
	GetLinks() (value []BusinessChatLink)
	GetChats() (value []ChatClass)
	GetUsers() (value []UserClass)
}) {
	b.Links = from.GetLinks()
	b.Chats = from.GetChats()
	b.Users = from.GetUsers()
}

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

// TypeName returns name of type in TL schema.
func (*AccountBusinessChatLinks) TypeName() string {
	return "account.businessChatLinks"
}

// TypeInfo returns info about TL type.
func (b *AccountBusinessChatLinks) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "account.businessChatLinks",
		ID:   AccountBusinessChatLinksTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Links",
			SchemaName: "links",
		},
		{
			Name:       "Chats",
			SchemaName: "chats",
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (b *AccountBusinessChatLinks) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode account.businessChatLinks#ec43a2d1 as nil")
	}
	buf.PutID(AccountBusinessChatLinksTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *AccountBusinessChatLinks) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode account.businessChatLinks#ec43a2d1 as nil")
	}
	buf.PutVectorHeader(len(b.Links))
	for idx, v := range b.Links {
		if err := v.Encode(buf); err != nil {
			return fmt.Errorf("unable to encode account.businessChatLinks#ec43a2d1: field links element with index %d: %w", idx, err)
		}
	}
	buf.PutVectorHeader(len(b.Chats))
	for idx, v := range b.Chats {
		if v == nil {
			return fmt.Errorf("unable to encode account.businessChatLinks#ec43a2d1: field chats element with index %d is nil", idx)
		}
		if err := v.Encode(buf); err != nil {
			return fmt.Errorf("unable to encode account.businessChatLinks#ec43a2d1: field chats element with index %d: %w", idx, err)
		}
	}
	buf.PutVectorHeader(len(b.Users))
	for idx, v := range b.Users {
		if v == nil {
			return fmt.Errorf("unable to encode account.businessChatLinks#ec43a2d1: field users element with index %d is nil", idx)
		}
		if err := v.Encode(buf); err != nil {
			return fmt.Errorf("unable to encode account.businessChatLinks#ec43a2d1: field users element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (b *AccountBusinessChatLinks) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode account.businessChatLinks#ec43a2d1 to nil")
	}
	if err := buf.ConsumeID(AccountBusinessChatLinksTypeID); err != nil {
		return fmt.Errorf("unable to decode account.businessChatLinks#ec43a2d1: %w", err)
	}
	return b.DecodeBare(buf)
}

// DecodeBare implements bin.BareDecoder.
func (b *AccountBusinessChatLinks) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode account.businessChatLinks#ec43a2d1 to nil")
	}
	{
		headerLen, err := buf.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode account.businessChatLinks#ec43a2d1: field links: %w", err)
		}

		if headerLen > 0 {
			b.Links = make([]BusinessChatLink, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value BusinessChatLink
			if err := value.Decode(buf); err != nil {
				return fmt.Errorf("unable to decode account.businessChatLinks#ec43a2d1: field links: %w", err)
			}
			b.Links = append(b.Links, value)
		}
	}
	{
		headerLen, err := buf.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode account.businessChatLinks#ec43a2d1: field chats: %w", err)
		}

		if headerLen > 0 {
			b.Chats = make([]ChatClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeChat(buf)
			if err != nil {
				return fmt.Errorf("unable to decode account.businessChatLinks#ec43a2d1: field chats: %w", err)
			}
			b.Chats = append(b.Chats, value)
		}
	}
	{
		headerLen, err := buf.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode account.businessChatLinks#ec43a2d1: field users: %w", err)
		}

		if headerLen > 0 {
			b.Users = make([]UserClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeUser(buf)
			if err != nil {
				return fmt.Errorf("unable to decode account.businessChatLinks#ec43a2d1: field users: %w", err)
			}
			b.Users = append(b.Users, value)
		}
	}
	return nil
}

// GetLinks returns value of Links field.
func (b *AccountBusinessChatLinks) GetLinks() (value []BusinessChatLink) {
	if b == nil {
		return
	}
	return b.Links
}

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

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

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

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