// Code generated by sdkgen. DO NOT EDIT.

// nolint
package organizationmanager

import (
	"context"

	"google.golang.org/grpc"

	"github.com/yandex-cloud/go-genproto/yandex/cloud/operation"
	organizationmanager "github.com/yandex-cloud/go-genproto/yandex/cloud/organizationmanager/v1"
)

//revive:disable

// GroupMappingServiceClient is a organizationmanager.GroupMappingServiceClient with
// lazy GRPC connection initialization.
type GroupMappingServiceClient struct {
	getConn func(ctx context.Context) (*grpc.ClientConn, error)
}

// Create implements organizationmanager.GroupMappingServiceClient
func (c *GroupMappingServiceClient) Create(ctx context.Context, in *organizationmanager.CreateGroupMappingRequest, opts ...grpc.CallOption) (*operation.Operation, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return organizationmanager.NewGroupMappingServiceClient(conn).Create(ctx, in, opts...)
}

// Delete implements organizationmanager.GroupMappingServiceClient
func (c *GroupMappingServiceClient) Delete(ctx context.Context, in *organizationmanager.DeleteGroupMappingRequest, opts ...grpc.CallOption) (*operation.Operation, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return organizationmanager.NewGroupMappingServiceClient(conn).Delete(ctx, in, opts...)
}

// Get implements organizationmanager.GroupMappingServiceClient
func (c *GroupMappingServiceClient) Get(ctx context.Context, in *organizationmanager.GetGroupMappingRequest, opts ...grpc.CallOption) (*organizationmanager.GetGroupMappingResponse, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return organizationmanager.NewGroupMappingServiceClient(conn).Get(ctx, in, opts...)
}

// ListItems implements organizationmanager.GroupMappingServiceClient
func (c *GroupMappingServiceClient) ListItems(ctx context.Context, in *organizationmanager.ListGroupMappingItemsRequest, opts ...grpc.CallOption) (*organizationmanager.ListGroupMappingItemsResponse, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return organizationmanager.NewGroupMappingServiceClient(conn).ListItems(ctx, in, opts...)
}

type GroupMappingItemsIterator struct {
	ctx  context.Context
	opts []grpc.CallOption

	err           error
	started       bool
	requestedSize int64
	pageSize      int64

	client  *GroupMappingServiceClient
	request *organizationmanager.ListGroupMappingItemsRequest

	items []*organizationmanager.GroupMappingItem
}

func (c *GroupMappingServiceClient) GroupMappingItemsIterator(ctx context.Context, req *organizationmanager.ListGroupMappingItemsRequest, opts ...grpc.CallOption) *GroupMappingItemsIterator {
	var pageSize int64
	const defaultPageSize = 1000
	pageSize = req.PageSize
	if pageSize == 0 {
		pageSize = defaultPageSize
	}
	return &GroupMappingItemsIterator{
		ctx:      ctx,
		opts:     opts,
		client:   c,
		request:  req,
		pageSize: pageSize,
	}
}

func (it *GroupMappingItemsIterator) Next() bool {
	if it.err != nil {
		return false
	}
	if len(it.items) > 1 {
		it.items[0] = nil
		it.items = it.items[1:]
		return true
	}
	it.items = nil // consume last item, if any

	if it.started && it.request.PageToken == "" {
		return false
	}
	it.started = true

	if it.requestedSize == 0 || it.requestedSize > it.pageSize {
		it.request.PageSize = it.pageSize
	} else {
		it.request.PageSize = it.requestedSize
	}

	response, err := it.client.ListItems(it.ctx, it.request, it.opts...)
	it.err = err
	if err != nil {
		return false
	}

	it.items = response.GroupMappingItems
	it.request.PageToken = response.NextPageToken
	return len(it.items) > 0
}

func (it *GroupMappingItemsIterator) Take(size int64) ([]*organizationmanager.GroupMappingItem, error) {
	if it.err != nil {
		return nil, it.err
	}

	if size == 0 {
		size = 1 << 32 // something insanely large
	}
	it.requestedSize = size
	defer func() {
		// reset iterator for future calls.
		it.requestedSize = 0
	}()

	var result []*organizationmanager.GroupMappingItem

	for it.requestedSize > 0 && it.Next() {
		it.requestedSize--
		result = append(result, it.Value())
	}

	if it.err != nil {
		return nil, it.err
	}

	return result, nil
}

func (it *GroupMappingItemsIterator) TakeAll() ([]*organizationmanager.GroupMappingItem, error) {
	return it.Take(0)
}

func (it *GroupMappingItemsIterator) Value() *organizationmanager.GroupMappingItem {
	if len(it.items) == 0 {
		panic("calling Value on empty iterator")
	}
	return it.items[0]
}

func (it *GroupMappingItemsIterator) Error() error {
	return it.err
}

// Update implements organizationmanager.GroupMappingServiceClient
func (c *GroupMappingServiceClient) Update(ctx context.Context, in *organizationmanager.UpdateGroupMappingRequest, opts ...grpc.CallOption) (*operation.Operation, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return organizationmanager.NewGroupMappingServiceClient(conn).Update(ctx, in, opts...)
}

// UpdateItems implements organizationmanager.GroupMappingServiceClient
func (c *GroupMappingServiceClient) UpdateItems(ctx context.Context, in *organizationmanager.UpdateGroupMappingItemsRequest, opts ...grpc.CallOption) (*operation.Operation, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return organizationmanager.NewGroupMappingServiceClient(conn).UpdateItems(ctx, in, opts...)
}
