package s3shared

import (
	
	

	

	
)

// ARNLookup is the initial middleware that looks up if an arn is provided.
// This middleware is responsible for fetching ARN from a arnable field, and registering the ARN on
// middleware context. This middleware must be executed before input validation step or any other
// arn processing middleware.
type ARNLookup struct {

	// GetARNValue takes in a input interface and returns a ptr to string and a bool
	GetARNValue func(interface{}) (*string, bool)
}

// ID for the middleware
func ( *ARNLookup) () string {
	return "S3Shared:ARNLookup"
}

// HandleInitialize handles the behavior of this initialize step
func ( *ARNLookup) ( context.Context,  middleware.InitializeInput,  middleware.InitializeHandler) (
	 middleware.InitializeOutput,  middleware.Metadata,  error,
) {
	// check if GetARNValue is supported
	if .GetARNValue == nil {
		return .HandleInitialize(, )
	}

	// check is input resource is an ARN; if not go to next
	,  := .GetARNValue(.Parameters)
	if ! ||  == nil || !arn.IsARN(*) {
		return .HandleInitialize(, )
	}

	// if ARN process ResourceRequest and put it on ctx
	,  := arn.Parse(*)
	if  != nil {
		return , , fmt.Errorf("error parsing arn: %w", )
	}
	// set parsed arn on context
	 = setARNResourceOnContext(, )

	return .HandleInitialize(, )
}

// arnResourceKey is the key set on context used to identify, retrive an ARN resource
// if present on the context.
type arnResourceKey struct{}

// SetARNResourceOnContext sets the S3 ARN on the context.
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func ( context.Context,  arn.ARN) context.Context {
	return middleware.WithStackValue(, arnResourceKey{}, )
}

// GetARNResourceFromContext returns an ARN from context and a bool indicating
// presence of ARN on ctx.
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func ( context.Context) (arn.ARN, bool) {
	,  := middleware.GetStackValue(, arnResourceKey{}).(arn.ARN)
	return , 
}