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

// EmailVerifyPurposeLoginSetup represents TL type `emailVerifyPurposeLoginSetup#4345be73`.
// Email verification purpose: setup login email
//
// See https://core.telegram.org/constructor/emailVerifyPurposeLoginSetup for reference.
type EmailVerifyPurposeLoginSetup struct {
	// Phone number
	PhoneNumber string
	// Phone code hash as specified by the documentation¹
	//
	// Links:
	//  1) https://core.telegram.org/api/auth#email-verification
	PhoneCodeHash string
}

// EmailVerifyPurposeLoginSetupTypeID is TL type id of EmailVerifyPurposeLoginSetup.
const EmailVerifyPurposeLoginSetupTypeID = 0x4345be73

// construct implements constructor of EmailVerifyPurposeClass.
func (e EmailVerifyPurposeLoginSetup) construct() EmailVerifyPurposeClass { return &e }

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

	_ EmailVerifyPurposeClass = &EmailVerifyPurposeLoginSetup{}
)

func (e *EmailVerifyPurposeLoginSetup) Zero() bool {
	if e == nil {
		return true
	}
	if !(e.PhoneNumber == "") {
		return false
	}
	if !(e.PhoneCodeHash == "") {
		return false
	}

	return true
}

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

// FillFrom fills EmailVerifyPurposeLoginSetup from given interface.
func (e *EmailVerifyPurposeLoginSetup) FillFrom(from interface {
	GetPhoneNumber() (value string)
	GetPhoneCodeHash() (value string)
}) {
	e.PhoneNumber = from.GetPhoneNumber()
	e.PhoneCodeHash = from.GetPhoneCodeHash()
}

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

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

// TypeInfo returns info about TL type.
func (e *EmailVerifyPurposeLoginSetup) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "emailVerifyPurposeLoginSetup",
		ID:   EmailVerifyPurposeLoginSetupTypeID,
	}
	if e == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "PhoneNumber",
			SchemaName: "phone_number",
		},
		{
			Name:       "PhoneCodeHash",
			SchemaName: "phone_code_hash",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (e *EmailVerifyPurposeLoginSetup) Encode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode emailVerifyPurposeLoginSetup#4345be73 as nil")
	}
	b.PutID(EmailVerifyPurposeLoginSetupTypeID)
	return e.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (e *EmailVerifyPurposeLoginSetup) EncodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode emailVerifyPurposeLoginSetup#4345be73 as nil")
	}
	b.PutString(e.PhoneNumber)
	b.PutString(e.PhoneCodeHash)
	return nil
}

// Decode implements bin.Decoder.
func (e *EmailVerifyPurposeLoginSetup) Decode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode emailVerifyPurposeLoginSetup#4345be73 to nil")
	}
	if err := b.ConsumeID(EmailVerifyPurposeLoginSetupTypeID); err != nil {
		return fmt.Errorf("unable to decode emailVerifyPurposeLoginSetup#4345be73: %w", err)
	}
	return e.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (e *EmailVerifyPurposeLoginSetup) DecodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode emailVerifyPurposeLoginSetup#4345be73 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode emailVerifyPurposeLoginSetup#4345be73: field phone_number: %w", err)
		}
		e.PhoneNumber = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode emailVerifyPurposeLoginSetup#4345be73: field phone_code_hash: %w", err)
		}
		e.PhoneCodeHash = value
	}
	return nil
}

// GetPhoneNumber returns value of PhoneNumber field.
func (e *EmailVerifyPurposeLoginSetup) GetPhoneNumber() (value string) {
	if e == nil {
		return
	}
	return e.PhoneNumber
}

// GetPhoneCodeHash returns value of PhoneCodeHash field.
func (e *EmailVerifyPurposeLoginSetup) GetPhoneCodeHash() (value string) {
	if e == nil {
		return
	}
	return e.PhoneCodeHash
}

// EmailVerifyPurposeLoginChange represents TL type `emailVerifyPurposeLoginChange#527d22eb`.
// Email verification purpose: change login email
//
// See https://core.telegram.org/constructor/emailVerifyPurposeLoginChange for reference.
type EmailVerifyPurposeLoginChange struct {
}

// EmailVerifyPurposeLoginChangeTypeID is TL type id of EmailVerifyPurposeLoginChange.
const EmailVerifyPurposeLoginChangeTypeID = 0x527d22eb

// construct implements constructor of EmailVerifyPurposeClass.
func (e EmailVerifyPurposeLoginChange) construct() EmailVerifyPurposeClass { return &e }

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

	_ EmailVerifyPurposeClass = &EmailVerifyPurposeLoginChange{}
)

