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

// HelpGetAppConfigRequest represents TL type `help.getAppConfig#61e3f854`.
// Get app-specific configuration, see client configuration¹ for more info on the result.
//
// Links:
//  1. https://core.telegram.org/api/config#client-configuration
//
// See https://core.telegram.org/method/help.getAppConfig for reference.
type HelpGetAppConfigRequest struct {
	// Hash for pagination, for more info click here¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets#hash-generation
	Hash int
}

// HelpGetAppConfigRequestTypeID is TL type id of HelpGetAppConfigRequest.
const HelpGetAppConfigRequestTypeID = 0x61e3f854

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

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

	return true
}

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

// FillFrom fills HelpGetAppConfigRequest from given interface.
func (g *HelpGetAppConfigRequest) FillFrom(from interface {
	GetHash() (value int)
}) {
	g.Hash = from.GetHash()
}

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

// TypeName returns name of type in TL schema.
func (*HelpGetAppConfigRequest) TypeName() string {
	return "help.getAppConfig"
}

// TypeInfo returns info about TL type.
func (g *HelpGetAppConfigRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "help.getAppConfig",
		ID:   HelpGetAppConfigRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Hash",
			SchemaName: "hash",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *HelpGetAppConfigRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode help.getAppConfig#61e3f854 as nil")
	}
	b.PutID(HelpGetAppConfigRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *HelpGetAppConfigRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode help.getAppConfig#61e3f854 as nil")
	}
	b.PutInt(g.Hash)
	return nil
}

// Decode implements bin.Decoder.
func (g *HelpGetAppConfigRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode help.getAppConfig#61e3f854 to nil")
	}
	if err := b.ConsumeID(HelpGetAppConfigRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode help.getAppConfig#61e3f854: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *HelpGetAppConfigRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode help.getAppConfig#61e3f854 to nil")
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode help.getAppConfig#61e3f854: field hash: %w", err)
		}
		g.Hash = value
	}
	return nil
}

// GetHash returns value of Hash field.
func (g *HelpGetAppConfigRequest) GetHash() (value int) {
	if g == nil {
		return
	}
	return g.Hash
}

// HelpGetAppConfig invokes method help.getAppConfig#61e3f854 returning error if any.
// Get app-specific configuration, see client configuration¹ for more info on the result.
//
// Links:
//  1. https://core.telegram.org/api/config#client-configuration
//
// See https://core.telegram.org/method/help.getAppConfig for reference.
func (c *Client) HelpGetAppConfig(ctx context.Context, hash int) (HelpAppConfigClass, error) {
	var result HelpAppConfigBox

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