// Code generated by sdkgen. DO NOT EDIT.

// nolint
package mcpgateway

import (
	"context"

	"google.golang.org/grpc"

	"github.com/yandex-cloud/go-genproto/yandex/cloud/access"
	"github.com/yandex-cloud/go-genproto/yandex/cloud/operation"
	mcpgateway "github.com/yandex-cloud/go-genproto/yandex/cloud/serverless/mcpgateway/v1"
)

//revive:disable

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

// Create implements mcpgateway.McpGatewayServiceClient
func (c *McpGatewayServiceClient) Create(ctx context.Context, in *mcpgateway.CreateMcpGatewayRequest, opts ...grpc.CallOption) (*operation.Operation, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return mcpgateway.NewMcpGatewayServiceClient(conn).Create(ctx, in, opts...)
}

// Delete implements mcpgateway.McpGatewayServiceClient
func (c *McpGatewayServiceClient) Delete(ctx context.Context, in *mcpgateway.DeleteMcpGatewayRequest, opts ...grpc.CallOption) (*operation.Operation, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return mcpgateway.NewMcpGatewayServiceClient(conn).Delete(ctx, in, opts...)
}

// Get implements mcpgateway.McpGatewayServiceClient
func (c *McpGatewayServiceClient) Get(ctx context.Context, in *mcpgateway.GetMcpGatewayRequest, opts ...grpc.CallOption) (*mcpgateway.McpGateway, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return mcpgateway.NewMcpGatewayServiceClient(conn).Get(ctx, in, opts...)
}

// List implements mcpgateway.McpGatewayServiceClient
func (c *McpGatewayServiceClient) List(ctx context.Context, in *mcpgateway.ListMcpGatewayRequest, opts ...grpc.CallOption) (*mcpgateway.ListMcpGatewayResponse, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return mcpgateway.NewMcpGatewayServiceClient(conn).List(ctx, in, opts...)
}

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

	err           error
	started       bool
	requestedSize int64
	pageSize      int64

	client  *McpGatewayServiceClient
	request *mcpgateway.ListMcpGatewayRequest

	items []*mcpgateway.McpGatewayPreview
}

func (c *McpGatewayServiceClient) McpGatewayIterator(ctx context.Context, req *mcpgateway.ListMcpGatewayRequest, opts ...grpc.CallOption) *McpGatewayIterator {
	var pageSize int64
	const defaultPageSize = 1000
	pageSize = req.PageSize
	if pageSize == 0 {
		pageSize = defaultPageSize
	}
	return &McpGatewayIterator{
		ctx:      ctx,
		opts:     opts,
		client:   c,
		request:  req,
		pageSize: pageSize,
	}
}

func (it *McpGatewayIterator) 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.List(it.ctx, it.request, it.opts...)
	it.err = err
	if err != nil {
		return false
	}

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

func (it *McpGatewayIterator) Take(size int64) ([]*mcpgateway.McpGatewayPreview, 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 []*mcpgateway.McpGatewayPreview

	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 *McpGatewayIterator) TakeAll() ([]*mcpgateway.McpGatewayPreview, error) {
	return it.Take(0)
}

func (it *McpGatewayIterator) Value() *mcpgateway.McpGatewayPreview {
	if len(it.items) == 0 {
		panic("calling Value on empty iterator")
	}
	return it.items[0]
}

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

// ListAccessBindings implements mcpgateway.McpGatewayServiceClient
func (c *McpGatewayServiceClient) ListAccessBindings(ctx context.Context, in *access.ListAccessBindingsRequest, opts ...grpc.CallOption) (*access.ListAccessBindingsResponse, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return mcpgateway.NewMcpGatewayServiceClient(conn).ListAccessBindings(ctx, in, opts...)
}

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

	err           error
	started       bool
	requestedSize int64
	pageSize      int64

	client  *McpGatewayServiceClient
	request *access.ListAccessBindingsRequest

	items []*access.AccessBinding
}

