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

// GetInternalLinkTypeRequest represents TL type `getInternalLinkType#8bdd5709`.
type GetInternalLinkTypeRequest struct {
	// The link
	Link string
}

// GetInternalLinkTypeRequestTypeID is TL type id of GetInternalLinkTypeRequest.
const GetInternalLinkTypeRequestTypeID = 0x8bdd5709

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

func (g *GetInternalLinkTypeRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Link == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetInternalLinkTypeRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getInternalLinkType",
		ID:   GetInternalLinkTypeRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Link",
			SchemaName: "link",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetInternalLinkTypeRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getInternalLinkType#8bdd5709 as nil")
	}
	b.PutID(GetInternalLinkTypeRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetInternalLinkTypeRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getInternalLinkType#8bdd5709 as nil")
	}
	b.PutString(g.Link)
	return nil
}

// Decode implements bin.Decoder.
func (g *GetInternalLinkTypeRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getInternalLinkType#8bdd5709 to nil")
	}
	if err := b.ConsumeID(GetInternalLinkTypeRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getInternalLinkType#8bdd5709: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetInternalLinkTypeRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getInternalLinkType#8bdd5709 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getInternalLinkType#8bdd5709: field link: %w", err)
		}
		g.Link = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetInternalLinkTypeRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getInternalLinkType#8bdd5709 as nil")
	}
	b.ObjStart()
	b.PutID("getInternalLinkType")
	b.Comma()
	b.FieldStart("link")
	b.PutString(g.Link)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetInternalLinkTypeRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getInternalLinkType#8bdd5709 to nil")
	}

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

// GetLink returns value of Link field.
func (g *GetInternalLinkTypeRequest) GetLink() (value string) {
	if g == nil {
		return
	}
	return g.Link
}

// GetInternalLinkType invokes method getInternalLinkType#8bdd5709 returning error if any.
func (c *Client) GetInternalLinkType(ctx context.Context, link string) (InternalLinkTypeClass, error) {
	var result InternalLinkTypeBox

	request := &GetInternalLinkTypeRequest{
		Link: link,
	}
	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return result.InternalLinkType, nil
}
