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

// DeleteChatFolderRequest represents TL type `deleteChatFolder#74260b87`.
type DeleteChatFolderRequest struct {
	// Chat folder identifier
	ChatFolderID int32
	// Identifiers of the chats to leave. The chats must be pinned or always included in the
	// folder
	LeaveChatIDs []int64
}

// DeleteChatFolderRequestTypeID is TL type id of DeleteChatFolderRequest.
const DeleteChatFolderRequestTypeID = 0x74260b87

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

func (d *DeleteChatFolderRequest) Zero() bool {
	if d == nil {
		return true
	}
	if !(d.ChatFolderID == 0) {
		return false
	}
	if !(d.LeaveChatIDs == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (d *DeleteChatFolderRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "deleteChatFolder",
		ID:   DeleteChatFolderRequestTypeID,
	}
	if d == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatFolderID",
			SchemaName: "chat_folder_id",
		},
		{
			Name:       "LeaveChatIDs",
			SchemaName: "leave_chat_ids",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (d *DeleteChatFolderRequest) Encode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode deleteChatFolder#74260b87 as nil")
	}
	b.PutID(DeleteChatFolderRequestTypeID)
	return d.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (d *DeleteChatFolderRequest) EncodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode deleteChatFolder#74260b87 as nil")
	}
	b.PutInt32(d.ChatFolderID)
	b.PutInt(len(d.LeaveChatIDs))
	for _, v := range d.LeaveChatIDs {
		b.PutInt53(v)
	}
	return nil
}

// Decode implements bin.Decoder.
func (d *DeleteChatFolderRequest) Decode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode deleteChatFolder#74260b87 to nil")
	}
	if err := b.ConsumeID(DeleteChatFolderRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode deleteChatFolder#74260b87: %w", err)
	}
	return d.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (d *DeleteChatFolderRequest) DecodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode deleteChatFolder#74260b87 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode deleteChatFolder#74260b87: field chat_folder_id: %w", err)
		}
		d.ChatFolderID = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode deleteChatFolder#74260b87: field leave_chat_ids: %w", err)
		}

		if headerLen > 0 {
			d.LeaveChatIDs = make([]int64, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode deleteChatFolder#74260b87: field leave_chat_ids: %w", err)
			}
			d.LeaveChatIDs = append(d.LeaveChatIDs, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (d *DeleteChatFolderRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if d == nil {
		return fmt.Errorf("can't encode deleteChatFolder#74260b87 as nil")
	}
	b.ObjStart()
	b.PutID("deleteChatFolder")
	b.Comma()
	b.FieldStart("chat_folder_id")
	b.PutInt32(d.ChatFolderID)
	b.Comma()
	b.FieldStart("leave_chat_ids")
	b.ArrStart()
	for _, v := range d.LeaveChatIDs {
		b.PutInt53(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (d *DeleteChatFolderRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if d == nil {
		return fmt.Errorf("can't decode deleteChatFolder#74260b87 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("deleteChatFolder"); err != nil {
				return fmt.Errorf("unable to decode deleteChatFolder#74260b87: %w", err)
			}
		case "chat_folder_id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode deleteChatFolder#74260b87: field chat_folder_id: %w", err)
			}
			d.ChatFolderID = value
		case "leave_chat_ids":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int53()
				if err != nil {
					return fmt.Errorf("unable to decode deleteChatFolder#74260b87: field leave_chat_ids: %w", err)
				}
				d.LeaveChatIDs = append(d.LeaveChatIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode deleteChatFolder#74260b87: field leave_chat_ids: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetLeaveChatIDs returns value of LeaveChatIDs field.
func (d *DeleteChatFolderRequest) GetLeaveChatIDs() (value []int64) {
	if d == nil {
		return
	}
	return d.LeaveChatIDs
}

// DeleteChatFolder invokes method deleteChatFolder#74260b87 returning error if any.
func (c *Client) DeleteChatFolder(ctx context.Context, request *DeleteChatFolderRequest) error {
	var ok Ok

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