package middleware

import 

// FinalizeInput provides the input parameters for the FinalizeMiddleware to
// consume. FinalizeMiddleware may modify the Request value before forwarding
// the FinalizeInput along to the next next FinalizeHandler.
type FinalizeInput struct {
	Request interface{}
}

// FinalizeOutput provides the result returned by the next FinalizeHandler.
type FinalizeOutput struct {
	Result interface{}
}

// FinalizeHandler provides the interface for the next handler the
// FinalizeMiddleware will call in the middleware chain.
type FinalizeHandler interface {
	HandleFinalize(ctx context.Context, in FinalizeInput) (
		out FinalizeOutput, metadata Metadata, err error,
	)
}

// FinalizeMiddleware provides the interface for middleware specific to the
// serialize step. Delegates to the next FinalizeHandler for further
// processing.
type FinalizeMiddleware interface {
	// ID returns a unique ID for the middleware in the FinalizeStep. The step does not
	// allow duplicate IDs.
	ID() string

	// HandleFinalize 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.
	HandleFinalize(ctx context.Context, in FinalizeInput, next FinalizeHandler) (
		out FinalizeOutput, metadata Metadata, err error,
	)
}

// FinalizeMiddlewareFunc returns a FinalizeMiddleware with the unique ID
// provided, and the func to be invoked.
func ( string,  func(context.Context, FinalizeInput, FinalizeHandler) (FinalizeOutput, Metadata, error)) FinalizeMiddleware {
	return finalizeMiddlewareFunc{
		id: ,
		fn: ,
	}
}

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

	// Middleware function to be called.
	fn func(context.Context, FinalizeInput, FinalizeHandler) (
		FinalizeOutput, Metadata, error,
	)
}

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

// HandleFinalize invokes the middleware Fn.
func ( finalizeMiddlewareFunc) ( context.Context,  FinalizeInput,  FinalizeHandler) (
	 FinalizeOutput,  Metadata,  error,
) {
	return .fn(, , )
}

var _ FinalizeMiddleware = (finalizeMiddlewareFunc{})

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

// NewFinalizeStep returns a FinalizeStep ready to have middleware for
// initialization added to it.
func () *FinalizeStep {
	return &FinalizeStep{
		ids: newOrderedIDs(),
	}
}

var _ Middleware = (*FinalizeStep)(nil)

// ID returns the unique id of the step as a middleware.
func ( *FinalizeStep) () string {
	return "Finalize 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 ( *FinalizeStep) ( context.Context,  interface{},  Handler) (
	 interface{},  Metadata,  error,
) {
	 := .ids.GetOrder()

	var  FinalizeHandler = finalizeWrapHandler{Next: }
	for  := len() - 1;  >= 0; -- {
		 = decoratedFinalizeHandler{
			Next: ,
			With: [].(FinalizeMiddleware),
		}
	}

	 := FinalizeInput{
		Request: ,
	}

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

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

// Add injects the middleware to the relative position of the middleware group.
// Returns an error if the middleware already exists.
func ( *FinalizeStep) ( FinalizeMiddleware,  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 ( *FinalizeStep) ( FinalizeMiddleware,  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 ( *FinalizeStep) ( string,  FinalizeMiddleware) (FinalizeMiddleware, error) {
	,  := .ids.Swap(, )
	if  != nil {
		return nil, 
	}

	return .(FinalizeMiddleware), nil
}

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

	return .(FinalizeMiddleware), nil
}

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

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

type finalizeWrapHandler struct {
	Next Handler
}

var _ FinalizeHandler = (*finalizeWrapHandler)(nil)

// HandleFinalize implements FinalizeHandler, converts types and delegates to underlying
// generic handler.
func ( finalizeWrapHandler) ( context.Context,  FinalizeInput) (
	 FinalizeOutput,  Metadata,  error,
) {
	, ,  := .Next.Handle(, .Request)
	return FinalizeOutput{
		Result: ,
	}, , 
}

type decoratedFinalizeHandler struct {
	Next FinalizeHandler
	With FinalizeMiddleware
}

var _ FinalizeHandler = (*decoratedFinalizeHandler)(nil)

func ( decoratedFinalizeHandler) ( context.Context,  FinalizeInput) (
	 FinalizeOutput,  Metadata,  error,
) {
	return .With.HandleFinalize(, , .Next)
}

// FinalizeHandlerFunc provides a wrapper around a function to be used as a finalize middleware handler.
type FinalizeHandlerFunc func(context.Context, FinalizeInput) (FinalizeOutput, Metadata, error)

// HandleFinalize invokes the wrapped function with the given arguments.
func ( FinalizeHandlerFunc) ( context.Context,  FinalizeInput) (FinalizeOutput, Metadata, error) {
	return (, )
}

var _ FinalizeHandler = FinalizeHandlerFunc(nil)