// Code generated by itergen, DO NOT EDIT.

package dialogs

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 {
	OffsetDate int
	OffsetID   int
	OffsetPeer tg.InputPeerClass
	Limit      int
}

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

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

// Query implements Query interface.
func (q QueryFunc) Query(ctx context.Context, req Request) (tg.MessagesDialogsClass, 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}
}

// GetDialogsQueryBuilder is query builder of MessagesGetDialogs.
type GetDialogsQueryBuilder struct {
	raw        *tg.Client
	req        tg.MessagesGetDialogsRequest
	batchSize  int
	offsetDate int
	offsetID   int
	offsetPeer tg.InputPeerClass
}

// GetDialogs creates query builder of MessagesGetDialogs.
func (q *QueryBuilder) GetDialogs() *GetDialogsQueryBuilder {
	b := &GetDialogsQueryBuilder{
		raw:       q.raw,
		batchSize: 1,
		req:       tg.MessagesGetDialogsRequest{},
	}

	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 *GetDialogsQueryBuilder) BatchSize(batchSize int) *GetDialogsQueryBuilder {
	b.batchSize = batchSize
	return b
}

// OffsetDate sets offsetDate from which iterate start.
func (b *GetDialogsQueryBuilder) OffsetDate(offsetDate int) *GetDialogsQueryBuilder {
	b.offsetDate = offsetDate
	return b
}

// OffsetID sets offsetID from which iterate start.
func (b *GetDialogsQueryBuilder) OffsetID(offsetID int) *GetDialogsQueryBuilder {
	b.offsetID = offsetID
	return b
}

// FolderID sets FolderID field of GetDialogs query.
func (b *GetDialogsQueryBuilder) FolderID(paramFolderID int) *GetDialogsQueryBuilder {
	b.req.FolderID = paramFolderID
	return b
}

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

	r.FolderID = b.req.FolderID
	r.OffsetDate = req.OffsetDate
	r.OffsetID = req.OffsetID
	r.OffsetPeer = req.OffsetPeer
	return b.raw.MessagesGetDialogs(ctx, r)
}

// Iter returns iterator using built query.
func (b *GetDialogsQueryBuilder) Iter() *Iterator {
	iter := NewIterator(b, b.batchSize)
	iter = iter.OffsetDate(b.offsetDate)
	iter = iter.OffsetID(b.offsetID)
	return iter
}

// ForEach calls given callback on each iterator element.
func (b *GetDialogsQueryBuilder) 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 *GetDialogsQueryBuilder) 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 *GetDialogsQueryBuilder) 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()
}
