// Code generated by itergen, DO NOT EDIT.

package featured

import (
	"context"

	"github.com/go-faster/errors"

	"go.mau.fi/mautrix-telegram/pkg/gotd/tg"
)

// No-op definition for keeping imports.
var _ = context.Background()

// Request is a parameter for Query.
type Request struct {
	Offset int
	Limit  int
}

// Query is an abstraction for featured request.
// NB: iterator mutates returned data (sorts, at least).
type Query interface {
	Query(ctx context.Context, req Request) (tg.MessagesFeaturedStickersClass, error)
}

// QueryFunc is a function adapter for Query.
type QueryFunc func(ctx context.Context, req Request) (tg.MessagesFeaturedStickersClass, error)

// Query implements Query interface.
func (q QueryFunc) Query(ctx context.Context, req Request) (tg.MessagesFeaturedStickersClass, error) {
	return q(ctx, req)
}

// QueryBuilder is a helper to create message queries.
type QueryBuilder struct {
	raw *tg.Client
}

// NewQueryBuilder creates new QueryBuilder.
func NewQueryBuilder(raw *tg.Client) *QueryBuilder {
	return &QueryBuilder{raw: raw}
}

// GetOldFeaturedStickersQueryBuilder is query builder of MessagesGetOldFeaturedStickers.
type GetOldFeaturedStickersQueryBuilder struct {
	raw       *tg.Client
	req       tg.MessagesGetOldFeaturedStickersRequest
	batchSize int
	offset    int
}

// GetOldFeaturedStickers creates query builder of MessagesGetOldFeaturedStickers.
func (q *QueryBuilder) GetOldFeaturedStickers() *GetOldFeaturedStickersQueryBuilder {
	b := &GetOldFeaturedStickersQueryBuilder{
		raw:       q.raw,
		batchSize: 1,
		req:       tg.MessagesGetOldFeaturedStickersRequest{},
	}

	return b
}

// BatchSize sets buffer of message loaded from one request.
// Be carefully, when set this limit, because Telegram does not return error if limit is too big,
// so results can be incorrect.
func (b *GetOldFeaturedStickersQueryBuilder) BatchSize(batchSize int) *GetOldFeaturedStickersQueryBuilder {
	b.batchSize = batchSize
	return b
}

// Query implements Query interface.
func (b *GetOldFeaturedStickersQueryBuilder) Query(ctx context.Context, req Request) (tg.MessagesFeaturedStickersClass, error) {
	r := &tg.MessagesGetOldFeaturedStickersRequest{
		Limit: req.Limit,
	}

	r.Offset = req.Offset
	return b.raw.MessagesGetOldFeaturedStickers(ctx, r)
}

// Iter returns iterator using built query.
func (b *GetOldFeaturedStickersQueryBuilder) Iter() *Iterator {
	iter := NewIterator(b, b.batchSize)
	iter = iter.Offset(b.offset)
	return iter
}

// ForEach calls given callback on each iterator element.
func (b *GetOldFeaturedStickersQueryBuilder) ForEach(ctx context.Context, cb func(context.Context, Elem) error) error {
	iter := b.Iter()
	for iter.Next(ctx) {
		if err := cb(ctx, iter.Value()); err != nil {
			return err
		}
	}
	return iter.Err()
}

// Count fetches remote state to get number of elements.
func (b *GetOldFeaturedStickersQueryBuilder) Count(ctx context.Context) (int, error) {
	iter := b.Iter()
	c, err := iter.Total(ctx)
	if err != nil {
		return 0, errors.Wrap(err, "get total")
	}
	return c, nil
}

// Collect creates iterator and collects all elements to slice.
func (b *GetOldFeaturedStickersQueryBuilder) Collect(ctx context.Context) ([]Elem, error) {
	iter := b.Iter()
	c, err := iter.Total(ctx)
	if err != nil {
		return nil, errors.Wrap(err, "get total")
	}

	r := make([]Elem, 0, c)
	for iter.Next(ctx) {
		r = append(r, iter.Value())
	}

	return r, iter.Err()
}
