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

// StoriesGetPeerMaxIDsRequest represents TL type `stories.getPeerMaxIDs#78499170`.
// Get the IDs of the maximum read stories for a set of peers.
//
// See https://core.telegram.org/method/stories.getPeerMaxIDs for reference.
type StoriesGetPeerMaxIDsRequest struct {
	// Peers
	ID []InputPeerClass
}

// StoriesGetPeerMaxIDsRequestTypeID is TL type id of StoriesGetPeerMaxIDsRequest.
const StoriesGetPeerMaxIDsRequestTypeID = 0x78499170

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

func (g *StoriesGetPeerMaxIDsRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.ID == nil) {
		return false
	}

	return true
}

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

// FillFrom fills StoriesGetPeerMaxIDsRequest from given interface.
func (g *StoriesGetPeerMaxIDsRequest) FillFrom(from interface {
	GetID() (value []InputPeerClass)
}) {
	g.ID = from.GetID()
}

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

// TypeName returns name of type in TL schema.
func (*StoriesGetPeerMaxIDsRequest) TypeName() string {
	return "stories.getPeerMaxIDs"
}

// TypeInfo returns info about TL type.
func (g *StoriesGetPeerMaxIDsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "stories.getPeerMaxIDs",
		ID:   StoriesGetPeerMaxIDsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ID",
			SchemaName: "id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *StoriesGetPeerMaxIDsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode stories.getPeerMaxIDs#78499170 as nil")
	}
	b.PutID(StoriesGetPeerMaxIDsRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *StoriesGetPeerMaxIDsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode stories.getPeerMaxIDs#78499170 as nil")
	}
	b.PutVectorHeader(len(g.ID))
	for idx, v := range g.ID {
		if v == nil {
			return fmt.Errorf("unable to encode stories.getPeerMaxIDs#78499170: field id element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode stories.getPeerMaxIDs#78499170: field id element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (g *StoriesGetPeerMaxIDsRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode stories.getPeerMaxIDs#78499170 to nil")
	}
	if err := b.ConsumeID(StoriesGetPeerMaxIDsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode stories.getPeerMaxIDs#78499170: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *StoriesGetPeerMaxIDsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode stories.getPeerMaxIDs#78499170 to nil")
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode stories.getPeerMaxIDs#78499170: field id: %w", err)
		}

		if headerLen > 0 {
			g.ID = make([]InputPeerClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeInputPeer(b)
			if err != nil {
				return fmt.Errorf("unable to decode stories.getPeerMaxIDs#78499170: field id: %w", err)
			}
			g.ID = append(g.ID, value)
		}
	}
	return nil
}

// GetID returns value of ID field.
func (g *StoriesGetPeerMaxIDsRequest) GetID() (value []InputPeerClass) {
	if g == nil {
		return
	}
	return g.ID
}

// MapID returns field ID wrapped in InputPeerClassArray helper.
func (g *StoriesGetPeerMaxIDsRequest) MapID() (value InputPeerClassArray) {
	return InputPeerClassArray(g.ID)
}

// StoriesGetPeerMaxIDs invokes method stories.getPeerMaxIDs#78499170 returning error if any.
// Get the IDs of the maximum read stories for a set of peers.
//
// See https://core.telegram.org/method/stories.getPeerMaxIDs for reference.
func (c *Client) StoriesGetPeerMaxIDs(ctx context.Context, id []InputPeerClass) ([]RecentStory, error) {
	var result RecentStoryVector

	request := &StoriesGetPeerMaxIDsRequest{
		ID: id,
	}
	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return []RecentStory(result.Elems), nil
}
