// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// ChatlistsEditExportedInviteRequest represents TL type `chatlists.editExportedInvite#653db63d`.
// Edit a chat folder deep link »¹.
//
// Links:
//  1. https://core.telegram.org/api/links#chat-folder-links
//
// See https://core.telegram.org/method/chatlists.editExportedInvite for reference.
type ChatlistsEditExportedInviteRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Folder ID
	Chatlist InputChatlistDialogFilter
	// slug obtained from the chat folder deep link »¹.
	//
	// Links:
	//  1) https://core.telegram.org/api/links#chat-folder-links
	Slug string
	// If set, sets a new name for the link
	//
	// Use SetTitle and GetTitle helpers.
	Title string
	// If set, changes the list of peers shared with the link
	//
	// Use SetPeers and GetPeers helpers.
	Peers []InputPeerClass
}

// ChatlistsEditExportedInviteRequestTypeID is TL type id of ChatlistsEditExportedInviteRequest.
const ChatlistsEditExportedInviteRequestTypeID = 0x653db63d

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

func (e *ChatlistsEditExportedInviteRequest) Zero() bool {
	if e == nil {
		return true
	}
	if !(e.Flags.Zero()) {
		return false
	}
	if !(e.Chatlist.Zero()) {
		return false
	}
	if !(e.Slug == "") {
		return false
	}
	if !(e.Title == "") {
		return false
	}
	if !(e.Peers == nil) {
		return false
	}

	return true
}

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

// FillFrom fills ChatlistsEditExportedInviteRequest from given interface.
func (e *ChatlistsEditExportedInviteRequest) FillFrom(from interface {
	GetChatlist() (value InputChatlistDialogFilter)
	GetSlug() (value string)
	GetTitle() (value string, ok bool)
	GetPeers() (value []InputPeerClass, ok bool)
}) {
	e.Chatlist = from.GetChatlist()
	e.Slug = from.GetSlug()
	if val, ok := from.GetTitle(); ok {
		e.Title = val
	}

	if val, ok := from.GetPeers(); ok {
		e.Peers = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*ChatlistsEditExportedInviteRequest) TypeName() string {
	return "chatlists.editExportedInvite"
}

// TypeInfo returns info about TL type.
func (e *ChatlistsEditExportedInviteRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "chatlists.editExportedInvite",
		ID:   ChatlistsEditExportedInviteRequestTypeID,
	}
	if e == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Chatlist",
			SchemaName: "chatlist",
		},
		{
			Name:       "Slug",
			SchemaName: "slug",
		},
		{
			Name:       "Title",
			SchemaName: "title",
			Null:       !e.Flags.Has(1),
		},
		{
			Name:       "Peers",
			SchemaName: "peers",
			Null:       !e.Flags.Has(2),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (e *ChatlistsEditExportedInviteRequest) SetFlags() {
	if !(e.Title == "") {
		e.Flags.Set(1)
	}
	if !(e.Peers == nil) {
		e.Flags.Set(2)
	}
}

// Encode implements bin.Encoder.
func (e *ChatlistsEditExportedInviteRequest) Encode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode chatlists.editExportedInvite#653db63d as nil")
	}
	b.PutID(ChatlistsEditExportedInviteRequestTypeID)
	return e.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (e *ChatlistsEditExportedInviteRequest) EncodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode chatlists.editExportedInvite#653db63d as nil")
	}
	e.SetFlags()
	if err := e.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode chatlists.editExportedInvite#653db63d: field flags: %w", err)
	}
	if err := e.Chatlist.Encode(b); err != nil {
		return fmt.Errorf("unable to encode chatlists.editExportedInvite#653db63d: field chatlist: %w", err)
	}
	b.PutString(e.Slug)
	if e.Flags.Has(1) {
		b.PutString(e.Title)
	}
	if e.Flags.Has(2) {
		b.PutVectorHeader(len(e.Peers))
		for idx, v := range e.Peers {
			if v == nil {
				return fmt.Errorf("unable to encode chatlists.editExportedInvite#653db63d: field peers element with index %d is nil", idx)
			}
			if err := v.Encode(b); err != nil {
				return fmt.Errorf("unable to encode chatlists.editExportedInvite#653db63d: field peers element with index %d: %w", idx, err)
			}
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (e *ChatlistsEditExportedInviteRequest) Decode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode chatlists.editExportedInvite#653db63d to nil")
	}
	if err := b.ConsumeID(ChatlistsEditExportedInviteRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode chatlists.editExportedInvite#653db63d: %w", err)
	}
	return e.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (e *ChatlistsEditExportedInviteRequest) DecodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode chatlists.editExportedInvite#653db63d to nil")
	}
	{
		if err := e.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode chatlists.editExportedInvite#653db63d: field flags: %w", err)
		}
	}
	{
		if err := e.Chatlist.Decode(b); err != nil {
			return fmt.Errorf("unable to decode chatlists.editExportedInvite#653db63d: field chatlist: %w", err)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode chatlists.editExportedInvite#653db63d: field slug: %w", err)
		}
		e.Slug = value
	}
	if e.Flags.Has(1) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode chatlists.editExportedInvite#653db63d: field title: %w", err)
		}
		e.Title = value
	}
	if e.Flags.Has(2) {
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode chatlists.editExportedInvite#653db63d: field peers: %w", err)
		}

		if headerLen > 0 {
			e.Peers = make([]InputPeerClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeInputPeer(b)
			if err != nil {
				return fmt.Errorf("unable to decode chatlists.editExportedInvite#653db63d: field peers: %w", err)
			}
			e.Peers = append(e.Peers, value)
		}
	}
	return nil
}

