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

// CloseRequest represents TL type `close#b933e17f`.
type CloseRequest struct {
}

// CloseRequestTypeID is TL type id of CloseRequest.
const CloseRequestTypeID = 0xb933e17f

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

func (c *CloseRequest) Zero() bool {
	if c == nil {
		return true
	}

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (c *CloseRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode close#b933e17f as nil")
	}
	b.PutID(CloseRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *CloseRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode close#b933e17f as nil")
	}
	return nil
}

// Decode implements bin.Decoder.
func (c *CloseRequest) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode close#b933e17f to nil")
	}
	if err := b.ConsumeID(CloseRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode close#b933e17f: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *CloseRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode close#b933e17f to nil")
	}
	return nil
}

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

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *CloseRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode close#b933e17f to nil")
	}

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

// Close invokes method close#b933e17f returning error if any.
func (c *Client) Close(ctx context.Context) error {
	var ok Ok

	request := &CloseRequest{}
	if err := c.rpc.Invoke(ctx, request, &ok); err != nil {
		return err
	}
	return nil
}
