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

// AccountPasskeys represents TL type `account.passkeys#f8e0aa1c`.
//
// See https://core.telegram.org/constructor/account.passkeys for reference.
type AccountPasskeys struct {
	// Passkeys field of AccountPasskeys.
	Passkeys []Passkey
}

// AccountPasskeysTypeID is TL type id of AccountPasskeys.
const AccountPasskeysTypeID = 0xf8e0aa1c

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

func (p *AccountPasskeys) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.Passkeys == nil) {
		return false
	}

	return true
}

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

// FillFrom fills AccountPasskeys from given interface.
func (p *AccountPasskeys) FillFrom(from interface {
	GetPasskeys() (value []Passkey)
}) {
	p.Passkeys = from.GetPasskeys()
}

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

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

// TypeInfo returns info about TL type.
func (p *AccountPasskeys) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "account.passkeys",
		ID:   AccountPasskeysTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Passkeys",
			SchemaName: "passkeys",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *AccountPasskeys) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode account.passkeys#f8e0aa1c as nil")
	}
	b.PutID(AccountPasskeysTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *AccountPasskeys) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode account.passkeys#f8e0aa1c as nil")
	}
	b.PutVectorHeader(len(p.Passkeys))
	for idx, v := range p.Passkeys {
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode account.passkeys#f8e0aa1c: field passkeys element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (p *AccountPasskeys) Decode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode account.passkeys#f8e0aa1c to nil")
	}
	if err := b.ConsumeID(AccountPasskeysTypeID); err != nil {
		return fmt.Errorf("unable to decode account.passkeys#f8e0aa1c: %w", err)
	}
	return p.DecodeBare(b)
}

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

		if headerLen > 0 {
			p.Passkeys = make([]Passkey, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value Passkey
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode account.passkeys#f8e0aa1c: field passkeys: %w", err)
			}
			p.Passkeys = append(p.Passkeys, value)
		}
	}
	return nil
}

// GetPasskeys returns value of Passkeys field.
func (p *AccountPasskeys) GetPasskeys() (value []Passkey) {
	if p == nil {
		return
	}
	return p.Passkeys
}