// GetChatlist returns value of Chatlist field.
func (e *ChatlistsEditExportedInviteRequest) GetChatlist() (value InputChatlistDialogFilter) {
	if e == nil {
		return
	}
	return e.Chatlist
}

// GetSlug returns value of Slug field.
func (e *ChatlistsEditExportedInviteRequest) GetSlug() (value string) {
	if e == nil {
		return
	}
	return e.Slug
}

// SetTitle sets value of Title conditional field.
func (e *ChatlistsEditExportedInviteRequest) SetTitle(value string) {
	e.Flags.Set(1)
	e.Title = value
}

// GetTitle returns value of Title conditional field and
// boolean which is true if field was set.
func (e *ChatlistsEditExportedInviteRequest) GetTitle() (value string, ok bool) {
	if e == nil {
		return
	}
	if !e.Flags.Has(1) {
		return value, false
	}
	return e.Title, true
}

// SetPeers sets value of Peers conditional field.
func (e *ChatlistsEditExportedInviteRequest) SetPeers(value []InputPeerClass) {
	e.Flags.Set(2)
	e.Peers = value
}

// GetPeers returns value of Peers conditional field and
// boolean which is true if field was set.
func (e *ChatlistsEditExportedInviteRequest) GetPeers() (value []InputPeerClass, ok bool) {
	if e == nil {
		return
	}
	if !e.Flags.Has(2) {
		return value, false
	}
	return e.Peers, true
}

// MapPeers returns field Peers wrapped in InputPeerClassArray helper.
func (e *ChatlistsEditExportedInviteRequest) MapPeers() (value InputPeerClassArray, ok bool) {
	if !e.Flags.Has(2) {
		return value, false
	}
	return InputPeerClassArray(e.Peers), true
}

// ChatlistsEditExportedInvite invokes method chatlists.editExportedInvite#653db63d returning error if any.
// Edit a chat folder deep link »¹.
//
// Links:
//  1. https://core.telegram.org/api/links#chat-folder-links
//
// Possible errors:
//
//	400 CHANNEL_INVALID: The provided channel is invalid.
//	400 FILTER_ID_INVALID: The specified filter ID is invalid.
//	400 FILTER_NOT_SUPPORTED: The specified filter cannot be used in this context.
//	400 INVITE_SLUG_EMPTY: The specified invite slug is empty.
//	400 INVITE_SLUG_EXPIRED: The specified chat folder link has expired.
//	400 PEERS_LIST_EMPTY: The specified list of peers is empty.
//
// See https://core.telegram.org/method/chatlists.editExportedInvite for reference.
func (c *Client) ChatlistsEditExportedInvite(ctx context.Context, request *ChatlistsEditExportedInviteRequest) (*ExportedChatlistInvite, error) {
	var result ExportedChatlistInvite

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