package middleware

import (
	"reflect"

	"github.com/ngaut/log"
	"github.com/pingcap/schrodinger/notifiermgr/event"
)

func forEachDriver(_ *interface{}) Middleware {
	input := make(chan event.Event)
	output := make(chan event.Event)

	go func() {
		for ev := range input {
			if reflect.TypeOf(ev).Kind() == reflect.Slice {
				slice := reflect.ValueOf(ev)

				for index := 0; index < slice.Len(); index++ {
					item := slice.Index(index).Interface()
					output <- item
				}
			} else {
				log.Error("Input of foreach is not a slice")
				continue
			}
		}
	}()

	return &RealMiddleware{
		input,
		output,
	}
}
