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

package acm

import (
	"context"
	"fmt"
	"github.com/aws/aws-sdk-go-v2/service/acm/types"
	"github.com/aws/smithy-go/middleware"
	"github.com/aws/smithy-go/ptr"
)

// Retrieves a list of external account bindings for the specified ACME endpoint.
func (c *Client) ListAcmeExternalAccountBindings(ctx context.Context, params *ListAcmeExternalAccountBindingsInput, optFns ...func(*Options)) (*ListAcmeExternalAccountBindingsOutput, error) {
	if params == nil {
		params = &ListAcmeExternalAccountBindingsInput{}
	}

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

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

type ListAcmeExternalAccountBindingsInput struct {

	// The Amazon Resource Name (ARN) of the ACME endpoint.
	//
	// This member is required.
	AcmeEndpointArn *string

	// The maximum number of results to return.
	MaxResults *int32

	// A token for pagination.
	NextToken *string

	noSmithyDocumentSerde
}

func (in *ListAcmeExternalAccountBindingsInput) bindEndpointParams(p *EndpointParameters) {

	p.ServiceType = ptr.String("ACM-ACME")
}

type ListAcmeExternalAccountBindingsOutput struct {

	// The list of external account bindings.
	ExternalAccountBindings []types.AcmeExternalAccountBindingSummary

	// A token for pagination.
	NextToken *string

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

	noSmithyDocumentSerde
}

func (c *Client) addOperationListAcmeExternalAccountBindingsMiddlewares(stack *middleware.Stack, options Options) (err error) {
	err = stack.Serialize.Add(&awsAwsjson11_serializeOpListAcmeExternalAccountBindings{}, middleware.After)
	if err != nil {
		return err
	}
	err = stack.Deserialize.Add(&awsAwsjson11_deserializeOpListAcmeExternalAccountBindings{}, middleware.After)
	if err != nil {
		return err
	}

	if err = addComputeContentLength(stack); err != nil {
		return err
	}
	if err = addResolveEndpointMiddleware(stack, options); err != nil {
		return err
	}
	if err = addComputePayloadSHA256(stack); err != nil {
		return err
	}
	if err = addRecordResponseTiming(stack, options); err != nil {
		return err
	}
	if err = addCredentialSource(stack, options); err != nil {
		return err
	}
	if err = addOpListAcmeExternalAccountBindingsValidationMiddleware(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
	}
	if err = addInterceptors(stack, options); err != nil {
		return err
	}
	return nil
}

// ListAcmeExternalAccountBindingsPaginatorOptions is the paginator options for
// ListAcmeExternalAccountBindings
type ListAcmeExternalAccountBindingsPaginatorOptions struct {
	// The maximum number of results to return.
	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
}

// ListAcmeExternalAccountBindingsPaginator is a paginator for
// ListAcmeExternalAccountBindings
type ListAcmeExternalAccountBindingsPaginator struct {
	options   ListAcmeExternalAccountBindingsPaginatorOptions
	client    ListAcmeExternalAccountBindingsAPIClient
	params    *ListAcmeExternalAccountBindingsInput
	nextToken *string
	firstPage bool
}

// NewListAcmeExternalAccountBindingsPaginator returns a new
// ListAcmeExternalAccountBindingsPaginator
func NewListAcmeExternalAccountBindingsPaginator(client ListAcmeExternalAccountBindingsAPIClient, params *ListAcmeExternalAccountBindingsInput, optFns ...func(*ListAcmeExternalAccountBindingsPaginatorOptions)) *ListAcmeExternalAccountBindingsPaginator {
	if params == nil {
		params = &ListAcmeExternalAccountBindingsInput{}
	}

	options := ListAcmeExternalAccountBindingsPaginatorOptions{}
	if params.MaxResults != nil {
		options.Limit = *params.MaxResults
	}

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

	return &ListAcmeExternalAccountBindingsPaginator{
		options:   options,
		client:    client,
		params:    params,
		firstPage: true,
		nextToken: params.NextToken,
	}
}

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

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

	params := *p.params
	params.NextToken = p.nextToken

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

	optFns = append([]func(*Options){
		addIsPaginatorUserAgent,
	}, optFns...)
	result, err := p.client.ListAcmeExternalAccountBindings(ctx, &params, optFns...)
	if err != nil {
		return nil, err
	}
	p.firstPage = false

	prevToken := p.nextToken
	p.nextToken = result.NextToken

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

	return result, nil
}

// ListAcmeExternalAccountBindingsAPIClient is a client that implements the
// ListAcmeExternalAccountBindings operation.
type ListAcmeExternalAccountBindingsAPIClient interface {
	ListAcmeExternalAccountBindings(context.Context, *ListAcmeExternalAccountBindingsInput, ...func(*Options)) (*ListAcmeExternalAccountBindingsOutput, error)
}

var _ ListAcmeExternalAccountBindingsAPIClient = (*Client)(nil)
