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

// StatsBroadcastRevenueWithdrawalURL represents TL type `stats.broadcastRevenueWithdrawalUrl#ec659737`.
// Contains the URL to use to withdraw channel ad revenue¹.
//
// Links:
//  1. https://core.telegram.org/api/revenue#withdrawing-revenue
//
// See https://core.telegram.org/constructor/stats.broadcastRevenueWithdrawalUrl for reference.
type StatsBroadcastRevenueWithdrawalURL struct {
	// A unique URL to a Fragment page where the user will be able to specify and submit the
	// address of the TON wallet where the funds will be sent.
	URL string
}

// StatsBroadcastRevenueWithdrawalURLTypeID is TL type id of StatsBroadcastRevenueWithdrawalURL.
const StatsBroadcastRevenueWithdrawalURLTypeID = 0xec659737

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

func (b *StatsBroadcastRevenueWithdrawalURL) Zero() bool {
	if b == nil {
		return true
	}
	if !(b.URL == "") {
		return false
	}

	return true
}

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

// FillFrom fills StatsBroadcastRevenueWithdrawalURL from given interface.
func (b *StatsBroadcastRevenueWithdrawalURL) FillFrom(from interface {
	GetURL() (value string)
}) {
	b.URL = from.GetURL()
}

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

// TypeName returns name of type in TL schema.
func (*StatsBroadcastRevenueWithdrawalURL) TypeName() string {
	return "stats.broadcastRevenueWithdrawalUrl"
}

// TypeInfo returns info about TL type.
func (b *StatsBroadcastRevenueWithdrawalURL) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "stats.broadcastRevenueWithdrawalUrl",
		ID:   StatsBroadcastRevenueWithdrawalURLTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "URL",
			SchemaName: "url",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (b *StatsBroadcastRevenueWithdrawalURL) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode stats.broadcastRevenueWithdrawalUrl#ec659737 as nil")
	}
	buf.PutID(StatsBroadcastRevenueWithdrawalURLTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *StatsBroadcastRevenueWithdrawalURL) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode stats.broadcastRevenueWithdrawalUrl#ec659737 as nil")
	}
	buf.PutString(b.URL)
	return nil
}

// Decode implements bin.Decoder.
func (b *StatsBroadcastRevenueWithdrawalURL) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode stats.broadcastRevenueWithdrawalUrl#ec659737 to nil")
	}
	if err := buf.ConsumeID(StatsBroadcastRevenueWithdrawalURLTypeID); err != nil {
		return fmt.Errorf("unable to decode stats.broadcastRevenueWithdrawalUrl#ec659737: %w", err)
	}
	return b.DecodeBare(buf)
}

// DecodeBare implements bin.BareDecoder.
func (b *StatsBroadcastRevenueWithdrawalURL) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode stats.broadcastRevenueWithdrawalUrl#ec659737 to nil")
	}
	{
		value, err := buf.String()
		if err != nil {
			return fmt.Errorf("unable to decode stats.broadcastRevenueWithdrawalUrl#ec659737: field url: %w", err)
		}
		b.URL = value
	}
	return nil
}

// GetURL returns value of URL field.
func (b *StatsBroadcastRevenueWithdrawalURL) GetURL() (value string) {
	if b == nil {
		return
	}
	return b.URL
}
