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

// GetChatFolderChatsToLeaveRequest represents TL type `getChatFolderChatsToLeave#8dc1e6af`.
type GetChatFolderChatsToLeaveRequest struct {
	// Chat folder identifier
	ChatFolderID int32
}

// GetChatFolderChatsToLeaveRequestTypeID is TL type id of GetChatFolderChatsToLeaveRequest.
const GetChatFolderChatsToLeaveRequestTypeID = 0x8dc1e6af

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetChatFolderChatsToLeaveRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getChatFolderChatsToLeave",
		ID:   GetChatFolderChatsToLeaveRequestTypeID,
	}
	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 *GetChatFolderChatsToLeaveRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getChatFolderChatsToLeave#8dc1e6af as nil")
	}
	b.PutID(GetChatFolderChatsToLeaveRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetChatFolderChatsToLeaveRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getChatFolderChatsToLeave#8dc1e6af as nil")
	}
	b.PutInt32(g.ChatFolderID)
	return nil
}

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

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

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetChatFolderChatsToLeaveRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getChatFolderChatsToLeave#8dc1e6af as nil")
	}
	b.ObjStart()
	b.PutID("getChatFolderChatsToLeave")
	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 *GetChatFolderChatsToLeaveRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getChatFolderChatsToLeave#8dc1e6af to nil")
	}

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

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

// GetChatFolderChatsToLeave invokes method getChatFolderChatsToLeave#8dc1e6af returning error if any.
func (c *Client) GetChatFolderChatsToLeave(ctx context.Context, chatfolderid int32) (*Chats, error) {
	var result Chats

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