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

// GetChatsForChatFolderInviteLinkRequest represents TL type `getChatsForChatFolderInviteLink#6fac4949`.
type GetChatsForChatFolderInviteLinkRequest struct {
	// Chat folder identifier
	ChatFolderID int32
}

// GetChatsForChatFolderInviteLinkRequestTypeID is TL type id of GetChatsForChatFolderInviteLinkRequest.
const GetChatsForChatFolderInviteLinkRequestTypeID = 0x6fac4949

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

func (g *GetChatsForChatFolderInviteLinkRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.ChatFolderID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetChatsForChatFolderInviteLinkRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getChatsForChatFolderInviteLink",
		ID:   GetChatsForChatFolderInviteLinkRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatFolderID",
			SchemaName: "chat_folder_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetChatsForChatFolderInviteLinkRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getChatsForChatFolderInviteLink#6fac4949 as nil")
	}
	b.PutID(GetChatsForChatFolderInviteLinkRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetChatsForChatFolderInviteLinkRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getChatsForChatFolderInviteLink#6fac4949 as nil")
	}
	b.PutInt32(g.ChatFolderID)
	return nil
}

// Decode implements bin.Decoder.
func (g *GetChatsForChatFolderInviteLinkRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getChatsForChatFolderInviteLink#6fac4949 to nil")
	}
	if err := b.ConsumeID(GetChatsForChatFolderInviteLinkRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getChatsForChatFolderInviteLink#6fac4949: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetChatsForChatFolderInviteLinkRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getChatsForChatFolderInviteLink#6fac4949 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getChatsForChatFolderInviteLink#6fac4949: field chat_folder_id: %w", err)
		}
		g.ChatFolderID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetChatsForChatFolderInviteLinkRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getChatsForChatFolderInviteLink#6fac4949 as nil")
	}
	b.ObjStart()
	b.PutID("getChatsForChatFolderInviteLink")
	b.Comma()
	b.FieldStart("chat_folder_id")
	b.PutInt32(g.ChatFolderID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetChatsForChatFolderInviteLinkRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getChatsForChatFolderInviteLink#6fac4949 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getChatsForChatFolderInviteLink"); err != nil {
				return fmt.Errorf("unable to decode getChatsForChatFolderInviteLink#6fac4949: %w", err)
			}
		case "chat_folder_id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode getChatsForChatFolderInviteLink#6fac4949: field chat_folder_id: %w", err)
			}
			g.ChatFolderID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetChatFolderID returns value of ChatFolderID field.
func (g *GetChatsForChatFolderInviteLinkRequest) GetChatFolderID() (value int32) {
	if g == nil {
		return
	}
	return g.ChatFolderID
}

// GetChatsForChatFolderInviteLink invokes method getChatsForChatFolderInviteLink#6fac4949 returning error if any.
func (c *Client) GetChatsForChatFolderInviteLink(ctx context.Context, chatfolderid int32) (*Chats, error) {
	var result Chats

	request := &GetChatsForChatFolderInviteLinkRequest{
		ChatFolderID: chatfolderid,
	}
	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return &result, nil
}
