// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"github.com/gotd/td/bin"
	"github.com/gotd/td/tdjson"
	"github.com/gotd/td/tdp"
	"github.com/gotd/td/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{}
)

// ChannelsToggleJoinRequestRequest represents TL type `channels.toggleJoinRequest#4c2985b6`.
// Set whether all users should request admin approval to join the group »¹.
//
// Links:
//  1. https://core.telegram.org/api/invites#join-requests
//
// See https://core.telegram.org/method/channels.toggleJoinRequest for reference.
type ChannelsToggleJoinRequestRequest struct {
	// Group
	Channel InputChannelClass
	// Toggle
	Enabled bool
}

// ChannelsToggleJoinRequestRequestTypeID is TL type id of ChannelsToggleJoinRequestRequest.
const ChannelsToggleJoinRequestRequestTypeID = 0x4c2985b6

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

func (t *ChannelsToggleJoinRequestRequest) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.Channel == nil) {
		return false
	}
	if !(t.Enabled == false) {
		return false
	}

	return true
}

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

// FillFrom fills ChannelsToggleJoinRequestRequest from given interface.
func (t *ChannelsToggleJoinRequestRequest) FillFrom(from interface {
	GetChannel() (value InputChannelClass)
	GetEnabled() (value bool)
}) {
	t.Channel = from.GetChannel()
	t.Enabled = from.GetEnabled()
}

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

// TypeName returns name of type in TL schema.
func (*ChannelsToggleJoinRequestRequest) TypeName() string {
	return "channels.toggleJoinRequest"
}

// TypeInfo returns info about TL type.
func (t *ChannelsToggleJoinRequestRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "channels.toggleJoinRequest",
		ID:   ChannelsToggleJoinRequestRequestTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Channel",
			SchemaName: "channel",
		},
		{
			Name:       "Enabled",
			SchemaName: "enabled",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *ChannelsToggleJoinRequestRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode channels.toggleJoinRequest#4c2985b6 as nil")
	}
	b.PutID(ChannelsToggleJoinRequestRequestTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *ChannelsToggleJoinRequestRequest) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode channels.toggleJoinRequest#4c2985b6 as nil")
	}
	if t.Channel == nil {
		return fmt.Errorf("unable to encode channels.toggleJoinRequest#4c2985b6: field channel is nil")
	}
	if err := t.Channel.Encode(b); err != nil {
		return fmt.Errorf("unable to encode channels.toggleJoinRequest#4c2985b6: field channel: %w", err)
	}
	b.PutBool(t.Enabled)
	return nil
}

// Decode implements bin.Decoder.
func (t *ChannelsToggleJoinRequestRequest) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode channels.toggleJoinRequest#4c2985b6 to nil")
	}
	if err := b.ConsumeID(ChannelsToggleJoinRequestRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode channels.toggleJoinRequest#4c2985b6: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *ChannelsToggleJoinRequestRequest) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode channels.toggleJoinRequest#4c2985b6 to nil")
	}
	{
		value, err := DecodeInputChannel(b)
		if err != nil {
			return fmt.Errorf("unable to decode channels.toggleJoinRequest#4c2985b6: field channel: %w", err)
		}
		t.Channel = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode channels.toggleJoinRequest#4c2985b6: field enabled: %w", err)
		}
		t.Enabled = value
	}
	return nil
}

// GetChannel returns value of Channel field.
func (t *ChannelsToggleJoinRequestRequest) GetChannel() (value InputChannelClass) {
	if t == nil {
		return
	}
	return t.Channel
}

// GetEnabled returns value of Enabled field.
func (t *ChannelsToggleJoinRequestRequest) GetEnabled() (value bool) {
	if t == nil {
		return
	}
	return t.Enabled
}

// GetChannelAsNotEmpty returns mapped value of Channel field.
func (t *ChannelsToggleJoinRequestRequest) GetChannelAsNotEmpty() (NotEmptyInputChannel, bool) {
	return t.Channel.AsNotEmpty()
}

// ChannelsToggleJoinRequest invokes method channels.toggleJoinRequest#4c2985b6 returning error if any.
// Set whether all users should request admin approval to join the group »¹.
//
// Links:
//  1. https://core.telegram.org/api/invites#join-requests
//
// Possible errors:
//
//	400 CHANNEL_INVALID: The provided channel is invalid.
//	400 CHAT_ID_INVALID: The provided chat id is invalid.
//	400 CHAT_NOT_MODIFIED: The pinned message wasn't modified.
//	400 CHAT_PUBLIC_REQUIRED: You can only enable join requests in public groups.
//
// See https://core.telegram.org/method/channels.toggleJoinRequest for reference.
func (c *Client) ChannelsToggleJoinRequest(ctx context.Context, request *ChannelsToggleJoinRequestRequest) (UpdatesClass, error) {
	var result UpdatesBox

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