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

// GetStarAdAccountURLRequest represents TL type `getStarAdAccountUrl#73a9455d`.
type GetStarAdAccountURLRequest struct {
	// Identifier of the owner of the Telegram Stars; can be identifier of an owned bot, or
	// identifier of an owned channel chat
	OwnerID MessageSenderClass
}

// GetStarAdAccountURLRequestTypeID is TL type id of GetStarAdAccountURLRequest.
const GetStarAdAccountURLRequestTypeID = 0x73a9455d

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetStarAdAccountURLRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getStarAdAccountUrl",
		ID:   GetStarAdAccountURLRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "OwnerID",
			SchemaName: "owner_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetStarAdAccountURLRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getStarAdAccountUrl#73a9455d as nil")
	}
	b.PutID(GetStarAdAccountURLRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetStarAdAccountURLRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getStarAdAccountUrl#73a9455d as nil")
	}
	if g.OwnerID == nil {
		return fmt.Errorf("unable to encode getStarAdAccountUrl#73a9455d: field owner_id is nil")
	}
	if err := g.OwnerID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode getStarAdAccountUrl#73a9455d: field owner_id: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (g *GetStarAdAccountURLRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getStarAdAccountUrl#73a9455d to nil")
	}
	if err := b.ConsumeID(GetStarAdAccountURLRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getStarAdAccountUrl#73a9455d: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetStarAdAccountURLRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getStarAdAccountUrl#73a9455d to nil")
	}
	{
		value, err := DecodeMessageSender(b)
		if err != nil {
			return fmt.Errorf("unable to decode getStarAdAccountUrl#73a9455d: field owner_id: %w", err)
		}
		g.OwnerID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetStarAdAccountURLRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getStarAdAccountUrl#73a9455d as nil")
	}
	b.ObjStart()
	b.PutID("getStarAdAccountUrl")
	b.Comma()
	b.FieldStart("owner_id")
	if g.OwnerID == nil {
		return fmt.Errorf("unable to encode getStarAdAccountUrl#73a9455d: field owner_id is nil")
	}
	if err := g.OwnerID.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode getStarAdAccountUrl#73a9455d: field owner_id: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetStarAdAccountURLRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getStarAdAccountUrl#73a9455d to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getStarAdAccountUrl"); err != nil {
				return fmt.Errorf("unable to decode getStarAdAccountUrl#73a9455d: %w", err)
			}
		case "owner_id":
			value, err := DecodeTDLibJSONMessageSender(b)
			if err != nil {
				return fmt.Errorf("unable to decode getStarAdAccountUrl#73a9455d: field owner_id: %w", err)
			}
			g.OwnerID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetOwnerID returns value of OwnerID field.
func (g *GetStarAdAccountURLRequest) GetOwnerID() (value MessageSenderClass) {
	if g == nil {
		return
	}
	return g.OwnerID
}

// GetStarAdAccountURL invokes method getStarAdAccountUrl#73a9455d returning error if any.
func (c *Client) GetStarAdAccountURL(ctx context.Context, ownerid MessageSenderClass) (*HTTPURL, error) {
	var result HTTPURL

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