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

// GetRecentlyVisitedTMeURLsRequest represents TL type `getRecentlyVisitedTMeUrls#30161a91`.
type GetRecentlyVisitedTMeURLsRequest struct {
	// Google Play referrer to identify the user
	Referrer string
}

// GetRecentlyVisitedTMeURLsRequestTypeID is TL type id of GetRecentlyVisitedTMeURLsRequest.
const GetRecentlyVisitedTMeURLsRequestTypeID = 0x30161a91

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

func (g *GetRecentlyVisitedTMeURLsRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Referrer == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetRecentlyVisitedTMeURLsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getRecentlyVisitedTMeUrls",
		ID:   GetRecentlyVisitedTMeURLsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Referrer",
			SchemaName: "referrer",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetRecentlyVisitedTMeURLsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getRecentlyVisitedTMeUrls#30161a91 as nil")
	}
	b.PutID(GetRecentlyVisitedTMeURLsRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetRecentlyVisitedTMeURLsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getRecentlyVisitedTMeUrls#30161a91 as nil")
	}
	b.PutString(g.Referrer)
	return nil
}

// Decode implements bin.Decoder.
func (g *GetRecentlyVisitedTMeURLsRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getRecentlyVisitedTMeUrls#30161a91 to nil")
	}
	if err := b.ConsumeID(GetRecentlyVisitedTMeURLsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getRecentlyVisitedTMeUrls#30161a91: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetRecentlyVisitedTMeURLsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getRecentlyVisitedTMeUrls#30161a91 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getRecentlyVisitedTMeUrls#30161a91: field referrer: %w", err)
		}
		g.Referrer = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetRecentlyVisitedTMeURLsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getRecentlyVisitedTMeUrls#30161a91 as nil")
	}
	b.ObjStart()
	b.PutID("getRecentlyVisitedTMeUrls")
	b.Comma()
	b.FieldStart("referrer")
	b.PutString(g.Referrer)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetRecentlyVisitedTMeURLsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getRecentlyVisitedTMeUrls#30161a91 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getRecentlyVisitedTMeUrls"); err != nil {
				return fmt.Errorf("unable to decode getRecentlyVisitedTMeUrls#30161a91: %w", err)
			}
		case "referrer":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode getRecentlyVisitedTMeUrls#30161a91: field referrer: %w", err)
			}
			g.Referrer = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetReferrer returns value of Referrer field.
func (g *GetRecentlyVisitedTMeURLsRequest) GetReferrer() (value string) {
	if g == nil {
		return
	}
	return g.Referrer
}

// GetRecentlyVisitedTMeURLs invokes method getRecentlyVisitedTMeUrls#30161a91 returning error if any.
func (c *Client) GetRecentlyVisitedTMeURLs(ctx context.Context, referrer string) (*TMeURLs, error) {
	var result TMeURLs

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