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

// GetCountryFlagEmojiRequest represents TL type `getCountryFlagEmoji#3a8629fa`.
type GetCountryFlagEmojiRequest struct {
	// A two-letter ISO 3166-1 alpha-2 country code as received from getCountries
	CountryCode string
}

// GetCountryFlagEmojiRequestTypeID is TL type id of GetCountryFlagEmojiRequest.
const GetCountryFlagEmojiRequestTypeID = 0x3a8629fa

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

func (g *GetCountryFlagEmojiRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.CountryCode == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetCountryFlagEmojiRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getCountryFlagEmoji",
		ID:   GetCountryFlagEmojiRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "CountryCode",
			SchemaName: "country_code",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetCountryFlagEmojiRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getCountryFlagEmoji#3a8629fa as nil")
	}
	b.PutID(GetCountryFlagEmojiRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetCountryFlagEmojiRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getCountryFlagEmoji#3a8629fa as nil")
	}
	b.PutString(g.CountryCode)
	return nil
}

// Decode implements bin.Decoder.
func (g *GetCountryFlagEmojiRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getCountryFlagEmoji#3a8629fa to nil")
	}
	if err := b.ConsumeID(GetCountryFlagEmojiRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getCountryFlagEmoji#3a8629fa: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetCountryFlagEmojiRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getCountryFlagEmoji#3a8629fa to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getCountryFlagEmoji#3a8629fa: field country_code: %w", err)
		}
		g.CountryCode = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetCountryFlagEmojiRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getCountryFlagEmoji#3a8629fa as nil")
	}
	b.ObjStart()
	b.PutID("getCountryFlagEmoji")
	b.Comma()
	b.FieldStart("country_code")
	b.PutString(g.CountryCode)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetCountryFlagEmojiRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getCountryFlagEmoji#3a8629fa to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getCountryFlagEmoji"); err != nil {
				return fmt.Errorf("unable to decode getCountryFlagEmoji#3a8629fa: %w", err)
			}
		case "country_code":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode getCountryFlagEmoji#3a8629fa: field country_code: %w", err)
			}
			g.CountryCode = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetCountryCode returns value of CountryCode field.
func (g *GetCountryFlagEmojiRequest) GetCountryCode() (value string) {
	if g == nil {
		return
	}
	return g.CountryCode
}

// GetCountryFlagEmoji invokes method getCountryFlagEmoji#3a8629fa returning error if any.
func (c *Client) GetCountryFlagEmoji(ctx context.Context, countrycode string) (*Text, error) {
	var result Text

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