package middleware

import 

// InitializeInput wraps the input parameters for the InitializeMiddlewares to
// consume. InitializeMiddleware may modify the parameter value before
// forwarding it along to the next InitializeHandler.
type InitializeInput struct {
	Parameters interface{}
}

// InitializeOutput provides the result returned by the next InitializeHandler.
type InitializeOutput struct {
	Result interface{}
}

// InitializeHandler provides the interface for the next handler the
// InitializeMiddleware will call in the middleware chain.
type InitializeHandler interface {
	HandleInitialize(ctx context.Context, in InitializeInput) (
		out InitializeOutput, metadata Metadata, err error,
	)
}

// InitializeMiddleware provides the interface for middleware specific to the
// initialize step. Delegates to the next InitializeHandler for further
// processing.
type InitializeMiddleware interface {
	// ID returns a unique ID for the middleware in the InitializeStep. The step does not
	// allow duplicate IDs.
	ID() string

	// HandleInitialize invokes the middleware behavior which must delegate to the next handler
	// for the middleware chain to continue. The method must return a result or
	// error to its caller.
	HandleInitialize(ctx context.Context, in InitializeInput, next InitializeHandler) (
		out InitializeOutput, metadata Metadata, err error,
	)
}

// InitializeMiddlewareFunc returns a InitializeMiddleware with the unique ID provided,
// and the func to be invoked.
func ( string,  func(context.Context, InitializeInput, InitializeHandler) (InitializeOutput, Metadata, error)) InitializeMiddleware {
	return initializeMiddlewareFunc{
		id: ,
		fn: ,
	}
}

type initializeMiddlewareFunc struct {
	// Unique ID for the middleware.
	id string

	// Middleware function to be called.
	fn func(context.Context, InitializeInput, InitializeHandler) (
		InitializeOutput, Metadata, error,
	)
}

// ID returns the unique ID for the middleware.
func ( initializeMiddlewareFunc) () string { return .id }

// HandleInitialize invokes the middleware Fn.
func ( initializeMiddlewareFunc) ( context.Context,  InitializeInput,  InitializeHandler) (
	 InitializeOutput,  Metadata,  error,
) {
	return .fn(, , )
}

var _ InitializeMiddleware = (initializeMiddlewareFunc{})

// InitializeStep provides the ordered grouping of InitializeMiddleware to be
// invoked on a handler.
type InitializeStep struct {
	ids *orderedIDs
}

// NewInitializeStep returns an InitializeStep ready to have middleware for
// initialization added to it.
func () *InitializeStep {
	return &InitializeStep{
		ids: newOrderedIDs(),
	}
}

var _ Middleware = (*InitializeStep)(nil)

// ID returns the unique ID of the step as a middleware.
func ( *InitializeStep) () string {
	return "Initialize stack step"
}

// HandleMiddleware invokes the middleware by decorating the next handler
// provided. Returns the result of the middleware and handler being invoked.
//
// Implements Middleware interface.
func ( *InitializeStep) ( context.Context,  interface{},  Handler) (
	 interface{},  Metadata,  error,
) {
	 := .ids.GetOrder()

	var  InitializeHandler = initializeWrapHandler{Next: }
	for  := len() - 1;  >= 0; -- {
		 = decoratedInitializeHandler{
			Next: ,
			With: [].(InitializeMiddleware),
		}
	}

	 := InitializeInput{
		Parameters: ,
	}

	, ,  := .HandleInitialize(, )
	return .Result, , 
}

// Get retrieves the middleware identified by id. If the middleware is not present, returns false.
func ( *InitializeStep) ( string) (InitializeMiddleware, bool) {
	,  := .ids.Get()
	if ! {
		return nil, false
	}
	return .(InitializeMiddleware), 
}

// Add injects the middleware to the relative position of the middleware group.
// Returns an error if the middleware already exists.
func ( *InitializeStep) ( InitializeMiddleware,  RelativePosition) error {
	return .ids.Add(, )
}

// Insert injects the middleware relative to an existing middleware ID.
// Returns error if the original middleware does not exist, or the middleware
// being added already exists.
func ( *InitializeStep) ( InitializeMiddleware,  string,  RelativePosition) error {
	return .ids.Insert(, , )
}

// Swap removes the middleware by id, replacing it with the new middleware.
// Returns the middleware removed, or error if the middleware to be removed
// doesn't exist.
func ( *InitializeStep) ( string,  InitializeMiddleware) (InitializeMiddleware, error) {
	,  := .ids.Swap(, )
	if  != nil {
		return nil, 
	}

	return .(InitializeMiddleware), nil
}

// Remove removes the middleware by id. Returns error if the middleware
// doesn't exist.
func ( *InitializeStep) ( string) (InitializeMiddleware, error) {
	,  := .ids.Remove()
	if  != nil {
		return nil, 
	}

	return .(InitializeMiddleware), nil
}

// List returns a list of the middleware in the step.
func ( *InitializeStep) () []string {
	return .ids.List()
}

// Clear removes all middleware in the step.
func ( *InitializeStep) () {
	.ids.Clear()
}

type initializeWrapHandler struct {
	Next Handler
}

var _ InitializeHandler = (*initializeWrapHandler)(nil)

// HandleInitialize implements InitializeHandler, converts types and delegates to underlying
// generic handler.
func ( initializeWrapHandler) ( context.Context,  InitializeInput) (
	 InitializeOutput,  Metadata,  error,
) {
	, ,  := .Next.Handle(, .Parameters)
	return InitializeOutput{
		Result: ,
	}, , 
}

type decoratedInitializeHandler struct {
	Next InitializeHandler
	With InitializeMiddleware
}

var _ InitializeHandler = (*decoratedInitializeHandler)(nil)

func ( decoratedInitializeHandler) ( context.Context,  InitializeInput) (
	 InitializeOutput,  Metadata,  error,
) {
	return .With.HandleInitialize(, , .Next)
}

// InitializeHandlerFunc provides a wrapper around a function to be used as an initialize middleware handler.
type InitializeHandlerFunc func(context.Context, InitializeInput) (InitializeOutput, Metadata, error)

// HandleInitialize calls the wrapped function with the provided arguments.
func ( InitializeHandlerFunc) ( context.Context,  InitializeInput) (InitializeOutput, Metadata, error) {
	return (, )
}

var _ InitializeHandler = InitializeHandlerFunc(nil)