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

// ContactsResolvePhoneRequest represents TL type `contacts.resolvePhone#8af94344`.
// Resolve a phone number to get user info, if their privacy settings allow it.
//
// See https://core.telegram.org/method/contacts.resolvePhone for reference.
type ContactsResolvePhoneRequest struct {
	// Phone number in international format, possibly obtained from a phone number deep
	// link¹.
	//
	// Links:
	//  1) https://core.telegram.org/api/links#phone-number-links
	Phone string
}

// ContactsResolvePhoneRequestTypeID is TL type id of ContactsResolvePhoneRequest.
const ContactsResolvePhoneRequestTypeID = 0x8af94344

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

func (r *ContactsResolvePhoneRequest) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.Phone == "") {
		return false
	}

	return true
}

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

// FillFrom fills ContactsResolvePhoneRequest from given interface.
func (r *ContactsResolvePhoneRequest) FillFrom(from interface {
	GetPhone() (value string)
}) {
	r.Phone = from.GetPhone()
}

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

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

// TypeInfo returns info about TL type.
func (r *ContactsResolvePhoneRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "contacts.resolvePhone",
		ID:   ContactsResolvePhoneRequestTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Phone",
			SchemaName: "phone",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (r *ContactsResolvePhoneRequest) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode contacts.resolvePhone#8af94344 as nil")
	}
	b.PutID(ContactsResolvePhoneRequestTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *ContactsResolvePhoneRequest) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode contacts.resolvePhone#8af94344 as nil")
	}
	b.PutString(r.Phone)
	return nil
}

// Decode implements bin.Decoder.
func (r *ContactsResolvePhoneRequest) Decode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode contacts.resolvePhone#8af94344 to nil")
	}
	if err := b.ConsumeID(ContactsResolvePhoneRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode contacts.resolvePhone#8af94344: %w", err)
	}
	return r.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (r *ContactsResolvePhoneRequest) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode contacts.resolvePhone#8af94344 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode contacts.resolvePhone#8af94344: field phone: %w", err)
		}
		r.Phone = value
	}
	return nil
}

// GetPhone returns value of Phone field.
func (r *ContactsResolvePhoneRequest) GetPhone() (value string) {
	if r == nil {
		return
	}
	return r.Phone
}

// ContactsResolvePhone invokes method contacts.resolvePhone#8af94344 returning error if any.
// Resolve a phone number to get user info, if their privacy settings allow it.
//
// Possible errors:
//
//	400 PHONE_NOT_OCCUPIED: No user is associated to the specified phone number.
//
// See https://core.telegram.org/method/contacts.resolvePhone for reference.
func (c *Client) ContactsResolvePhone(ctx context.Context, phone string) (*ContactsResolvedPeer, error) {
	var result ContactsResolvedPeer

	request := &ContactsResolvePhoneRequest{
		Phone: phone,
	}
	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return &result, nil
}
