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

// FoundUsers represents TL type `foundUsers#c531e854`.
type FoundUsers struct {
	// Identifiers of the found users
	UserIDs []int64
	// The offset for the next request. If empty, then there are no more results
	NextOffset string
}

// FoundUsersTypeID is TL type id of FoundUsers.
const FoundUsersTypeID = 0xc531e854

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

func (f *FoundUsers) Zero() bool {
	if f == nil {
		return true
	}
	if !(f.UserIDs == nil) {
		return false
	}
	if !(f.NextOffset == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (f *FoundUsers) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "foundUsers",
		ID:   FoundUsersTypeID,
	}
	if f == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "UserIDs",
			SchemaName: "user_ids",
		},
		{
			Name:       "NextOffset",
			SchemaName: "next_offset",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (f *FoundUsers) Encode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode foundUsers#c531e854 as nil")
	}
	b.PutID(FoundUsersTypeID)
	return f.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (f *FoundUsers) EncodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode foundUsers#c531e854 as nil")
	}
	b.PutInt(len(f.UserIDs))
	for _, v := range f.UserIDs {
		b.PutInt53(v)
	}
	b.PutString(f.NextOffset)
	return nil
}

// Decode implements bin.Decoder.
func (f *FoundUsers) Decode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode foundUsers#c531e854 to nil")
	}
	if err := b.ConsumeID(FoundUsersTypeID); err != nil {
		return fmt.Errorf("unable to decode foundUsers#c531e854: %w", err)
	}
	return f.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (f *FoundUsers) DecodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode foundUsers#c531e854 to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode foundUsers#c531e854: field user_ids: %w", err)
		}

		if headerLen > 0 {
			f.UserIDs = make([]int64, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode foundUsers#c531e854: field user_ids: %w", err)
			}
			f.UserIDs = append(f.UserIDs, value)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode foundUsers#c531e854: field next_offset: %w", err)
		}
		f.NextOffset = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (f *FoundUsers) EncodeTDLibJSON(b tdjson.Encoder) error {
	if f == nil {
		return fmt.Errorf("can't encode foundUsers#c531e854 as nil")
	}
	b.ObjStart()
	b.PutID("foundUsers")
	b.Comma()
	b.FieldStart("user_ids")
	b.ArrStart()
	for _, v := range f.UserIDs {
		b.PutInt53(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("next_offset")
	b.PutString(f.NextOffset)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (f *FoundUsers) DecodeTDLibJSON(b tdjson.Decoder) error {
	if f == nil {
		return fmt.Errorf("can't decode foundUsers#c531e854 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("foundUsers"); err != nil {
				return fmt.Errorf("unable to decode foundUsers#c531e854: %w", err)
			}
		case "user_ids":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int53()
				if err != nil {
					return fmt.Errorf("unable to decode foundUsers#c531e854: field user_ids: %w", err)
				}
				f.UserIDs = append(f.UserIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode foundUsers#c531e854: field user_ids: %w", err)
			}
		case "next_offset":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode foundUsers#c531e854: field next_offset: %w", err)
			}
			f.NextOffset = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetUserIDs returns value of UserIDs field.
func (f *FoundUsers) GetUserIDs() (value []int64) {
	if f == nil {
		return
	}
	return f.UserIDs
}

// GetNextOffset returns value of NextOffset field.
func (f *FoundUsers) GetNextOffset() (value string) {
	if f == nil {
		return
	}
	return f.NextOffset
}
