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

// GetBusinessAccountStarAmountRequest represents TL type `getBusinessAccountStarAmount#93b0b1cb`.
type GetBusinessAccountStarAmountRequest struct {
	// Unique identifier of business connection
	BusinessConnectionID string
}

// GetBusinessAccountStarAmountRequestTypeID is TL type id of GetBusinessAccountStarAmountRequest.
const GetBusinessAccountStarAmountRequestTypeID = 0x93b0b1cb

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetBusinessAccountStarAmountRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getBusinessAccountStarAmount",
		ID:   GetBusinessAccountStarAmountRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "BusinessConnectionID",
			SchemaName: "business_connection_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetBusinessAccountStarAmountRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getBusinessAccountStarAmount#93b0b1cb as nil")
	}
	b.PutID(GetBusinessAccountStarAmountRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetBusinessAccountStarAmountRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getBusinessAccountStarAmount#93b0b1cb as nil")
	}
	b.PutString(g.BusinessConnectionID)
	return nil
}

// Decode implements bin.Decoder.
func (g *GetBusinessAccountStarAmountRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getBusinessAccountStarAmount#93b0b1cb to nil")
	}
	if err := b.ConsumeID(GetBusinessAccountStarAmountRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getBusinessAccountStarAmount#93b0b1cb: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetBusinessAccountStarAmountRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getBusinessAccountStarAmount#93b0b1cb to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getBusinessAccountStarAmount#93b0b1cb: field business_connection_id: %w", err)
		}
		g.BusinessConnectionID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetBusinessAccountStarAmountRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getBusinessAccountStarAmount#93b0b1cb as nil")
	}
	b.ObjStart()
	b.PutID("getBusinessAccountStarAmount")
	b.Comma()
	b.FieldStart("business_connection_id")
	b.PutString(g.BusinessConnectionID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetBusinessAccountStarAmountRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getBusinessAccountStarAmount#93b0b1cb to nil")
	}

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

// GetBusinessConnectionID returns value of BusinessConnectionID field.
func (g *GetBusinessAccountStarAmountRequest) GetBusinessConnectionID() (value string) {
	if g == nil {
		return
	}
	return g.BusinessConnectionID
}

// GetBusinessAccountStarAmount invokes method getBusinessAccountStarAmount#93b0b1cb returning error if any.
func (c *Client) GetBusinessAccountStarAmount(ctx context.Context, businessconnectionid string) (*StarAmount, error) {
	var result StarAmount

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