package middleware

import (
	
)

// DeserializeInput provides the input parameters for the DeserializeInput to
// consume. DeserializeMiddleware should not modify the Request, and instead
// forward it along to the next DeserializeHandler.
type DeserializeInput struct {
	Request interface{}
}

// DeserializeOutput provides the result returned by the next
// DeserializeHandler. The DeserializeMiddleware should deserialize the
// RawResponse into a Result that can be consumed by middleware higher up in
// the stack.
type DeserializeOutput struct {
	RawResponse interface{}
	Result      interface{}
}

// DeserializeHandler provides the interface for the next handler the
// DeserializeMiddleware will call in the middleware chain.
type DeserializeHandler interface {
	HandleDeserialize(ctx context.Context, in DeserializeInput) (
		out DeserializeOutput, metadata Metadata, err error,
	)
}

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

	// HandleDeserialize 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.
	HandleDeserialize(ctx context.Context, in DeserializeInput, next DeserializeHandler) (
		out DeserializeOutput, metadata Metadata, err error,
	)
}

// DeserializeMiddlewareFunc returns a DeserializeMiddleware with the unique ID
// provided, and the func to be invoked.
func ( string,  func(context.Context, DeserializeInput, DeserializeHandler) (DeserializeOutput, Metadata, error)) DeserializeMiddleware {
	return deserializeMiddlewareFunc{
		id: ,
		fn: ,
	}
}

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

	// Middleware function to be called.
	fn func(context.Context, DeserializeInput, DeserializeHandler) (
		DeserializeOutput, Metadata, error,
	)
}

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

// HandleDeserialize invokes the middleware Fn.
func ( deserializeMiddlewareFunc) ( context.Context,  DeserializeInput,  DeserializeHandler) (
	 DeserializeOutput,  Metadata,  error,
) {
	return .fn(, , )
}

var _ DeserializeMiddleware = (deserializeMiddlewareFunc{})

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

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

var _ Middleware = (*DeserializeStep)(nil)

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

	var  DeserializeHandler = deserializeWrapHandler{Next: }
	for  := len() - 1;  >= 0; -- {
		 = decoratedDeserializeHandler{
			Next: ,
			With: [].(DeserializeMiddleware),
		}
	}

	 := DeserializeInput{
		Request: ,
	}

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

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

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

	return .(DeserializeMiddleware), nil
}

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

	return .(DeserializeMiddleware), nil
}

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

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

type deserializeWrapHandler struct {
	Next Handler
}

var _ DeserializeHandler = (*deserializeWrapHandler)(nil)

// HandleDeserialize implements DeserializeHandler, converts types and delegates to underlying
// generic handler.
func ( deserializeWrapHandler) ( context.Context,  DeserializeInput) (
	 DeserializeOutput,  Metadata,  error,
) {
	, ,  := .Next.Handle(, .Request)
	return DeserializeOutput{
		RawResponse: ,
	}, , 
}

type decoratedDeserializeHandler struct {
	Next DeserializeHandler
	With DeserializeMiddleware
}

var _ DeserializeHandler = (*decoratedDeserializeHandler)(nil)

func ( decoratedDeserializeHandler) ( context.Context,  DeserializeInput) (
	 DeserializeOutput,  Metadata,  error,
) {
	return .With.HandleDeserialize(, , .Next)
}

// DeserializeHandlerFunc provides a wrapper around a function to be used as a deserialize middleware handler.
type DeserializeHandlerFunc func(context.Context, DeserializeInput) (DeserializeOutput, Metadata, error)

// HandleDeserialize invokes the wrapped function with the given arguments.
func ( DeserializeHandlerFunc) ( context.Context,  DeserializeInput) (DeserializeOutput, Metadata, error) {
	return (, )
}

var _ DeserializeHandler = DeserializeHandlerFunc(nil)