package process

import (
	
)

// Chain returns a Runnable instance that starts and runs processes by nesting
// them in callbacks. If no processes are given, it returns Nop instance.
func ( ...Runnable) Runnable {
	switch len() {
	case 0:
		return Nop()
	case 1:
		return [0]
	}
	return &chainRunnable{}
}

type chainRunnable struct {
	deps []Runnable
}

func ( *chainRunnable) ( context.Context,  Callback) error {
	 := chainState{deps: .deps}
	return .Run(, )
}

type chainState struct {
	index int
	deps  []Runnable // len(deps) >= 2
	main  func(ctx context.Context) error
}

func ( *chainState) ( context.Context,  Callback) error {
	switch .index {
	case 0:
		.main = 
		 = .next
	case len(.deps) - 1:
		 = .main
	default:
		 = .next
	}
	 := .index
	.index++
	return .deps[].Run(, )
}

func ( *chainState) ( context.Context) error {
	return .Run(, nil)
}