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

// AffiliateProgramSortOrderProfitability represents TL type `affiliateProgramSortOrderProfitability#8afaaf67`.
type AffiliateProgramSortOrderProfitability struct {
}

// AffiliateProgramSortOrderProfitabilityTypeID is TL type id of AffiliateProgramSortOrderProfitability.
const AffiliateProgramSortOrderProfitabilityTypeID = 0x8afaaf67

// construct implements constructor of AffiliateProgramSortOrderClass.
func (a AffiliateProgramSortOrderProfitability) construct() AffiliateProgramSortOrderClass { return &a }

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

	_ AffiliateProgramSortOrderClass = &AffiliateProgramSortOrderProfitability{}
)

func (a *AffiliateProgramSortOrderProfitability) Zero() bool {
	if a == nil {
		return true
	}

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (a *AffiliateProgramSortOrderProfitability) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode affiliateProgramSortOrderProfitability#8afaaf67 as nil")
	}
	b.PutID(AffiliateProgramSortOrderProfitabilityTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *AffiliateProgramSortOrderProfitability) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode affiliateProgramSortOrderProfitability#8afaaf67 as nil")
	}
	return nil
}

// Decode implements bin.Decoder.
func (a *AffiliateProgramSortOrderProfitability) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode affiliateProgramSortOrderProfitability#8afaaf67 to nil")
	}
	if err := b.ConsumeID(AffiliateProgramSortOrderProfitabilityTypeID); err != nil {
		return fmt.Errorf("unable to decode affiliateProgramSortOrderProfitability#8afaaf67: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *AffiliateProgramSortOrderProfitability) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode affiliateProgramSortOrderProfitability#8afaaf67 to nil")
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (a *AffiliateProgramSortOrderProfitability) EncodeTDLibJSON(b tdjson.Encoder) error {
	if a == nil {
		return fmt.Errorf("can't encode affiliateProgramSortOrderProfitability#8afaaf67 as nil")
	}
	b.ObjStart()
	b.PutID("affiliateProgramSortOrderProfitability")
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (a *AffiliateProgramSortOrderProfitability) DecodeTDLibJSON(b tdjson.Decoder) error {
	if a == nil {
		return fmt.Errorf("can't decode affiliateProgramSortOrderProfitability#8afaaf67 to nil")
	}

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

// AffiliateProgramSortOrderCreationDate represents TL type `affiliateProgramSortOrderCreationDate#a319390d`.
type AffiliateProgramSortOrderCreationDate struct {
}

// AffiliateProgramSortOrderCreationDateTypeID is TL type id of AffiliateProgramSortOrderCreationDate.
const AffiliateProgramSortOrderCreationDateTypeID = 0xa319390d

// construct implements constructor of AffiliateProgramSortOrderClass.
func (a AffiliateProgramSortOrderCreationDate) construct() AffiliateProgramSortOrderClass { return &a }

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

	_ AffiliateProgramSortOrderClass = &AffiliateProgramSortOrderCreationDate{}
)

func (a *AffiliateProgramSortOrderCreationDate) Zero() bool {
	if a == nil {
		return true
	}

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (a *AffiliateProgramSortOrderCreationDate) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode affiliateProgramSortOrderCreationDate#a319390d as nil")
	}
	b.PutID(AffiliateProgramSortOrderCreationDateTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *AffiliateProgramSortOrderCreationDate) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode affiliateProgramSortOrderCreationDate#a319390d as nil")
	}
	return nil
}

// Decode implements bin.Decoder.
func (a *AffiliateProgramSortOrderCreationDate) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode affiliateProgramSortOrderCreationDate#a319390d to nil")
	}
	if err := b.ConsumeID(AffiliateProgramSortOrderCreationDateTypeID); err != nil {
		return fmt.Errorf("unable to decode affiliateProgramSortOrderCreationDate#a319390d: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *AffiliateProgramSortOrderCreationDate) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode affiliateProgramSortOrderCreationDate#a319390d to nil")
	}
	return nil
}

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

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (a *AffiliateProgramSortOrderCreationDate) DecodeTDLibJSON(b tdjson.Decoder) error {
	if a == nil {
		return fmt.Errorf("can't decode affiliateProgramSortOrderCreationDate#a319390d to nil")
	}

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

// AffiliateProgramSortOrderRevenue represents TL type `affiliateProgramSortOrderRevenue#72a2c2b8`.
type AffiliateProgramSortOrderRevenue struct {
}

// AffiliateProgramSortOrderRevenueTypeID is TL type id of AffiliateProgramSortOrderRevenue.
const AffiliateProgramSortOrderRevenueTypeID = 0x72a2c2b8

// construct implements constructor of AffiliateProgramSortOrderClass.
func (a AffiliateProgramSortOrderRevenue) construct() AffiliateProgramSortOrderClass { return &a }

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

	_ AffiliateProgramSortOrderClass = &AffiliateProgramSortOrderRevenue{}
)

