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

// UsersUsers represents TL type `users.users#62d706b8`.
// Describes a list of users (or bots).
//
// See https://core.telegram.org/constructor/users.users for reference.
type UsersUsers struct {
	// Users
	Users []UserClass
}

// UsersUsersTypeID is TL type id of UsersUsers.
const UsersUsersTypeID = 0x62d706b8

// construct implements constructor of UsersUsersClass.
func (u UsersUsers) construct() UsersUsersClass { return &u }

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

	_ UsersUsersClass = &UsersUsers{}
)

func (u *UsersUsers) Zero() bool {
	if u == nil {
		return true
	}
	if !(u.Users == nil) {
		return false
	}

	return true
}

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

// FillFrom fills UsersUsers from given interface.
func (u *UsersUsers) FillFrom(from interface {
	GetUsers() (value []UserClass)
}) {
	u.Users = from.GetUsers()
}

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

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

// TypeInfo returns info about TL type.
func (u *UsersUsers) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "users.users",
		ID:   UsersUsersTypeID,
	}
	if u == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Users",
			SchemaName: "users",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (u *UsersUsers) Encode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode users.users#62d706b8 as nil")
	}
	b.PutID(UsersUsersTypeID)
	return u.EncodeBare(b)
}

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

// Decode implements bin.Decoder.
func (u *UsersUsers) Decode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode users.users#62d706b8 to nil")
	}
	if err := b.ConsumeID(UsersUsersTypeID); err != nil {
		return fmt.Errorf("unable to decode users.users#62d706b8: %w", err)
	}
	return u.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (u *UsersUsers) DecodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode users.users#62d706b8 to nil")
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode users.users#62d706b8: field users: %w", err)
		}

		if headerLen > 0 {
			u.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 users.users#62d706b8: field users: %w", err)
			}
			u.Users = append(u.Users, value)
		}
	}
	return nil
}

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

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

// UsersUsersSlice represents TL type `users.usersSlice#315a4974`.
// Describes a partial list of users.
//
// See https://core.telegram.org/constructor/users.usersSlice for reference.
type UsersUsersSlice struct {
	// Total number of users (bigger than the users specified in users)
	Count int
	// Subset of users.
	Users []UserClass
}

// UsersUsersSliceTypeID is TL type id of UsersUsersSlice.
const UsersUsersSliceTypeID = 0x315a4974

// construct implements constructor of UsersUsersClass.
func (u UsersUsersSlice) construct() UsersUsersClass { return &u }

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

	_ UsersUsersClass = &UsersUsersSlice{}
)

func (u *UsersUsersSlice) Zero() bool {
	if u == nil {
		return true
	}
	if !(u.Count == 0) {
		return false
	}
	if !(u.Users == nil) {
		return false
	}

	return true
}

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

// FillFrom fills UsersUsersSlice from given interface.
func (u *UsersUsersSlice) FillFrom(from interface {
	GetCount() (value int)
	GetUsers() (value []UserClass)
}) {
	u.Count = from.GetCount()
	u.Users = from.GetUsers()
}

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

// TypeName returns name of type in TL schema.
func (*UsersUsersSlice) TypeName() string {
	return "users.usersSlice"
}

// TypeInfo returns info about TL type.
func (u *UsersUsersSlice) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "users.usersSlice",
		ID:   UsersUsersSliceTypeID,
	}
	if u == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Count",
			SchemaName: "count",
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (u *UsersUsersSlice) Encode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode users.usersSlice#315a4974 as nil")
	}
	b.PutID(UsersUsersSliceTypeID)
	return u.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (u *UsersUsersSlice) EncodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode users.usersSlice#315a4974 as nil")
	}
	b.PutInt(u.Count)
	b.PutVectorHeader(len(u.Users))
	for idx, v := range u.Users {
		if v == nil {
			return fmt.Errorf("unable to encode users.usersSlice#315a4974: field users element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode users.usersSlice#315a4974: field users element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (u *UsersUsersSlice) Decode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode users.usersSlice#315a4974 to nil")
	}
	if err := b.ConsumeID(UsersUsersSliceTypeID); err != nil {
		return fmt.Errorf("unable to decode users.usersSlice#315a4974: %w", err)
	}
	return u.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (u *UsersUsersSlice) DecodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode users.usersSlice#315a4974 to nil")
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode users.usersSlice#315a4974: field count: %w", err)
		}
		u.Count = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode users.usersSlice#315a4974: field users: %w", err)
		}

		if headerLen > 0 {
			u.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 users.usersSlice#315a4974: field users: %w", err)
			}
			u.Users = append(u.Users, value)
		}
	}
	return nil
}

// GetCount returns value of Count field.
func (u *UsersUsersSlice) GetCount() (value int) {
	if u == nil {
		return
	}
	return u.Count
}

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

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

// UsersUsersClassName is schema name of UsersUsersClass.
const UsersUsersClassName = "users.Users"

// UsersUsersClass represents users.Users generic type.
//
// See https://core.telegram.org/type/users.Users for reference.
//
// Example:
//
//	g, err := tg.DecodeUsersUsers(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tg.UsersUsers: // users.users#62d706b8
//	case *tg.UsersUsersSlice: // users.usersSlice#315a4974
//	default: panic(v)
//	}
type UsersUsersClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() UsersUsersClass

	// 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

	// Users
	GetUsers() (value []UserClass)
	// Users
	MapUsers() (value UserClassArray)
}

// DecodeUsersUsers implements binary de-serialization for UsersUsersClass.
func DecodeUsersUsers(buf *bin.Buffer) (UsersUsersClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case UsersUsersTypeID:
		// Decoding users.users#62d706b8.
		v := UsersUsers{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode UsersUsersClass: %w", err)
		}
		return &v, nil
	case UsersUsersSliceTypeID:
		// Decoding users.usersSlice#315a4974.
		v := UsersUsersSlice{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode UsersUsersClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode UsersUsersClass: %w", bin.NewUnexpectedID(id))
	}
}

// UsersUsers boxes the UsersUsersClass providing a helper.
type UsersUsersBox struct {
	Users UsersUsersClass
}

// Decode implements bin.Decoder for UsersUsersBox.
func (b *UsersUsersBox) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("unable to decode UsersUsersBox to nil")
	}
	v, err := DecodeUsersUsers(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.Users = v
	return nil
}

// Encode implements bin.Encode for UsersUsersBox.
func (b *UsersUsersBox) Encode(buf *bin.Buffer) error {
	if b == nil || b.Users == nil {
		return fmt.Errorf("unable to encode UsersUsersClass as nil")
	}
	return b.Users.Encode(buf)
}
