// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"github.com/gotd/td/bin"
	"github.com/gotd/td/tdjson"
	"github.com/gotd/td/tdp"
	"github.com/gotd/td/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{}
)

// SecurePasswordKdfAlgoUnknown represents TL type `securePasswordKdfAlgoUnknown#4a8537`.
// Unknown KDF algo (most likely the client has to be updated)
//
// See https://core.telegram.org/constructor/securePasswordKdfAlgoUnknown for reference.
type SecurePasswordKdfAlgoUnknown struct {
}

// SecurePasswordKdfAlgoUnknownTypeID is TL type id of SecurePasswordKdfAlgoUnknown.
const SecurePasswordKdfAlgoUnknownTypeID = 0x4a8537

// construct implements constructor of SecurePasswordKdfAlgoClass.
func (s SecurePasswordKdfAlgoUnknown) construct() SecurePasswordKdfAlgoClass { return &s }

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

	_ SecurePasswordKdfAlgoClass = &SecurePasswordKdfAlgoUnknown{}
)

func (s *SecurePasswordKdfAlgoUnknown) Zero() bool {
	if s == nil {
		return true
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SecurePasswordKdfAlgoUnknown) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "securePasswordKdfAlgoUnknown",
		ID:   SecurePasswordKdfAlgoUnknownTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{}
	return typ
}

// Encode implements bin.Encoder.
func (s *SecurePasswordKdfAlgoUnknown) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode securePasswordKdfAlgoUnknown#4a8537 as nil")
	}
	b.PutID(SecurePasswordKdfAlgoUnknownTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SecurePasswordKdfAlgoUnknown) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode securePasswordKdfAlgoUnknown#4a8537 as nil")
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *SecurePasswordKdfAlgoUnknown) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode securePasswordKdfAlgoUnknown#4a8537 to nil")
	}
	if err := b.ConsumeID(SecurePasswordKdfAlgoUnknownTypeID); err != nil {
		return fmt.Errorf("unable to decode securePasswordKdfAlgoUnknown#4a8537: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SecurePasswordKdfAlgoUnknown) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode securePasswordKdfAlgoUnknown#4a8537 to nil")
	}
	return nil
}

// SecurePasswordKdfAlgoPBKDF2HMACSHA512iter100000 represents TL type `securePasswordKdfAlgoPBKDF2HMACSHA512iter100000#bbf2dda0`.
// PBKDF2 with SHA512 and 100000 iterations KDF algo
//
// See https://core.telegram.org/constructor/securePasswordKdfAlgoPBKDF2HMACSHA512iter100000 for reference.
type SecurePasswordKdfAlgoPBKDF2HMACSHA512iter100000 struct {
	// Salt
	Salt []byte
}

// SecurePasswordKdfAlgoPBKDF2HMACSHA512iter100000TypeID is TL type id of SecurePasswordKdfAlgoPBKDF2HMACSHA512iter100000.
const SecurePasswordKdfAlgoPBKDF2HMACSHA512iter100000TypeID = 0xbbf2dda0

// construct implements constructor of SecurePasswordKdfAlgoClass.
func (s SecurePasswordKdfAlgoPBKDF2HMACSHA512iter100000) construct() SecurePasswordKdfAlgoClass {
	return &s
}

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

	_ SecurePasswordKdfAlgoClass = &SecurePasswordKdfAlgoPBKDF2HMACSHA512iter100000{}
)

func (s *SecurePasswordKdfAlgoPBKDF2HMACSHA512iter100000) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Salt == nil) {
		return false
	}

	return true
}

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

// FillFrom fills SecurePasswordKdfAlgoPBKDF2HMACSHA512iter100000 from given interface.
func (s *SecurePasswordKdfAlgoPBKDF2HMACSHA512iter100000) FillFrom(from interface {
	GetSalt() (value []byte)
}) {
	s.Salt = from.GetSalt()
}

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

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

// TypeInfo returns info about TL type.
func (s *SecurePasswordKdfAlgoPBKDF2HMACSHA512iter100000) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "securePasswordKdfAlgoPBKDF2HMACSHA512iter100000",
		ID:   SecurePasswordKdfAlgoPBKDF2HMACSHA512iter100000TypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Salt",
			SchemaName: "salt",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SecurePasswordKdfAlgoPBKDF2HMACSHA512iter100000) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode securePasswordKdfAlgoPBKDF2HMACSHA512iter100000#bbf2dda0 as nil")
	}
	b.PutID(SecurePasswordKdfAlgoPBKDF2HMACSHA512iter100000TypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SecurePasswordKdfAlgoPBKDF2HMACSHA512iter100000) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode securePasswordKdfAlgoPBKDF2HMACSHA512iter100000#bbf2dda0 as nil")
	}
	b.PutBytes(s.Salt)
	return nil
}

// Decode implements bin.Decoder.
func (s *SecurePasswordKdfAlgoPBKDF2HMACSHA512iter100000) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode securePasswordKdfAlgoPBKDF2HMACSHA512iter100000#bbf2dda0 to nil")
	}
	if err := b.ConsumeID(SecurePasswordKdfAlgoPBKDF2HMACSHA512iter100000TypeID); err != nil {
		return fmt.Errorf("unable to decode securePasswordKdfAlgoPBKDF2HMACSHA512iter100000#bbf2dda0: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SecurePasswordKdfAlgoPBKDF2HMACSHA512iter100000) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode securePasswordKdfAlgoPBKDF2HMACSHA512iter100000#bbf2dda0 to nil")
	}
	{
		value, err := b.Bytes()
		if err != nil {
			return fmt.Errorf("unable to decode securePasswordKdfAlgoPBKDF2HMACSHA512iter100000#bbf2dda0: field salt: %w", err)
		}
		s.Salt = value
	}
	return nil
}

