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

// PhoneJoinAsPeers represents TL type `phone.joinAsPeers#afe5623f`.
// A list of peers that can be used to join a group call, presenting yourself as a
// specific user/channel.
//
// See https://core.telegram.org/constructor/phone.joinAsPeers for reference.
type PhoneJoinAsPeers struct {
	// Peers
	Peers []PeerClass
	// Chats mentioned in the peers vector
	Chats []ChatClass
	// Users mentioned in the peers vector
	Users []UserClass
}

// PhoneJoinAsPeersTypeID is TL type id of PhoneJoinAsPeers.
const PhoneJoinAsPeersTypeID = 0xafe5623f

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

func (j *PhoneJoinAsPeers) Zero() bool {
	if j == nil {
		return true
	}
	if !(j.Peers == nil) {
		return false
	}
	if !(j.Chats == nil) {
		return false
	}
	if !(j.Users == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PhoneJoinAsPeers from given interface.
func (j *PhoneJoinAsPeers) FillFrom(from interface {
	GetPeers() (value []PeerClass)
	GetChats() (value []ChatClass)
	GetUsers() (value []UserClass)
}) {
	j.Peers = from.GetPeers()
	j.Chats = from.GetChats()
	j.Users = from.GetUsers()
}

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

// TypeName returns name of type in TL schema.
func (*PhoneJoinAsPeers) TypeName() string {
	return "phone.joinAsPeers"
}

// TypeInfo returns info about TL type.
func (j *PhoneJoinAsPeers) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "phone.joinAsPeers",
		ID:   PhoneJoinAsPeersTypeID,
	}
	if j == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peers",
			SchemaName: "peers",
		},
		{
			Name:       "Chats",
			SchemaName: "chats",
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (j *PhoneJoinAsPeers) Encode(b *bin.Buffer) error {
	if j == nil {
		return fmt.Errorf("can't encode phone.joinAsPeers#afe5623f as nil")
	}
	b.PutID(PhoneJoinAsPeersTypeID)
	return j.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (j *PhoneJoinAsPeers) EncodeBare(b *bin.Buffer) error {
	if j == nil {
		return fmt.Errorf("can't encode phone.joinAsPeers#afe5623f as nil")
	}
	b.PutVectorHeader(len(j.Peers))
	for idx, v := range j.Peers {
		if v == nil {
			return fmt.Errorf("unable to encode phone.joinAsPeers#afe5623f: field peers element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode phone.joinAsPeers#afe5623f: field peers element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(j.Chats))
	for idx, v := range j.Chats {
		if v == nil {
			return fmt.Errorf("unable to encode phone.joinAsPeers#afe5623f: field chats element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode phone.joinAsPeers#afe5623f: field chats element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(j.Users))
	for idx, v := range j.Users {
		if v == nil {
			return fmt.Errorf("unable to encode phone.joinAsPeers#afe5623f: field users element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode phone.joinAsPeers#afe5623f: field users element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (j *PhoneJoinAsPeers) Decode(b *bin.Buffer) error {
	if j == nil {
		return fmt.Errorf("can't decode phone.joinAsPeers#afe5623f to nil")
	}
	if err := b.ConsumeID(PhoneJoinAsPeersTypeID); err != nil {
		return fmt.Errorf("unable to decode phone.joinAsPeers#afe5623f: %w", err)
	}
	return j.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (j *PhoneJoinAsPeers) DecodeBare(b *bin.Buffer) error {
	if j == nil {
		return fmt.Errorf("can't decode phone.joinAsPeers#afe5623f to nil")
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode phone.joinAsPeers#afe5623f: field peers: %w", err)
		}

		if headerLen > 0 {
			j.Peers = make([]PeerClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodePeer(b)
			if err != nil {
				return fmt.Errorf("unable to decode phone.joinAsPeers#afe5623f: field peers: %w", err)
			}
			j.Peers = append(j.Peers, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode phone.joinAsPeers#afe5623f: field chats: %w", err)
		}

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

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

// GetPeers returns value of Peers field.
func (j *PhoneJoinAsPeers) GetPeers() (value []PeerClass) {
	if j == nil {
		return
	}
	return j.Peers
}

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

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

// MapPeers returns field Peers wrapped in PeerClassArray helper.
func (j *PhoneJoinAsPeers) MapPeers() (value PeerClassArray) {
	return PeerClassArray(j.Peers)
}

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

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