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

// ContactsImportContactTokenRequest represents TL type `contacts.importContactToken#13005788`.
// Obtain user info from a temporary profile link¹.
//
// Links:
//  1. https://core.telegram.org/api/links#temporary-profile-links
//
// See https://core.telegram.org/method/contacts.importContactToken for reference.
type ContactsImportContactTokenRequest struct {
	// The token extracted from the temporary profile link¹.
	//
	// Links:
	//  1) https://core.telegram.org/api/links#temporary-profile-links
	Token string
}

// ContactsImportContactTokenRequestTypeID is TL type id of ContactsImportContactTokenRequest.
const ContactsImportContactTokenRequestTypeID = 0x13005788

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

func (i *ContactsImportContactTokenRequest) Zero() bool {
	if i == nil {
		return true
	}
	if !(i.Token == "") {
		return false
	}

	return true
}

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

// FillFrom fills ContactsImportContactTokenRequest from given interface.
func (i *ContactsImportContactTokenRequest) FillFrom(from interface {
	GetToken() (value string)
}) {
	i.Token = from.GetToken()
}

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

// TypeName returns name of type in TL schema.
func (*ContactsImportContactTokenRequest) TypeName() string {
	return "contacts.importContactToken"
}

// TypeInfo returns info about TL type.
func (i *ContactsImportContactTokenRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "contacts.importContactToken",
		ID:   ContactsImportContactTokenRequestTypeID,
	}
	if i == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Token",
			SchemaName: "token",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (i *ContactsImportContactTokenRequest) Encode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode contacts.importContactToken#13005788 as nil")
	}
	b.PutID(ContactsImportContactTokenRequestTypeID)
	return i.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (i *ContactsImportContactTokenRequest) EncodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode contacts.importContactToken#13005788 as nil")
	}
	b.PutString(i.Token)
	return nil
}

// Decode implements bin.Decoder.
func (i *ContactsImportContactTokenRequest) Decode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode contacts.importContactToken#13005788 to nil")
	}
	if err := b.ConsumeID(ContactsImportContactTokenRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode contacts.importContactToken#13005788: %w", err)
	}
	return i.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (i *ContactsImportContactTokenRequest) DecodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode contacts.importContactToken#13005788 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode contacts.importContactToken#13005788: field token: %w", err)
		}
		i.Token = value
	}
	return nil
}

// GetToken returns value of Token field.
func (i *ContactsImportContactTokenRequest) GetToken() (value string) {
	if i == nil {
		return
	}
	return i.Token
}

// ContactsImportContactToken invokes method contacts.importContactToken#13005788 returning error if any.
// Obtain user info from a temporary profile link¹.
//
// Links:
//  1. https://core.telegram.org/api/links#temporary-profile-links
//
// Possible errors:
//
//	400 IMPORT_TOKEN_INVALID: The specified token is invalid.
//
// See https://core.telegram.org/method/contacts.importContactToken for reference.
func (c *Client) ContactsImportContactToken(ctx context.Context, token string) (UserClass, error) {
	var result UserBox

	request := &ContactsImportContactTokenRequest{
		Token: token,
	}
	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return result.User, nil
}