// GetSalt returns value of Salt field.
func (s *SecurePasswordKdfAlgoPBKDF2HMACSHA512iter100000) GetSalt() (value []byte) {
	if s == nil {
		return
	}
	return s.Salt
}

// SecurePasswordKdfAlgoSHA512 represents TL type `securePasswordKdfAlgoSHA512#86471d92`.
// SHA512 KDF algo
//
// See https://core.telegram.org/constructor/securePasswordKdfAlgoSHA512 for reference.
type SecurePasswordKdfAlgoSHA512 struct {
	// Salt
	Salt []byte
}

// SecurePasswordKdfAlgoSHA512TypeID is TL type id of SecurePasswordKdfAlgoSHA512.
const SecurePasswordKdfAlgoSHA512TypeID = 0x86471d92

// construct implements constructor of SecurePasswordKdfAlgoClass.
func (s SecurePasswordKdfAlgoSHA512) construct() SecurePasswordKdfAlgoClass { return &s }

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

	_ SecurePasswordKdfAlgoClass = &SecurePasswordKdfAlgoSHA512{}
)

func (s *SecurePasswordKdfAlgoSHA512) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Salt == nil) {
		return false
	}

	return true
}

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

// FillFrom fills SecurePasswordKdfAlgoSHA512 from given interface.
func (s *SecurePasswordKdfAlgoSHA512) FillFrom(from interface {
	GetSalt() (value []byte)
}) {
	s.Salt = from.GetSalt()
}

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

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

// TypeInfo returns info about TL type.
func (s *SecurePasswordKdfAlgoSHA512) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "securePasswordKdfAlgoSHA512",
		ID:   SecurePasswordKdfAlgoSHA512TypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Salt",
			SchemaName: "salt",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SecurePasswordKdfAlgoSHA512) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode securePasswordKdfAlgoSHA512#86471d92 as nil")
	}
	b.PutID(SecurePasswordKdfAlgoSHA512TypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SecurePasswordKdfAlgoSHA512) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode securePasswordKdfAlgoSHA512#86471d92 as nil")
	}
	b.PutBytes(s.Salt)
	return nil
}

// Decode implements bin.Decoder.
func (s *SecurePasswordKdfAlgoSHA512) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode securePasswordKdfAlgoSHA512#86471d92 to nil")
	}
	if err := b.ConsumeID(SecurePasswordKdfAlgoSHA512TypeID); err != nil {
		return fmt.Errorf("unable to decode securePasswordKdfAlgoSHA512#86471d92: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SecurePasswordKdfAlgoSHA512) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode securePasswordKdfAlgoSHA512#86471d92 to nil")
	}
	{
		value, err := b.Bytes()
		if err != nil {
			return fmt.Errorf("unable to decode securePasswordKdfAlgoSHA512#86471d92: field salt: %w", err)
		}
		s.Salt = value
	}
	return nil
}

// GetSalt returns value of Salt field.
func (s *SecurePasswordKdfAlgoSHA512) GetSalt() (value []byte) {
	if s == nil {
		return
	}
	return s.Salt
}

// SecurePasswordKdfAlgoClassName is schema name of SecurePasswordKdfAlgoClass.
const SecurePasswordKdfAlgoClassName = "SecurePasswordKdfAlgo"

// SecurePasswordKdfAlgoClass represents SecurePasswordKdfAlgo generic type.
//
// See https://core.telegram.org/type/SecurePasswordKdfAlgo for reference.
//
// Example:
//
//	g, err := tg.DecodeSecurePasswordKdfAlgo(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tg.SecurePasswordKdfAlgoUnknown: // securePasswordKdfAlgoUnknown#4a8537
//	case *tg.SecurePasswordKdfAlgoPBKDF2HMACSHA512iter100000: // securePasswordKdfAlgoPBKDF2HMACSHA512iter100000#bbf2dda0
//	case *tg.SecurePasswordKdfAlgoSHA512: // securePasswordKdfAlgoSHA512#86471d92
//	default: panic(v)
//	}
type SecurePasswordKdfAlgoClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() SecurePasswordKdfAlgoClass

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

// DecodeSecurePasswordKdfAlgo implements binary de-serialization for SecurePasswordKdfAlgoClass.
func DecodeSecurePasswordKdfAlgo(buf *bin.Buffer) (SecurePasswordKdfAlgoClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case SecurePasswordKdfAlgoUnknownTypeID:
		// Decoding securePasswordKdfAlgoUnknown#4a8537.
		v := SecurePasswordKdfAlgoUnknown{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode SecurePasswordKdfAlgoClass: %w", err)
		}
		return &v, nil
	case SecurePasswordKdfAlgoPBKDF2HMACSHA512iter100000TypeID:
		// Decoding securePasswordKdfAlgoPBKDF2HMACSHA512iter100000#bbf2dda0.
		v := SecurePasswordKdfAlgoPBKDF2HMACSHA512iter100000{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode SecurePasswordKdfAlgoClass: %w", err)
		}
		return &v, nil
	case SecurePasswordKdfAlgoSHA512TypeID:
		// Decoding securePasswordKdfAlgoSHA512#86471d92.
		v := SecurePasswordKdfAlgoSHA512{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode SecurePasswordKdfAlgoClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode SecurePasswordKdfAlgoClass: %w", bin.NewUnexpectedID(id))
	}
}

// SecurePasswordKdfAlgo boxes the SecurePasswordKdfAlgoClass providing a helper.
type SecurePasswordKdfAlgoBox struct {
	SecurePasswordKdfAlgo SecurePasswordKdfAlgoClass
}

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

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