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

// GetPhoneNumberInfoSyncRequest represents TL type `getPhoneNumberInfoSync#209b7d38`.
type GetPhoneNumberInfoSyncRequest struct {
	// A two-letter ISO 639-1 language code for country information localization
	LanguageCode string
	// The phone number prefix
	PhoneNumberPrefix string
}

// GetPhoneNumberInfoSyncRequestTypeID is TL type id of GetPhoneNumberInfoSyncRequest.
const GetPhoneNumberInfoSyncRequestTypeID = 0x209b7d38

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

func (g *GetPhoneNumberInfoSyncRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.LanguageCode == "") {
		return false
	}
	if !(g.PhoneNumberPrefix == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetPhoneNumberInfoSyncRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getPhoneNumberInfoSync",
		ID:   GetPhoneNumberInfoSyncRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "LanguageCode",
			SchemaName: "language_code",
		},
		{
			Name:       "PhoneNumberPrefix",
			SchemaName: "phone_number_prefix",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetPhoneNumberInfoSyncRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getPhoneNumberInfoSync#209b7d38 as nil")
	}
	b.PutID(GetPhoneNumberInfoSyncRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetPhoneNumberInfoSyncRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getPhoneNumberInfoSync#209b7d38 as nil")
	}
	b.PutString(g.LanguageCode)
	b.PutString(g.PhoneNumberPrefix)
	return nil
}

// Decode implements bin.Decoder.
func (g *GetPhoneNumberInfoSyncRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getPhoneNumberInfoSync#209b7d38 to nil")
	}
	if err := b.ConsumeID(GetPhoneNumberInfoSyncRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getPhoneNumberInfoSync#209b7d38: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetPhoneNumberInfoSyncRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getPhoneNumberInfoSync#209b7d38 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getPhoneNumberInfoSync#209b7d38: field language_code: %w", err)
		}
		g.LanguageCode = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getPhoneNumberInfoSync#209b7d38: field phone_number_prefix: %w", err)
		}
		g.PhoneNumberPrefix = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetPhoneNumberInfoSyncRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getPhoneNumberInfoSync#209b7d38 as nil")
	}
	b.ObjStart()
	b.PutID("getPhoneNumberInfoSync")
	b.Comma()
	b.FieldStart("language_code")
	b.PutString(g.LanguageCode)
	b.Comma()
	b.FieldStart("phone_number_prefix")
	b.PutString(g.PhoneNumberPrefix)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetPhoneNumberInfoSyncRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getPhoneNumberInfoSync#209b7d38 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getPhoneNumberInfoSync"); err != nil {
				return fmt.Errorf("unable to decode getPhoneNumberInfoSync#209b7d38: %w", err)
			}
		case "language_code":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode getPhoneNumberInfoSync#209b7d38: field language_code: %w", err)
			}
			g.LanguageCode = value
		case "phone_number_prefix":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode getPhoneNumberInfoSync#209b7d38: field phone_number_prefix: %w", err)
			}
			g.PhoneNumberPrefix = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetLanguageCode returns value of LanguageCode field.
func (g *GetPhoneNumberInfoSyncRequest) GetLanguageCode() (value string) {
	if g == nil {
		return
	}
	return g.LanguageCode
}

// GetPhoneNumberPrefix returns value of PhoneNumberPrefix field.
func (g *GetPhoneNumberInfoSyncRequest) GetPhoneNumberPrefix() (value string) {
	if g == nil {
		return
	}
	return g.PhoneNumberPrefix
}

// GetPhoneNumberInfoSync invokes method getPhoneNumberInfoSync#209b7d38 returning error if any.
func (c *Client) GetPhoneNumberInfoSync(ctx context.Context, request *GetPhoneNumberInfoSyncRequest) (*PhoneNumberInfo, error) {
	var result PhoneNumberInfo

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