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

// DeletePassportElementRequest represents TL type `deletePassportElement#9981aa74`.
type DeletePassportElementRequest struct {
	// Element type
	Type PassportElementTypeClass
}

// DeletePassportElementRequestTypeID is TL type id of DeletePassportElementRequest.
const DeletePassportElementRequestTypeID = 0x9981aa74

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

func (d *DeletePassportElementRequest) Zero() bool {
	if d == nil {
		return true
	}
	if !(d.Type == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (d *DeletePassportElementRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "deletePassportElement",
		ID:   DeletePassportElementRequestTypeID,
	}
	if d == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Type",
			SchemaName: "type",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (d *DeletePassportElementRequest) Encode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode deletePassportElement#9981aa74 as nil")
	}
	b.PutID(DeletePassportElementRequestTypeID)
	return d.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (d *DeletePassportElementRequest) EncodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode deletePassportElement#9981aa74 as nil")
	}
	if d.Type == nil {
		return fmt.Errorf("unable to encode deletePassportElement#9981aa74: field type is nil")
	}
	if err := d.Type.Encode(b); err != nil {
		return fmt.Errorf("unable to encode deletePassportElement#9981aa74: field type: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (d *DeletePassportElementRequest) Decode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode deletePassportElement#9981aa74 to nil")
	}
	if err := b.ConsumeID(DeletePassportElementRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode deletePassportElement#9981aa74: %w", err)
	}
	return d.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (d *DeletePassportElementRequest) DecodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode deletePassportElement#9981aa74 to nil")
	}
	{
		value, err := DecodePassportElementType(b)
		if err != nil {
			return fmt.Errorf("unable to decode deletePassportElement#9981aa74: field type: %w", err)
		}
		d.Type = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (d *DeletePassportElementRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if d == nil {
		return fmt.Errorf("can't encode deletePassportElement#9981aa74 as nil")
	}
	b.ObjStart()
	b.PutID("deletePassportElement")
	b.Comma()
	b.FieldStart("type")
	if d.Type == nil {
		return fmt.Errorf("unable to encode deletePassportElement#9981aa74: field type is nil")
	}
	if err := d.Type.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode deletePassportElement#9981aa74: field type: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (d *DeletePassportElementRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if d == nil {
		return fmt.Errorf("can't decode deletePassportElement#9981aa74 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("deletePassportElement"); err != nil {
				return fmt.Errorf("unable to decode deletePassportElement#9981aa74: %w", err)
			}
		case "type":
			value, err := DecodeTDLibJSONPassportElementType(b)
			if err != nil {
				return fmt.Errorf("unable to decode deletePassportElement#9981aa74: field type: %w", err)
			}
			d.Type = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetType returns value of Type field.
func (d *DeletePassportElementRequest) GetType() (value PassportElementTypeClass) {
	if d == nil {
		return
	}
	return d.Type
}

// DeletePassportElement invokes method deletePassportElement#9981aa74 returning error if any.
func (c *Client) DeletePassportElement(ctx context.Context, type_ PassportElementTypeClass) error {
	var ok Ok

	request := &DeletePassportElementRequest{
		Type: type_,
	}
	if err := c.rpc.Invoke(ctx, request, &ok); err != nil {
		return err
	}
	return nil
}
