package middleware

import 

// SerializeInput provides the input parameters for the SerializeMiddleware to
// consume. SerializeMiddleware may modify the Request value before forwarding
// SerializeInput along to the next SerializeHandler. The Parameters member
// should not be modified by SerializeMiddleware, InitializeMiddleware should
// be responsible for modifying the provided Parameter value.
type SerializeInput struct {
	Parameters interface{}
	Request    interface{}
}

// SerializeOutput provides the result returned by the next SerializeHandler.
type SerializeOutput struct {
	Result interface{}
}

// SerializeHandler provides the interface for the next handler the
// SerializeMiddleware will call in the middleware chain.
type SerializeHandler interface {
	HandleSerialize(ctx context.Context, in SerializeInput) (
		out SerializeOutput, metadata Metadata, err error,
	)
}

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

	// HandleSerialize 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.
	HandleSerialize(ctx context.Context, in SerializeInput, next SerializeHandler) (
		out SerializeOutput, metadata Metadata, err error,
	)
}

// SerializeMiddlewareFunc returns a SerializeMiddleware with the unique ID
// provided, and the func to be invoked.
func ( string,  func(context.Context, SerializeInput, SerializeHandler) (SerializeOutput, Metadata, error)) SerializeMiddleware {
	return serializeMiddlewareFunc{
		id: ,
		fn: ,
	}
}

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

	// Middleware function to be called.
	fn func(context.Context, SerializeInput, SerializeHandler) (
		SerializeOutput, Metadata, error,
	)
}

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

// HandleSerialize invokes the middleware Fn.
func ( serializeMiddlewareFunc) ( context.Context,  SerializeInput,  SerializeHandler) (
	 SerializeOutput,  Metadata,  error,
) {
	return .fn(, , )
}

var _ SerializeMiddleware = (serializeMiddlewareFunc{})

// SerializeStep provides the ordered grouping of SerializeMiddleware to be
// invoked on a handler.
type SerializeStep struct {
	newRequest func() interface{}
	ids        *orderedIDs
}

// NewSerializeStep returns a SerializeStep ready to have middleware for
// initialization added to it. The newRequest func parameter is used to
// initialize the transport specific request for the stack SerializeStep to
// serialize the input parameters into.
func ( func() interface{}) *SerializeStep {
	return &SerializeStep{
		ids:        newOrderedIDs(),
		newRequest: ,
	}
}

var _ Middleware = (*SerializeStep)(nil)

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

	var  SerializeHandler = serializeWrapHandler{Next: }
	for  := len() - 1;  >= 0; -- {
		 = decoratedSerializeHandler{
			Next: ,
			With: [].(SerializeMiddleware),
		}
	}

	 := SerializeInput{
		Parameters: ,
		Request:    .newRequest(),
	}

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

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

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

	return .(SerializeMiddleware), nil
}

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

	return .(SerializeMiddleware), nil
}

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

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

type serializeWrapHandler struct {
	Next Handler
}

var _ SerializeHandler = (*serializeWrapHandler)(nil)

// Implements SerializeHandler, converts types and delegates to underlying
// generic handler.
func ( serializeWrapHandler) ( context.Context,  SerializeInput) (
	 SerializeOutput,  Metadata,  error,
) {
	, ,  := .Next.Handle(, .Request)
	return SerializeOutput{
		Result: ,
	}, , 
}

type decoratedSerializeHandler struct {
	Next SerializeHandler
	With SerializeMiddleware
}

var _ SerializeHandler = (*decoratedSerializeHandler)(nil)

func ( decoratedSerializeHandler) ( context.Context,  SerializeInput) (
	 SerializeOutput,  Metadata,  error,
) {
	return .With.HandleSerialize(, , .Next)
}

// SerializeHandlerFunc provides a wrapper around a function to be used as a serialize middleware handler.
type SerializeHandlerFunc func(context.Context, SerializeInput) (SerializeOutput, Metadata, error)

// HandleSerialize calls the wrapped function with the provided arguments.
func ( SerializeHandlerFunc) ( context.Context,  SerializeInput) (SerializeOutput, Metadata, error) {
	return (, )
}

var _ SerializeHandler = SerializeHandlerFunc(nil)