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

// PaymentsCraftStarGiftRequest represents TL type `payments.craftStarGift#b0f9684f`.
//
// See https://core.telegram.org/method/payments.craftStarGift for reference.
type PaymentsCraftStarGiftRequest struct {
	// Stargift field of PaymentsCraftStarGiftRequest.
	Stargift []InputSavedStarGiftClass
}

// PaymentsCraftStarGiftRequestTypeID is TL type id of PaymentsCraftStarGiftRequest.
const PaymentsCraftStarGiftRequestTypeID = 0xb0f9684f

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

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

	return true
}

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

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

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

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

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

// Encode implements bin.Encoder.
func (c *PaymentsCraftStarGiftRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode payments.craftStarGift#b0f9684f as nil")
	}
	b.PutID(PaymentsCraftStarGiftRequestTypeID)
	return c.EncodeBare(b)
}

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

// Decode implements bin.Decoder.
func (c *PaymentsCraftStarGiftRequest) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode payments.craftStarGift#b0f9684f to nil")
	}
	if err := b.ConsumeID(PaymentsCraftStarGiftRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode payments.craftStarGift#b0f9684f: %w", err)
	}
	return c.DecodeBare(b)
}

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

		if headerLen > 0 {
			c.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.craftStarGift#b0f9684f: field stargift: %w", err)
			}
			c.Stargift = append(c.Stargift, value)
		}
	}
	return nil
}

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

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

// PaymentsCraftStarGift invokes method payments.craftStarGift#b0f9684f returning error if any.
//
// See https://core.telegram.org/method/payments.craftStarGift for reference.
func (c *Client) PaymentsCraftStarGift(ctx context.Context, stargift []InputSavedStarGiftClass) (UpdatesClass, error) {
	var result UpdatesBox

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