// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// PaymentsGetStarGiftWithdrawalURLRequest represents TL type `payments.getStarGiftWithdrawalUrl#d06e93a8`.
// Convert a collectible gift »¹ to an NFT on the TON blockchain.
//
// Links:
//  1. https://core.telegram.org/api/gifts
//
// See https://core.telegram.org/method/payments.getStarGiftWithdrawalUrl for reference.
type PaymentsGetStarGiftWithdrawalURLRequest struct {
	// The collectible gift to export.
	Stargift InputSavedStarGiftClass
	// The current user's 2FA password, passed as specified here »¹.
	//
	// Links:
	//  1) https://core.telegram.org/api/srp
	Password InputCheckPasswordSRPClass
}

// PaymentsGetStarGiftWithdrawalURLRequestTypeID is TL type id of PaymentsGetStarGiftWithdrawalURLRequest.
const PaymentsGetStarGiftWithdrawalURLRequestTypeID = 0xd06e93a8

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

func (g *PaymentsGetStarGiftWithdrawalURLRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Stargift == nil) {
		return false
	}
	if !(g.Password == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsGetStarGiftWithdrawalURLRequest from given interface.
func (g *PaymentsGetStarGiftWithdrawalURLRequest) FillFrom(from interface {
	GetStargift() (value InputSavedStarGiftClass)
	GetPassword() (value InputCheckPasswordSRPClass)
}) {
	g.Stargift = from.GetStargift()
	g.Password = from.GetPassword()
}

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

// TypeName returns name of type in TL schema.
func (*PaymentsGetStarGiftWithdrawalURLRequest) TypeName() string {
	return "payments.getStarGiftWithdrawalUrl"
}

// TypeInfo returns info about TL type.
func (g *PaymentsGetStarGiftWithdrawalURLRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.getStarGiftWithdrawalUrl",
		ID:   PaymentsGetStarGiftWithdrawalURLRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Stargift",
			SchemaName: "stargift",
		},
		{
			Name:       "Password",
			SchemaName: "password",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *PaymentsGetStarGiftWithdrawalURLRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode payments.getStarGiftWithdrawalUrl#d06e93a8 as nil")
	}
	b.PutID(PaymentsGetStarGiftWithdrawalURLRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *PaymentsGetStarGiftWithdrawalURLRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode payments.getStarGiftWithdrawalUrl#d06e93a8 as nil")
	}
	if g.Stargift == nil {
		return fmt.Errorf("unable to encode payments.getStarGiftWithdrawalUrl#d06e93a8: field stargift is nil")
	}
	if err := g.Stargift.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.getStarGiftWithdrawalUrl#d06e93a8: field stargift: %w", err)
	}
	if g.Password == nil {
		return fmt.Errorf("unable to encode payments.getStarGiftWithdrawalUrl#d06e93a8: field password is nil")
	}
	if err := g.Password.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.getStarGiftWithdrawalUrl#d06e93a8: field password: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (g *PaymentsGetStarGiftWithdrawalURLRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode payments.getStarGiftWithdrawalUrl#d06e93a8 to nil")
	}
	if err := b.ConsumeID(PaymentsGetStarGiftWithdrawalURLRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode payments.getStarGiftWithdrawalUrl#d06e93a8: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *PaymentsGetStarGiftWithdrawalURLRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode payments.getStarGiftWithdrawalUrl#d06e93a8 to nil")
	}
	{
		value, err := DecodeInputSavedStarGift(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.getStarGiftWithdrawalUrl#d06e93a8: field stargift: %w", err)
		}
		g.Stargift = value
	}
	{
		value, err := DecodeInputCheckPasswordSRP(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.getStarGiftWithdrawalUrl#d06e93a8: field password: %w", err)
		}
		g.Password = value
	}
	return nil
}

// GetStargift returns value of Stargift field.
func (g *PaymentsGetStarGiftWithdrawalURLRequest) GetStargift() (value InputSavedStarGiftClass) {
	if g == nil {
		return
	}
	return g.Stargift
}

// GetPassword returns value of Password field.
func (g *PaymentsGetStarGiftWithdrawalURLRequest) GetPassword() (value InputCheckPasswordSRPClass) {
	if g == nil {
		return
	}
	return g.Password
}

// GetPasswordAsNotEmpty returns mapped value of Password field.
func (g *PaymentsGetStarGiftWithdrawalURLRequest) GetPasswordAsNotEmpty() (*InputCheckPasswordSRP, bool) {
	return g.Password.AsNotEmpty()
}

// PaymentsGetStarGiftWithdrawalURL invokes method payments.getStarGiftWithdrawalUrl#d06e93a8 returning error if any.
// Convert a collectible gift »¹ to an NFT on the TON blockchain.
//
// Links:
//  1. https://core.telegram.org/api/gifts
//
// Possible errors:
//
//	400 PASSWORD_HASH_INVALID: The provided password hash is invalid.
//	400 PASSWORD_TOO_FRESH_%d: The password was modified less than 24 hours ago, try again in %d seconds.
//	400 SESSION_TOO_FRESH_%d: This session was created less than 24 hours ago, try again in %d seconds.
//
// See https://core.telegram.org/method/payments.getStarGiftWithdrawalUrl for reference.
func (c *Client) PaymentsGetStarGiftWithdrawalURL(ctx context.Context, request *PaymentsGetStarGiftWithdrawalURLRequest) (*PaymentsStarGiftWithdrawalURL, error) {
	var result PaymentsStarGiftWithdrawalURL

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