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

// Data represents TL type `data#d2f3419`.
type Data struct {
	// Data
	Data []byte
}

// DataTypeID is TL type id of Data.
const DataTypeID = 0xd2f3419

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

func (d *Data) Zero() bool {
	if d == nil {
		return true
	}
	if !(d.Data == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (d *Data) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "data",
		ID:   DataTypeID,
	}
	if d == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Data",
			SchemaName: "data",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (d *Data) Encode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode data#d2f3419 as nil")
	}
	b.PutID(DataTypeID)
	return d.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (d *Data) EncodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode data#d2f3419 as nil")
	}
	b.PutBytes(d.Data)
	return nil
}

// Decode implements bin.Decoder.
func (d *Data) Decode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode data#d2f3419 to nil")
	}
	if err := b.ConsumeID(DataTypeID); err != nil {
		return fmt.Errorf("unable to decode data#d2f3419: %w", err)
	}
	return d.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (d *Data) DecodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode data#d2f3419 to nil")
	}
	{
		value, err := b.Bytes()
		if err != nil {
			return fmt.Errorf("unable to decode data#d2f3419: field data: %w", err)
		}
		d.Data = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (d *Data) EncodeTDLibJSON(b tdjson.Encoder) error {
	if d == nil {
		return fmt.Errorf("can't encode data#d2f3419 as nil")
	}
	b.ObjStart()
	b.PutID("data")
	b.Comma()
	b.FieldStart("data")
	b.PutBytes(d.Data)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (d *Data) DecodeTDLibJSON(b tdjson.Decoder) error {
	if d == nil {
		return fmt.Errorf("can't decode data#d2f3419 to nil")
	}

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

// GetData returns value of Data field.
func (d *Data) GetData() (value []byte) {
	if d == nil {
		return
	}
	return d.Data
}