func (c *McpGatewayServiceClient) McpGatewayAccessBindingsIterator(ctx context.Context, req *access.ListAccessBindingsRequest, opts ...grpc.CallOption) *McpGatewayAccessBindingsIterator {
	var pageSize int64
	const defaultPageSize = 1000
	pageSize = req.PageSize
	if pageSize == 0 {
		pageSize = defaultPageSize
	}
	return &McpGatewayAccessBindingsIterator{
		ctx:      ctx,
		opts:     opts,
		client:   c,
		request:  req,
		pageSize: pageSize,
	}
}

func (it *McpGatewayAccessBindingsIterator) 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.ListAccessBindings(it.ctx, it.request, it.opts...)
	it.err = err
	if err != nil {
		return false
	}

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

func (it *McpGatewayAccessBindingsIterator) Take(size int64) ([]*access.AccessBinding, 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 []*access.AccessBinding

	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 *McpGatewayAccessBindingsIterator) TakeAll() ([]*access.AccessBinding, error) {
	return it.Take(0)
}

func (it *McpGatewayAccessBindingsIterator) Value() *access.AccessBinding {
	if len(it.items) == 0 {
		panic("calling Value on empty iterator")
	}
	return it.items[0]
}

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

// ListOperations implements mcpgateway.McpGatewayServiceClient
func (c *McpGatewayServiceClient) ListOperations(ctx context.Context, in *mcpgateway.ListOperationsRequest, opts ...grpc.CallOption) (*mcpgateway.ListOperationsResponse, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return mcpgateway.NewMcpGatewayServiceClient(conn).ListOperations(ctx, in, opts...)
}

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

	err           error
	started       bool
	requestedSize int64
	pageSize      int64

	client  *McpGatewayServiceClient
	request *mcpgateway.ListOperationsRequest

	items []*operation.Operation
}

func (c *McpGatewayServiceClient) McpGatewayOperationsIterator(ctx context.Context, req *mcpgateway.ListOperationsRequest, opts ...grpc.CallOption) *McpGatewayOperationsIterator {
	var pageSize int64
	const defaultPageSize = 1000
	pageSize = req.PageSize
	if pageSize == 0 {
		pageSize = defaultPageSize
	}
	return &McpGatewayOperationsIterator{
		ctx:      ctx,
		opts:     opts,
		client:   c,
		request:  req,
		pageSize: pageSize,
	}
}

func (it *McpGatewayOperationsIterator) 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.ListOperations(it.ctx, it.request, it.opts...)
	it.err = err
	if err != nil {
		return false
	}

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

func (it *McpGatewayOperationsIterator) Take(size int64) ([]*operation.Operation, 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 []*operation.Operation

	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 *McpGatewayOperationsIterator) TakeAll() ([]*operation.Operation, error) {
	return it.Take(0)
}

func (it *McpGatewayOperationsIterator) Value() *operation.Operation {
	if len(it.items) == 0 {
		panic("calling Value on empty iterator")
	}
	return it.items[0]
}

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

// SetAccessBindings implements mcpgateway.McpGatewayServiceClient
func (c *McpGatewayServiceClient) SetAccessBindings(ctx context.Context, in *access.SetAccessBindingsRequest, opts ...grpc.CallOption) (*operation.Operation, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return mcpgateway.NewMcpGatewayServiceClient(conn).SetAccessBindings(ctx, in, opts...)
}

// Update implements mcpgateway.McpGatewayServiceClient
func (c *McpGatewayServiceClient) Update(ctx context.Context, in *mcpgateway.UpdateMcpGatewayRequest, opts ...grpc.CallOption) (*operation.Operation, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return mcpgateway.NewMcpGatewayServiceClient(conn).Update(ctx, in, opts...)
}

// UpdateAccessBindings implements mcpgateway.McpGatewayServiceClient
func (c *McpGatewayServiceClient) UpdateAccessBindings(ctx context.Context, in *access.UpdateAccessBindingsRequest, opts ...grpc.CallOption) (*operation.Operation, error) {
	conn, err := c.getConn(ctx)
	if err != nil {
		return nil, err
	}
	return mcpgateway.NewMcpGatewayServiceClient(conn).UpdateAccessBindings(ctx, in, opts...)
}
