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

// CreateBusinessChatLinkRequest represents TL type `createBusinessChatLink#91131d40`.
type CreateBusinessChatLinkRequest struct {
	// Information about the link to create
	LinkInfo InputBusinessChatLink
}

// CreateBusinessChatLinkRequestTypeID is TL type id of CreateBusinessChatLinkRequest.
const CreateBusinessChatLinkRequestTypeID = 0x91131d40

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

func (c *CreateBusinessChatLinkRequest) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.LinkInfo.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *CreateBusinessChatLinkRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "createBusinessChatLink",
		ID:   CreateBusinessChatLinkRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "LinkInfo",
			SchemaName: "link_info",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *CreateBusinessChatLinkRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode createBusinessChatLink#91131d40 as nil")
	}
	b.PutID(CreateBusinessChatLinkRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *CreateBusinessChatLinkRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode createBusinessChatLink#91131d40 as nil")
	}
	if err := c.LinkInfo.Encode(b); err != nil {
		return fmt.Errorf("unable to encode createBusinessChatLink#91131d40: field link_info: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *CreateBusinessChatLinkRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode createBusinessChatLink#91131d40 to nil")
	}
	{
		if err := c.LinkInfo.Decode(b); err != nil {
			return fmt.Errorf("unable to decode createBusinessChatLink#91131d40: field link_info: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *CreateBusinessChatLinkRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode createBusinessChatLink#91131d40 as nil")
	}
	b.ObjStart()
	b.PutID("createBusinessChatLink")
	b.Comma()
	b.FieldStart("link_info")
	if err := c.LinkInfo.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode createBusinessChatLink#91131d40: field link_info: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

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

// GetLinkInfo returns value of LinkInfo field.
func (c *CreateBusinessChatLinkRequest) GetLinkInfo() (value InputBusinessChatLink) {
	if c == nil {
		return
	}
	return c.LinkInfo
}

// CreateBusinessChatLink invokes method createBusinessChatLink#91131d40 returning error if any.
func (c *Client) CreateBusinessChatLink(ctx context.Context, linkinfo InputBusinessChatLink) (*BusinessChatLink, error) {
	var result BusinessChatLink

	request := &CreateBusinessChatLinkRequest{
		LinkInfo: linkinfo,
	}
	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return &result, nil
}
