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

// JSONObjectMember represents TL type `jsonObjectMember#9483ae96`.
type JSONObjectMember struct {
	// Member's key
	Key string
	// Member's value
	Value JSONValueClass
}

// JSONObjectMemberTypeID is TL type id of JSONObjectMember.
const JSONObjectMemberTypeID = 0x9483ae96

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

func (j *JSONObjectMember) Zero() bool {
	if j == nil {
		return true
	}
	if !(j.Key == "") {
		return false
	}
	if !(j.Value == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (j *JSONObjectMember) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "jsonObjectMember",
		ID:   JSONObjectMemberTypeID,
	}
	if j == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Key",
			SchemaName: "key",
		},
		{
			Name:       "Value",
			SchemaName: "value",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (j *JSONObjectMember) Encode(b *bin.Buffer) error {
	if j == nil {
		return fmt.Errorf("can't encode jsonObjectMember#9483ae96 as nil")
	}
	b.PutID(JSONObjectMemberTypeID)
	return j.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (j *JSONObjectMember) EncodeBare(b *bin.Buffer) error {
	if j == nil {
		return fmt.Errorf("can't encode jsonObjectMember#9483ae96 as nil")
	}
	b.PutString(j.Key)
	if j.Value == nil {
		return fmt.Errorf("unable to encode jsonObjectMember#9483ae96: field value is nil")
	}
	if err := j.Value.Encode(b); err != nil {
		return fmt.Errorf("unable to encode jsonObjectMember#9483ae96: field value: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (j *JSONObjectMember) Decode(b *bin.Buffer) error {
	if j == nil {
		return fmt.Errorf("can't decode jsonObjectMember#9483ae96 to nil")
	}
	if err := b.ConsumeID(JSONObjectMemberTypeID); err != nil {
		return fmt.Errorf("unable to decode jsonObjectMember#9483ae96: %w", err)
	}
	return j.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (j *JSONObjectMember) DecodeBare(b *bin.Buffer) error {
	if j == nil {
		return fmt.Errorf("can't decode jsonObjectMember#9483ae96 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode jsonObjectMember#9483ae96: field key: %w", err)
		}
		j.Key = value
	}
	{
		value, err := DecodeJSONValue(b)
		if err != nil {
			return fmt.Errorf("unable to decode jsonObjectMember#9483ae96: field value: %w", err)
		}
		j.Value = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (j *JSONObjectMember) EncodeTDLibJSON(b tdjson.Encoder) error {
	if j == nil {
		return fmt.Errorf("can't encode jsonObjectMember#9483ae96 as nil")
	}
	b.ObjStart()
	b.PutID("jsonObjectMember")
	b.Comma()
	b.FieldStart("key")
	b.PutString(j.Key)
	b.Comma()
	b.FieldStart("value")
	if j.Value == nil {
		return fmt.Errorf("unable to encode jsonObjectMember#9483ae96: field value is nil")
	}
	if err := j.Value.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode jsonObjectMember#9483ae96: field value: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (j *JSONObjectMember) DecodeTDLibJSON(b tdjson.Decoder) error {
	if j == nil {
		return fmt.Errorf("can't decode jsonObjectMember#9483ae96 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("jsonObjectMember"); err != nil {
				return fmt.Errorf("unable to decode jsonObjectMember#9483ae96: %w", err)
			}
		case "key":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode jsonObjectMember#9483ae96: field key: %w", err)
			}
			j.Key = value
		case "value":
			value, err := DecodeTDLibJSONJSONValue(b)
			if err != nil {
				return fmt.Errorf("unable to decode jsonObjectMember#9483ae96: field value: %w", err)
			}
			j.Value = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetKey returns value of Key field.
func (j *JSONObjectMember) GetKey() (value string) {
	if j == nil {
		return
	}
	return j.Key
}

// GetValue returns value of Value field.
func (j *JSONObjectMember) GetValue() (value JSONValueClass) {
	if j == nil {
		return
	}
	return j.Value
}