func (e *EmailVerifyPurposeLoginChange) Zero() bool {
	if e == nil {
		return true
	}

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (e *EmailVerifyPurposeLoginChange) Encode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode emailVerifyPurposeLoginChange#527d22eb as nil")
	}
	b.PutID(EmailVerifyPurposeLoginChangeTypeID)
	return e.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (e *EmailVerifyPurposeLoginChange) EncodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode emailVerifyPurposeLoginChange#527d22eb as nil")
	}
	return nil
}

// Decode implements bin.Decoder.
func (e *EmailVerifyPurposeLoginChange) Decode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode emailVerifyPurposeLoginChange#527d22eb to nil")
	}
	if err := b.ConsumeID(EmailVerifyPurposeLoginChangeTypeID); err != nil {
		return fmt.Errorf("unable to decode emailVerifyPurposeLoginChange#527d22eb: %w", err)
	}
	return e.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (e *EmailVerifyPurposeLoginChange) DecodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode emailVerifyPurposeLoginChange#527d22eb to nil")
	}
	return nil
}

// EmailVerifyPurposePassport represents TL type `emailVerifyPurposePassport#bbf51685`.
// Verify an email for use in telegram passport¹
//
// Links:
//  1. https://core.telegram.org/api/passport
//
// See https://core.telegram.org/constructor/emailVerifyPurposePassport for reference.
type EmailVerifyPurposePassport struct {
}

// EmailVerifyPurposePassportTypeID is TL type id of EmailVerifyPurposePassport.
const EmailVerifyPurposePassportTypeID = 0xbbf51685

// construct implements constructor of EmailVerifyPurposeClass.
func (e EmailVerifyPurposePassport) construct() EmailVerifyPurposeClass { return &e }

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

	_ EmailVerifyPurposeClass = &EmailVerifyPurposePassport{}
)

func (e *EmailVerifyPurposePassport) Zero() bool {
	if e == nil {
		return true
	}

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (e *EmailVerifyPurposePassport) Encode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode emailVerifyPurposePassport#bbf51685 as nil")
	}
	b.PutID(EmailVerifyPurposePassportTypeID)
	return e.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (e *EmailVerifyPurposePassport) EncodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode emailVerifyPurposePassport#bbf51685 as nil")
	}
	return nil
}

// Decode implements bin.Decoder.
func (e *EmailVerifyPurposePassport) Decode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode emailVerifyPurposePassport#bbf51685 to nil")
	}
	if err := b.ConsumeID(EmailVerifyPurposePassportTypeID); err != nil {
		return fmt.Errorf("unable to decode emailVerifyPurposePassport#bbf51685: %w", err)
	}
	return e.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (e *EmailVerifyPurposePassport) DecodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode emailVerifyPurposePassport#bbf51685 to nil")
	}
	return nil
}

// EmailVerifyPurposeClassName is schema name of EmailVerifyPurposeClass.
const EmailVerifyPurposeClassName = "EmailVerifyPurpose"

// EmailVerifyPurposeClass represents EmailVerifyPurpose generic type.
//
// See https://core.telegram.org/type/EmailVerifyPurpose for reference.
//
// Example:
//
//	g, err := tg.DecodeEmailVerifyPurpose(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tg.EmailVerifyPurposeLoginSetup: // emailVerifyPurposeLoginSetup#4345be73
//	case *tg.EmailVerifyPurposeLoginChange: // emailVerifyPurposeLoginChange#527d22eb
//	case *tg.EmailVerifyPurposePassport: // emailVerifyPurposePassport#bbf51685
//	default: panic(v)
//	}
type EmailVerifyPurposeClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() EmailVerifyPurposeClass

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

// DecodeEmailVerifyPurpose implements binary de-serialization for EmailVerifyPurposeClass.
func DecodeEmailVerifyPurpose(buf *bin.Buffer) (EmailVerifyPurposeClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case EmailVerifyPurposeLoginSetupTypeID:
		// Decoding emailVerifyPurposeLoginSetup#4345be73.
		v := EmailVerifyPurposeLoginSetup{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode EmailVerifyPurposeClass: %w", err)
		}
		return &v, nil
	case EmailVerifyPurposeLoginChangeTypeID:
		// Decoding emailVerifyPurposeLoginChange#527d22eb.
		v := EmailVerifyPurposeLoginChange{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode EmailVerifyPurposeClass: %w", err)
		}
		return &v, nil
	case EmailVerifyPurposePassportTypeID:
		// Decoding emailVerifyPurposePassport#bbf51685.
		v := EmailVerifyPurposePassport{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode EmailVerifyPurposeClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode EmailVerifyPurposeClass: %w", bin.NewUnexpectedID(id))
	}
}

// EmailVerifyPurpose boxes the EmailVerifyPurposeClass providing a helper.
type EmailVerifyPurposeBox struct {
	EmailVerifyPurpose EmailVerifyPurposeClass
}

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

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