func (a *AffiliateProgramSortOrderRevenue) Zero() bool {
	if a == nil {
		return true
	}

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (a *AffiliateProgramSortOrderRevenue) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode affiliateProgramSortOrderRevenue#72a2c2b8 as nil")
	}
	b.PutID(AffiliateProgramSortOrderRevenueTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *AffiliateProgramSortOrderRevenue) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode affiliateProgramSortOrderRevenue#72a2c2b8 as nil")
	}
	return nil
}

// Decode implements bin.Decoder.
func (a *AffiliateProgramSortOrderRevenue) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode affiliateProgramSortOrderRevenue#72a2c2b8 to nil")
	}
	if err := b.ConsumeID(AffiliateProgramSortOrderRevenueTypeID); err != nil {
		return fmt.Errorf("unable to decode affiliateProgramSortOrderRevenue#72a2c2b8: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *AffiliateProgramSortOrderRevenue) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode affiliateProgramSortOrderRevenue#72a2c2b8 to nil")
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (a *AffiliateProgramSortOrderRevenue) EncodeTDLibJSON(b tdjson.Encoder) error {
	if a == nil {
		return fmt.Errorf("can't encode affiliateProgramSortOrderRevenue#72a2c2b8 as nil")
	}
	b.ObjStart()
	b.PutID("affiliateProgramSortOrderRevenue")
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (a *AffiliateProgramSortOrderRevenue) DecodeTDLibJSON(b tdjson.Decoder) error {
	if a == nil {
		return fmt.Errorf("can't decode affiliateProgramSortOrderRevenue#72a2c2b8 to nil")
	}

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

// AffiliateProgramSortOrderClassName is schema name of AffiliateProgramSortOrderClass.
const AffiliateProgramSortOrderClassName = "AffiliateProgramSortOrder"

// AffiliateProgramSortOrderClass represents AffiliateProgramSortOrder generic type.
//
// Example:
//
//	g, err := tdapi.DecodeAffiliateProgramSortOrder(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tdapi.AffiliateProgramSortOrderProfitability: // affiliateProgramSortOrderProfitability#8afaaf67
//	case *tdapi.AffiliateProgramSortOrderCreationDate: // affiliateProgramSortOrderCreationDate#a319390d
//	case *tdapi.AffiliateProgramSortOrderRevenue: // affiliateProgramSortOrderRevenue#72a2c2b8
//	default: panic(v)
//	}
type AffiliateProgramSortOrderClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() AffiliateProgramSortOrderClass

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

// DecodeAffiliateProgramSortOrder implements binary de-serialization for AffiliateProgramSortOrderClass.
func DecodeAffiliateProgramSortOrder(buf *bin.Buffer) (AffiliateProgramSortOrderClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case AffiliateProgramSortOrderProfitabilityTypeID:
		// Decoding affiliateProgramSortOrderProfitability#8afaaf67.
		v := AffiliateProgramSortOrderProfitability{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode AffiliateProgramSortOrderClass: %w", err)
		}
		return &v, nil
	case AffiliateProgramSortOrderCreationDateTypeID:
		// Decoding affiliateProgramSortOrderCreationDate#a319390d.
		v := AffiliateProgramSortOrderCreationDate{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode AffiliateProgramSortOrderClass: %w", err)
		}
		return &v, nil
	case AffiliateProgramSortOrderRevenueTypeID:
		// Decoding affiliateProgramSortOrderRevenue#72a2c2b8.
		v := AffiliateProgramSortOrderRevenue{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode AffiliateProgramSortOrderClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode AffiliateProgramSortOrderClass: %w", bin.NewUnexpectedID(id))
	}
}

// DecodeTDLibJSONAffiliateProgramSortOrder implements binary de-serialization for AffiliateProgramSortOrderClass.
func DecodeTDLibJSONAffiliateProgramSortOrder(buf tdjson.Decoder) (AffiliateProgramSortOrderClass, error) {
	id, err := buf.FindTypeID()
	if err != nil {
		return nil, err
	}
	switch id {
	case "affiliateProgramSortOrderProfitability":
		// Decoding affiliateProgramSortOrderProfitability#8afaaf67.
		v := AffiliateProgramSortOrderProfitability{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode AffiliateProgramSortOrderClass: %w", err)
		}
		return &v, nil
	case "affiliateProgramSortOrderCreationDate":
		// Decoding affiliateProgramSortOrderCreationDate#a319390d.
		v := AffiliateProgramSortOrderCreationDate{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode AffiliateProgramSortOrderClass: %w", err)
		}
		return &v, nil
	case "affiliateProgramSortOrderRevenue":
		// Decoding affiliateProgramSortOrderRevenue#72a2c2b8.
		v := AffiliateProgramSortOrderRevenue{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode AffiliateProgramSortOrderClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode AffiliateProgramSortOrderClass: %w", tdjson.NewUnexpectedID(id))
	}
}

// AffiliateProgramSortOrder boxes the AffiliateProgramSortOrderClass providing a helper.
type AffiliateProgramSortOrderBox struct {
	AffiliateProgramSortOrder AffiliateProgramSortOrderClass
}

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

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

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

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