//go:build !tinygo

package machine

import (
	
	
	
)

// ///// ///// /////

// ///// MACHINE

// ///// ///// /////

// HandlersBind binds a struct of handler methods to machine's states, based on
// the naming convention, eg `FooState(e *Event)`. Negotiation handlers can
// optionally return bool.
func ( *Machine) ( any,  ...BindOpts) (string, error) {
	 := reflect.ValueOf()
	if .Kind() != reflect.Pointer || .Elem().Kind() != reflect.Struct {
		return "", errors.New("BindHandlers expects a pointer to a struct")
	}

	return .bindHandlers(.newHandlerStruct(, &), optBindOpts())
}

func ( *handlerCall) () *handlerReturn {
	 := handlerReturn{}
	if .refl.fn != nil {
		 := .refl.fn.Call([]reflect.Value{reflect.ValueOf(.event)})
		.called = true
		if len() > 0 {
			// TODO log err?
			,  := [0].Interface().(bool)
			,  := [0].Interface().(S)
			if  {
				.retBool = 
			} else if  {
				// TODO optimize: with nil as prev value
				.retStates = 
				.partialNeg = true
			}
		} else {
			// default
			.retBool = true
		}
	} else if .negotiation != nil {
		.retBool = .negotiation(.event)
		.called = true
	} else if .negotiationPartial != nil {
		.retStates = .negotiationPartial(.event)
		.partialNeg = true
		.called = true
	} else if .final != nil {
		.final(.event)
		.called = true
	} else {
		panic("invalid handler call " + .name)
	}

	return &
}

// newHandlerStruct creates a [handler] binding for a struct.
func ( *Machine) (
	 any,  *reflect.Value,
) *handler {
	if .disposing.Load() {
		return &handler{}
	}
	 := &handler{
		isMap: false,
		refl: handlerReflect{
			h:            ,
			methods:      ,
			methodCache:  make(map[string]reflect.Value),
			missingCache: make(map[string]struct{}),
		},
	}

	return 
}

func tracerValidate( Tracer) error {
	 := reflect.ValueOf()
	if .Kind() != reflect.Pointer || .Elem().Kind() != reflect.Struct {
		return errors.New("BindTracer expects a pointer to a struct")
	}

	return nil
}

// SetGroups organizes the schema into a tree using schema-v2 structs.
func ( *Machine) ( any,  States) {
	// TODO rename to SchemaOrganize(optGroups, optStates, ...)
	// TODO call VerifyStates from optStates.Names()

	.schemaMx.Lock()
	defer .schemaMx.Unlock()
	 := map[string][]int{}
	 := []string{}
	 := .stateNames

	// add all the groups
	if  != nil {
		// TODO extract
		var  func(reflect.Value)
		 = func( reflect.Value) {
			if .Kind() == reflect.Pointer {
				if .IsNil() {
					return
				}
				 = .Elem()
			}
			if .Kind() != reflect.Struct {
				return
			}

			 := .Type()
			// Local groups first.
			for  := 0;  < .NumField(); ++ {
				 := .Field()
				if .Type.Kind() != reflect.Slice {
					continue
				}
				 := .Field()
				if !.CanInterface() {
					continue
				}
				,  := .Interface().(S)
				if ! {
					continue
				}
				if ,  := [.Name];  {
					continue
				}
				[.Name] = .Index()
				 = append(, .Name)
			}

			// Then inherited embedded groups.
			for  := 0;  < .NumField(); ++ {
				 := .Field()
				if !.Anonymous {
					continue
				}
				 := .Type.Kind()
				if  == reflect.Struct ||
					( == reflect.Pointer &&
						.Type.Elem().Kind() == reflect.Struct) {

					(.Field())
				}
			}
		}
		(reflect.ValueOf())
	}

	// add all the schemas (nested)

	// state schema structure
	if  != nil {
		,  := .StateGroups()
		for ,  := range  {
			[] = []
			 = append(, )
		}
	}

	.groups = 
	.groupsOrder = 
}

// TODO move
func funcName( func()) string {
	 := reflect.ValueOf().Pointer()
	 := runtime.FuncForPC()
	if  == nil {
		return ""
	}
	 := .Name()

	// file, line := funcObj.FileLine(funcPtr)
	return 
}