package main

import (
	
	

	

	amhelp 
	am 
	amxhelp 
)

func init() {
	// load .env
	_ = godotenv.Load()

	// am-dbg is required for debugging, go run it
	// go run github.com/pancsta/asyncmachine-go/tools/cmd/am-dbg@latest
	amhelp.EnableDebugging(false)
	amhelp.SetEnvLogLevel(am.LogChanges)
}

func main() {
	 := context.Background()

	 := am.Schema{
		// task start state
		"Task": {Require: am.S{"Start"}},
		// task done state
		"TaskDone": {},

		// rest

		"Start": {},
		"Ready": {
			Auto:    true,
			Require: am.S{"TaskDone"},
		},
		"Healthcheck": {Multi: true},
	}
	 := am.S{"Start", "Task", "TaskDone", "Ready", "Healthcheck", am.StateException}

	// init state machine
	,  := am.NewCommon(, "fan", , , &am.ExceptionHandler{}, nil, &am.Opts{
		LogLevel: am.LogOps,
	})
	if  != nil {
		panic()
	}
	amhelp.MachDebugEnv()

	// define a task func
	 := func( int, ,  string) {
		 := .NewStateCtx()
		go func() {
			if .Err() != nil {
				return // expired
			}
			amhelp.Wait(, time.Second)
			if .Err() != nil {
				return // expired
			}
			.Add1(, nil)
		}()
	}

	// create task states
	// 10 tasks, 3 running concurrently
	_,  = amxhelp.FanOutIn(, "Task", 15, 3, )
	if  != nil {
		panic()
	}

	// start and wait
	.Add(am.S{"Start", "Task"}, nil)
	<-.When1("Ready", nil)

	// end
	println("done")

	// debug
	time.Sleep(time.Second)
}