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

// AccountRegisterPasskeyRequest represents TL type `account.registerPasskey#55b41fd6`.
//
// See https://core.telegram.org/method/account.registerPasskey for reference.
type AccountRegisterPasskeyRequest struct {
	// Credential field of AccountRegisterPasskeyRequest.
	Credential InputPasskeyCredentialClass
}

// AccountRegisterPasskeyRequestTypeID is TL type id of AccountRegisterPasskeyRequest.
const AccountRegisterPasskeyRequestTypeID = 0x55b41fd6

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

func (r *AccountRegisterPasskeyRequest) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.Credential == nil) {
		return false
	}

	return true
}

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

// FillFrom fills AccountRegisterPasskeyRequest from given interface.
func (r *AccountRegisterPasskeyRequest) FillFrom(from interface {
	GetCredential() (value InputPasskeyCredentialClass)
}) {
	r.Credential = from.GetCredential()
}

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

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

// TypeInfo returns info about TL type.
func (r *AccountRegisterPasskeyRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "account.registerPasskey",
		ID:   AccountRegisterPasskeyRequestTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Credential",
			SchemaName: "credential",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (r *AccountRegisterPasskeyRequest) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode account.registerPasskey#55b41fd6 as nil")
	}
	b.PutID(AccountRegisterPasskeyRequestTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *AccountRegisterPasskeyRequest) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode account.registerPasskey#55b41fd6 as nil")
	}
	if r.Credential == nil {
		return fmt.Errorf("unable to encode account.registerPasskey#55b41fd6: field credential is nil")
	}
	if err := r.Credential.Encode(b); err != nil {
		return fmt.Errorf("unable to encode account.registerPasskey#55b41fd6: field credential: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (r *AccountRegisterPasskeyRequest) Decode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode account.registerPasskey#55b41fd6 to nil")
	}
	if err := b.ConsumeID(AccountRegisterPasskeyRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode account.registerPasskey#55b41fd6: %w", err)
	}
	return r.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (r *AccountRegisterPasskeyRequest) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode account.registerPasskey#55b41fd6 to nil")
	}
	{
		value, err := DecodeInputPasskeyCredential(b)
		if err != nil {
			return fmt.Errorf("unable to decode account.registerPasskey#55b41fd6: field credential: %w", err)
		}
		r.Credential = value
	}
	return nil
}

// GetCredential returns value of Credential field.
func (r *AccountRegisterPasskeyRequest) GetCredential() (value InputPasskeyCredentialClass) {
	if r == nil {
		return
	}
	return r.Credential
}

// AccountRegisterPasskey invokes method account.registerPasskey#55b41fd6 returning error if any.
//
// See https://core.telegram.org/method/account.registerPasskey for reference.
func (c *Client) AccountRegisterPasskey(ctx context.Context, credential InputPasskeyCredentialClass) (*Passkey, error) {
	var result Passkey

	request := &AccountRegisterPasskeyRequest{
		Credential: credential,
	}
	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return &result, nil
}
