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

// PaymentsGetSavedStarGiftRequest represents TL type `payments.getSavedStarGift#b455a106`.
// Fetch info about specific gifts¹ owned by a peer we control.
// Note that unlike what the name suggests, the method can be used to fetch both "saved"
// and "unsaved" gifts (aka gifts both pinned and not pinned to the profile).
//
// Links:
//  1. https://core.telegram.org/api/gifts
//
// See https://core.telegram.org/method/payments.getSavedStarGift for reference.
type PaymentsGetSavedStarGiftRequest struct {
	// List of gifts to fetch info about.
	Stargift []InputSavedStarGiftClass
}

// PaymentsGetSavedStarGiftRequestTypeID is TL type id of PaymentsGetSavedStarGiftRequest.
const PaymentsGetSavedStarGiftRequestTypeID = 0xb455a106

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

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

	return true
}

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

// FillFrom fills PaymentsGetSavedStarGiftRequest from given interface.
func (g *PaymentsGetSavedStarGiftRequest) FillFrom(from interface {
	GetStargift() (value []InputSavedStarGiftClass)
}) {
	g.Stargift = from.GetStargift()
}

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

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

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

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

// EncodeBare implements bin.BareEncoder.
func (g *PaymentsGetSavedStarGiftRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode payments.getSavedStarGift#b455a106 as nil")
	}
	b.PutVectorHeader(len(g.Stargift))
	for idx, v := range g.Stargift {
		if v == nil {
			return fmt.Errorf("unable to encode payments.getSavedStarGift#b455a106: field stargift element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.getSavedStarGift#b455a106: field stargift element with index %d: %w", idx, err)
		}
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *PaymentsGetSavedStarGiftRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode payments.getSavedStarGift#b455a106 to nil")
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.getSavedStarGift#b455a106: field stargift: %w", err)
		}

		if headerLen > 0 {
			g.Stargift = make([]InputSavedStarGiftClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeInputSavedStarGift(b)
			if err != nil {
				return fmt.Errorf("unable to decode payments.getSavedStarGift#b455a106: field stargift: %w", err)
			}
			g.Stargift = append(g.Stargift, value)
		}
	}
	return nil
}

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

// MapStargift returns field Stargift wrapped in InputSavedStarGiftClassArray helper.
func (g *PaymentsGetSavedStarGiftRequest) MapStargift() (value InputSavedStarGiftClassArray) {
	return InputSavedStarGiftClassArray(g.Stargift)
}

// PaymentsGetSavedStarGift invokes method payments.getSavedStarGift#b455a106 returning error if any.
// Fetch info about specific gifts¹ owned by a peer we control.
// Note that unlike what the name suggests, the method can be used to fetch both "saved"
// and "unsaved" gifts (aka gifts both pinned and not pinned to the profile).
//
// Links:
//  1. https://core.telegram.org/api/gifts
//
// Possible errors:
//
//	400 SAVED_ID_EMPTY: The passed inputSavedStarGiftChat.saved_id is empty.
//	400 STARGIFT_SLUG_INVALID: The specified gift slug is invalid.
//
// See https://core.telegram.org/method/payments.getSavedStarGift for reference.
func (c *Client) PaymentsGetSavedStarGift(ctx context.Context, stargift []InputSavedStarGiftClass) (*PaymentsSavedStarGifts, error) {
	var result PaymentsSavedStarGifts

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