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

// PhoneNumberCodeTypeChange represents TL type `phoneNumberCodeTypeChange#531ba1a`.
type PhoneNumberCodeTypeChange struct {
}

// PhoneNumberCodeTypeChangeTypeID is TL type id of PhoneNumberCodeTypeChange.
const PhoneNumberCodeTypeChangeTypeID = 0x531ba1a

// construct implements constructor of PhoneNumberCodeTypeClass.
func (p PhoneNumberCodeTypeChange) construct() PhoneNumberCodeTypeClass { return &p }

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

	_ PhoneNumberCodeTypeClass = &PhoneNumberCodeTypeChange{}
)

func (p *PhoneNumberCodeTypeChange) Zero() bool {
	if p == nil {
		return true
	}

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (p *PhoneNumberCodeTypeChange) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode phoneNumberCodeTypeChange#531ba1a as nil")
	}
	b.PutID(PhoneNumberCodeTypeChangeTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PhoneNumberCodeTypeChange) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode phoneNumberCodeTypeChange#531ba1a as nil")
	}
	return nil
}

// Decode implements bin.Decoder.
func (p *PhoneNumberCodeTypeChange) Decode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode phoneNumberCodeTypeChange#531ba1a to nil")
	}
	if err := b.ConsumeID(PhoneNumberCodeTypeChangeTypeID); err != nil {
		return fmt.Errorf("unable to decode phoneNumberCodeTypeChange#531ba1a: %w", err)
	}
	return p.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (p *PhoneNumberCodeTypeChange) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode phoneNumberCodeTypeChange#531ba1a to nil")
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (p *PhoneNumberCodeTypeChange) EncodeTDLibJSON(b tdjson.Encoder) error {
	if p == nil {
		return fmt.Errorf("can't encode phoneNumberCodeTypeChange#531ba1a as nil")
	}
	b.ObjStart()
	b.PutID("phoneNumberCodeTypeChange")
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (p *PhoneNumberCodeTypeChange) DecodeTDLibJSON(b tdjson.Decoder) error {
	if p == nil {
		return fmt.Errorf("can't decode phoneNumberCodeTypeChange#531ba1a to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("phoneNumberCodeTypeChange"); err != nil {
				return fmt.Errorf("unable to decode phoneNumberCodeTypeChange#531ba1a: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// PhoneNumberCodeTypeVerify represents TL type `phoneNumberCodeTypeVerify#c2a48fdb`.
type PhoneNumberCodeTypeVerify struct {
}

// PhoneNumberCodeTypeVerifyTypeID is TL type id of PhoneNumberCodeTypeVerify.
const PhoneNumberCodeTypeVerifyTypeID = 0xc2a48fdb

// construct implements constructor of PhoneNumberCodeTypeClass.
func (p PhoneNumberCodeTypeVerify) construct() PhoneNumberCodeTypeClass { return &p }

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

	_ PhoneNumberCodeTypeClass = &PhoneNumberCodeTypeVerify{}
)

func (p *PhoneNumberCodeTypeVerify) Zero() bool {
	if p == nil {
		return true
	}

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (p *PhoneNumberCodeTypeVerify) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode phoneNumberCodeTypeVerify#c2a48fdb as nil")
	}
	b.PutID(PhoneNumberCodeTypeVerifyTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PhoneNumberCodeTypeVerify) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode phoneNumberCodeTypeVerify#c2a48fdb as nil")
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (p *PhoneNumberCodeTypeVerify) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode phoneNumberCodeTypeVerify#c2a48fdb to nil")
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (p *PhoneNumberCodeTypeVerify) EncodeTDLibJSON(b tdjson.Encoder) error {
	if p == nil {
		return fmt.Errorf("can't encode phoneNumberCodeTypeVerify#c2a48fdb as nil")
	}
	b.ObjStart()
	b.PutID("phoneNumberCodeTypeVerify")
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (p *PhoneNumberCodeTypeVerify) DecodeTDLibJSON(b tdjson.Decoder) error {
	if p == nil {
		return fmt.Errorf("can't decode phoneNumberCodeTypeVerify#c2a48fdb to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("phoneNumberCodeTypeVerify"); err != nil {
				return fmt.Errorf("unable to decode phoneNumberCodeTypeVerify#c2a48fdb: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// PhoneNumberCodeTypeConfirmOwnership represents TL type `phoneNumberCodeTypeConfirmOwnership#e3114fe8`.
type PhoneNumberCodeTypeConfirmOwnership struct {
	// Hash value from the link
	Hash string
}

// PhoneNumberCodeTypeConfirmOwnershipTypeID is TL type id of PhoneNumberCodeTypeConfirmOwnership.
const PhoneNumberCodeTypeConfirmOwnershipTypeID = 0xe3114fe8

// construct implements constructor of PhoneNumberCodeTypeClass.
func (p PhoneNumberCodeTypeConfirmOwnership) construct() PhoneNumberCodeTypeClass { return &p }

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

	_ PhoneNumberCodeTypeClass = &PhoneNumberCodeTypeConfirmOwnership{}
)

func (p *PhoneNumberCodeTypeConfirmOwnership) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.Hash == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (p *PhoneNumberCodeTypeConfirmOwnership) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "phoneNumberCodeTypeConfirmOwnership",
		ID:   PhoneNumberCodeTypeConfirmOwnershipTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Hash",
			SchemaName: "hash",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *PhoneNumberCodeTypeConfirmOwnership) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode phoneNumberCodeTypeConfirmOwnership#e3114fe8 as nil")
	}
	b.PutID(PhoneNumberCodeTypeConfirmOwnershipTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PhoneNumberCodeTypeConfirmOwnership) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode phoneNumberCodeTypeConfirmOwnership#e3114fe8 as nil")
	}
	b.PutString(p.Hash)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (p *PhoneNumberCodeTypeConfirmOwnership) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode phoneNumberCodeTypeConfirmOwnership#e3114fe8 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode phoneNumberCodeTypeConfirmOwnership#e3114fe8: field hash: %w", err)
		}
		p.Hash = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (p *PhoneNumberCodeTypeConfirmOwnership) EncodeTDLibJSON(b tdjson.Encoder) error {
	if p == nil {
		return fmt.Errorf("can't encode phoneNumberCodeTypeConfirmOwnership#e3114fe8 as nil")
	}
	b.ObjStart()
	b.PutID("phoneNumberCodeTypeConfirmOwnership")
	b.Comma()
	b.FieldStart("hash")
	b.PutString(p.Hash)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (p *PhoneNumberCodeTypeConfirmOwnership) DecodeTDLibJSON(b tdjson.Decoder) error {
	if p == nil {
		return fmt.Errorf("can't decode phoneNumberCodeTypeConfirmOwnership#e3114fe8 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("phoneNumberCodeTypeConfirmOwnership"); err != nil {
				return fmt.Errorf("unable to decode phoneNumberCodeTypeConfirmOwnership#e3114fe8: %w", err)
			}
		case "hash":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode phoneNumberCodeTypeConfirmOwnership#e3114fe8: field hash: %w", err)
			}
			p.Hash = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetHash returns value of Hash field.
func (p *PhoneNumberCodeTypeConfirmOwnership) GetHash() (value string) {
	if p == nil {
		return
	}
	return p.Hash
}

// PhoneNumberCodeTypeClassName is schema name of PhoneNumberCodeTypeClass.
const PhoneNumberCodeTypeClassName = "PhoneNumberCodeType"

// PhoneNumberCodeTypeClass represents PhoneNumberCodeType generic type.
//
// Example:
//
//	g, err := tdapi.DecodePhoneNumberCodeType(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tdapi.PhoneNumberCodeTypeChange: // phoneNumberCodeTypeChange#531ba1a
//	case *tdapi.PhoneNumberCodeTypeVerify: // phoneNumberCodeTypeVerify#c2a48fdb
//	case *tdapi.PhoneNumberCodeTypeConfirmOwnership: // phoneNumberCodeTypeConfirmOwnership#e3114fe8
//	default: panic(v)
//	}
type PhoneNumberCodeTypeClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() PhoneNumberCodeTypeClass

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

	EncodeTDLibJSON(b tdjson.Encoder) error
	DecodeTDLibJSON(b tdjson.Decoder) error
}

// DecodePhoneNumberCodeType implements binary de-serialization for PhoneNumberCodeTypeClass.
func DecodePhoneNumberCodeType(buf *bin.Buffer) (PhoneNumberCodeTypeClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case PhoneNumberCodeTypeChangeTypeID:
		// Decoding phoneNumberCodeTypeChange#531ba1a.
		v := PhoneNumberCodeTypeChange{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode PhoneNumberCodeTypeClass: %w", err)
		}
		return &v, nil
	case PhoneNumberCodeTypeVerifyTypeID:
		// Decoding phoneNumberCodeTypeVerify#c2a48fdb.
		v := PhoneNumberCodeTypeVerify{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode PhoneNumberCodeTypeClass: %w", err)
		}
		return &v, nil
	case PhoneNumberCodeTypeConfirmOwnershipTypeID:
		// Decoding phoneNumberCodeTypeConfirmOwnership#e3114fe8.
		v := PhoneNumberCodeTypeConfirmOwnership{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode PhoneNumberCodeTypeClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode PhoneNumberCodeTypeClass: %w", bin.NewUnexpectedID(id))
	}
}

// DecodeTDLibJSONPhoneNumberCodeType implements binary de-serialization for PhoneNumberCodeTypeClass.
func DecodeTDLibJSONPhoneNumberCodeType(buf tdjson.Decoder) (PhoneNumberCodeTypeClass, error) {
	id, err := buf.FindTypeID()
	if err != nil {
		return nil, err
	}
	switch id {
	case "phoneNumberCodeTypeChange":
		// Decoding phoneNumberCodeTypeChange#531ba1a.
		v := PhoneNumberCodeTypeChange{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode PhoneNumberCodeTypeClass: %w", err)
		}
		return &v, nil
	case "phoneNumberCodeTypeVerify":
		// Decoding phoneNumberCodeTypeVerify#c2a48fdb.
		v := PhoneNumberCodeTypeVerify{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode PhoneNumberCodeTypeClass: %w", err)
		}
		return &v, nil
	case "phoneNumberCodeTypeConfirmOwnership":
		// Decoding phoneNumberCodeTypeConfirmOwnership#e3114fe8.
		v := PhoneNumberCodeTypeConfirmOwnership{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode PhoneNumberCodeTypeClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode PhoneNumberCodeTypeClass: %w", tdjson.NewUnexpectedID(id))
	}
}

// PhoneNumberCodeType boxes the PhoneNumberCodeTypeClass providing a helper.
type PhoneNumberCodeTypeBox struct {
	PhoneNumberCodeType PhoneNumberCodeTypeClass
}

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

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

// DecodeTDLibJSON implements bin.Decoder for PhoneNumberCodeTypeBox.
func (b *PhoneNumberCodeTypeBox) DecodeTDLibJSON(buf tdjson.Decoder) error {
	if b == nil {
		return fmt.Errorf("unable to decode PhoneNumberCodeTypeBox to nil")
	}
	v, err := DecodeTDLibJSONPhoneNumberCodeType(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.PhoneNumberCodeType = v
	return nil
}

// EncodeTDLibJSON implements bin.Encode for PhoneNumberCodeTypeBox.
func (b *PhoneNumberCodeTypeBox) EncodeTDLibJSON(buf tdjson.Encoder) error {
	if b == nil || b.PhoneNumberCodeType == nil {
		return fmt.Errorf("unable to encode PhoneNumberCodeTypeClass as nil")
	}
	return b.PhoneNumberCodeType.EncodeTDLibJSON(buf)
}
