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

// DeleteBusinessMessagesRequest represents TL type `deleteBusinessMessages#17fff9a9`.
type DeleteBusinessMessagesRequest struct {
	// Unique identifier of business connection through which the messages were received
	BusinessConnectionID string
	// Identifier of the messages
	MessageIDs []int64
}

// DeleteBusinessMessagesRequestTypeID is TL type id of DeleteBusinessMessagesRequest.
const DeleteBusinessMessagesRequestTypeID = 0x17fff9a9

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

func (d *DeleteBusinessMessagesRequest) Zero() bool {
	if d == nil {
		return true
	}
	if !(d.BusinessConnectionID == "") {
		return false
	}
	if !(d.MessageIDs == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (d *DeleteBusinessMessagesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "deleteBusinessMessages",
		ID:   DeleteBusinessMessagesRequestTypeID,
	}
	if d == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "BusinessConnectionID",
			SchemaName: "business_connection_id",
		},
		{
			Name:       "MessageIDs",
			SchemaName: "message_ids",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (d *DeleteBusinessMessagesRequest) Encode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode deleteBusinessMessages#17fff9a9 as nil")
	}
	b.PutID(DeleteBusinessMessagesRequestTypeID)
	return d.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (d *DeleteBusinessMessagesRequest) EncodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode deleteBusinessMessages#17fff9a9 as nil")
	}
	b.PutString(d.BusinessConnectionID)
	b.PutInt(len(d.MessageIDs))
	for _, v := range d.MessageIDs {
		b.PutInt53(v)
	}
	return nil
}

// Decode implements bin.Decoder.
func (d *DeleteBusinessMessagesRequest) Decode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode deleteBusinessMessages#17fff9a9 to nil")
	}
	if err := b.ConsumeID(DeleteBusinessMessagesRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode deleteBusinessMessages#17fff9a9: %w", err)
	}
	return d.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (d *DeleteBusinessMessagesRequest) DecodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode deleteBusinessMessages#17fff9a9 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode deleteBusinessMessages#17fff9a9: field business_connection_id: %w", err)
		}
		d.BusinessConnectionID = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode deleteBusinessMessages#17fff9a9: field message_ids: %w", err)
		}

		if headerLen > 0 {
			d.MessageIDs = make([]int64, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode deleteBusinessMessages#17fff9a9: field message_ids: %w", err)
			}
			d.MessageIDs = append(d.MessageIDs, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (d *DeleteBusinessMessagesRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if d == nil {
		return fmt.Errorf("can't encode deleteBusinessMessages#17fff9a9 as nil")
	}
	b.ObjStart()
	b.PutID("deleteBusinessMessages")
	b.Comma()
	b.FieldStart("business_connection_id")
	b.PutString(d.BusinessConnectionID)
	b.Comma()
	b.FieldStart("message_ids")
	b.ArrStart()
	for _, v := range d.MessageIDs {
		b.PutInt53(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (d *DeleteBusinessMessagesRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if d == nil {
		return fmt.Errorf("can't decode deleteBusinessMessages#17fff9a9 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("deleteBusinessMessages"); err != nil {
				return fmt.Errorf("unable to decode deleteBusinessMessages#17fff9a9: %w", err)
			}
		case "business_connection_id":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode deleteBusinessMessages#17fff9a9: field business_connection_id: %w", err)
			}
			d.BusinessConnectionID = value
		case "message_ids":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int53()
				if err != nil {
					return fmt.Errorf("unable to decode deleteBusinessMessages#17fff9a9: field message_ids: %w", err)
				}
				d.MessageIDs = append(d.MessageIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode deleteBusinessMessages#17fff9a9: field message_ids: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetBusinessConnectionID returns value of BusinessConnectionID field.
func (d *DeleteBusinessMessagesRequest) GetBusinessConnectionID() (value string) {
	if d == nil {
		return
	}
	return d.BusinessConnectionID
}

// GetMessageIDs returns value of MessageIDs field.
func (d *DeleteBusinessMessagesRequest) GetMessageIDs() (value []int64) {
	if d == nil {
		return
	}
	return d.MessageIDs
}

// DeleteBusinessMessages invokes method deleteBusinessMessages#17fff9a9 returning error if any.
func (c *Client) DeleteBusinessMessages(ctx context.Context, request *DeleteBusinessMessagesRequest) error {
	var ok Ok

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