// Code generated by smithy-go-codegen DO NOT EDIT.

package cloudfront

import (
	"context"
	"fmt"
	awsmiddleware "github.com/aws/aws-sdk-go-v2/aws/middleware"
	"github.com/aws/aws-sdk-go-v2/aws/signer/v4"
	"github.com/aws/aws-sdk-go-v2/service/cloudfront/types"
	"github.com/aws/smithy-go/middleware"
	smithyhttp "github.com/aws/smithy-go/transport/http"
)

// Specifies the Key Value Stores to list.
func (c *Client) ListKeyValueStores(ctx context.Context, params *ListKeyValueStoresInput, optFns ...func(*Options)) (*ListKeyValueStoresOutput, error) {
	if params == nil {
		params = &ListKeyValueStoresInput{}
	}

	result, metadata, err := c.invokeOperation(ctx, "ListKeyValueStores", params, optFns, c.addOperationListKeyValueStoresMiddlewares)
	if err != nil {
		return nil, err
	}

	out := result.(*ListKeyValueStoresOutput)
	out.ResultMetadata = metadata
	return out, nil
}

type ListKeyValueStoresInput struct {

	// The marker associated with the Key Value Stores list.
	Marker *string

	// The maximum number of items in the Key Value Stores list.
	MaxItems *int32

	// The status of the request for the Key Value Stores list.
	Status *string

	noSmithyDocumentSerde
}

type ListKeyValueStoresOutput struct {

	// The resulting Key Value Stores list.
	KeyValueStoreList *types.KeyValueStoreList

	// Metadata pertaining to the operation's result.
	ResultMetadata middleware.Metadata

	noSmithyDocumentSerde
}

func (c *Client) addOperationListKeyValueStoresMiddlewares(stack *middleware.Stack, options Options) (err error) {
	if err := stack.Serialize.Add(&setOperationInputMiddleware{}, middleware.After); err != nil {
		return err
	}
	err = stack.Serialize.Add(&awsRestxml_serializeOpListKeyValueStores{}, middleware.After)
	if err != nil {
		return err
	}
	err = stack.Deserialize.Add(&awsRestxml_deserializeOpListKeyValueStores{}, middleware.After)
	if err != nil {
		return err
	}
	if err := addProtocolFinalizerMiddlewares(stack, options, "ListKeyValueStores"); err != nil {
		return fmt.Errorf("add protocol finalizers: %v", err)
	}

	if err = addlegacyEndpointContextSetter(stack, options); err != nil {
		return err
	}
	if err = addSetLoggerMiddleware(stack, options); err != nil {
		return err
	}
	if err = awsmiddleware.AddClientRequestIDMiddleware(stack); err != nil {
		return err
	}
	if err = smithyhttp.AddComputeContentLengthMiddleware(stack); err != nil {
		return err
	}
	if err = addResolveEndpointMiddleware(stack, options); err != nil {
		return err
	}
	if err = v4.AddComputePayloadSHA256Middleware(stack); err != nil {
		return err
	}
	if err = addRetryMiddlewares(stack, options); err != nil {
		return err
	}
	if err = awsmiddleware.AddRawResponseToMetadata(stack); err != nil {
		return err
	}
	if err = awsmiddleware.AddRecordResponseTiming(stack); err != nil {
		return err
	}
	if err = addClientUserAgent(stack, options); err != nil {
		return err
	}
	if err = smithyhttp.AddErrorCloseResponseBodyMiddleware(stack); err != nil {
		return err
	}
	if err = smithyhttp.AddCloseResponseBodyMiddleware(stack); err != nil {
		return err
	}
	if err = addSetLegacyContextSigningOptionsMiddleware(stack); err != nil {
		return err
	}
	if err = stack.Initialize.Add(newServiceMetadataMiddleware_opListKeyValueStores(options.Region), middleware.Before); err != nil {
		return err
	}
	if err = awsmiddleware.AddRecursionDetection(stack); err != nil {
		return err
	}
	if err = addRequestIDRetrieverMiddleware(stack); err != nil {
		return err
	}
	if err = addResponseErrorMiddleware(stack); err != nil {
		return err
	}
	if err = addRequestResponseLogging(stack, options); err != nil {
		return err
	}
	if err = addDisableHTTPSMiddleware(stack, options); err != nil {
		return err
	}
	return nil
}

// ListKeyValueStoresAPIClient is a client that implements the ListKeyValueStores
// operation.
type ListKeyValueStoresAPIClient interface {
	ListKeyValueStores(context.Context, *ListKeyValueStoresInput, ...func(*Options)) (*ListKeyValueStoresOutput, error)
}

var _ ListKeyValueStoresAPIClient = (*Client)(nil)

// ListKeyValueStoresPaginatorOptions is the paginator options for
// ListKeyValueStores
type ListKeyValueStoresPaginatorOptions struct {
	// The maximum number of items in the Key Value Stores list.
	Limit int32

	// Set to true if pagination should stop if the service returns a pagination token
	// that matches the most recent token provided to the service.
	StopOnDuplicateToken bool
}

// ListKeyValueStoresPaginator is a paginator for ListKeyValueStores
type ListKeyValueStoresPaginator struct {
	options   ListKeyValueStoresPaginatorOptions
	client    ListKeyValueStoresAPIClient
	params    *ListKeyValueStoresInput
	nextToken *string
	firstPage bool
}

// NewListKeyValueStoresPaginator returns a new ListKeyValueStoresPaginator
func NewListKeyValueStoresPaginator(client ListKeyValueStoresAPIClient, params *ListKeyValueStoresInput, optFns ...func(*ListKeyValueStoresPaginatorOptions)) *ListKeyValueStoresPaginator {
	if params == nil {
		params = &ListKeyValueStoresInput{}
	}

	options := ListKeyValueStoresPaginatorOptions{}
	if params.MaxItems != nil {
		options.Limit = *params.MaxItems
	}

	for _, fn := range optFns {
		fn(&options)
	}

	return &ListKeyValueStoresPaginator{
		options:   options,
		client:    client,
		params:    params,
		firstPage: true,
		nextToken: params.Marker,
	}
}

// HasMorePages returns a boolean indicating whether more pages are available
func (p *ListKeyValueStoresPaginator) HasMorePages() bool {
	return p.firstPage || (p.nextToken != nil && len(*p.nextToken) != 0)
}

// NextPage retrieves the next ListKeyValueStores page.
func (p *ListKeyValueStoresPaginator) NextPage(ctx context.Context, optFns ...func(*Options)) (*ListKeyValueStoresOutput, error) {
	if !p.HasMorePages() {
		return nil, fmt.Errorf("no more pages available")
	}

	params := *p.params
	params.Marker = p.nextToken

	var limit *int32
	if p.options.Limit > 0 {
		limit = &p.options.Limit
	}
	params.MaxItems = limit

	result, err := p.client.ListKeyValueStores(ctx, &params, optFns...)
	if err != nil {
		return nil, err
	}
	p.firstPage = false

	prevToken := p.nextToken
	p.nextToken = nil
	if result.KeyValueStoreList != nil {
		p.nextToken = result.KeyValueStoreList.NextMarker
	}

	if p.options.StopOnDuplicateToken &&
		prevToken != nil &&
		p.nextToken != nil &&
		*prevToken == *p.nextToken {
		p.nextToken = nil
	}

	return result, nil
}

func newServiceMetadataMiddleware_opListKeyValueStores(region string) *awsmiddleware.RegisterServiceMetadata {
	return &awsmiddleware.RegisterServiceMetadata{
		Region:        region,
		ServiceID:     ServiceID,
		OperationName: "ListKeyValueStores",
	}
}
