//go:build !tinygo

package machine

import (
	
	
)

// NewStates inits the *StatesDef struct by assigning state names to string
// values and storing the name order.
func [ States]( )  {
	// read and assign names of all the embedded structs
	 := S{}
	 := map[string][]int{}
	 := reflect.ValueOf(&).Elem()
	 := []string{}
	parseStateNames(, &, "self", , &, "")
	.SetNames()
	.SetStateGroups(, )

	return 
}

func parseStateNames(
	 reflect.Value,  *S,  string,  map[string][]int,
	 *[]string,  string,
) {
	if  != "StatesBase" {
		[] = []int{}
		* = append(*, )
	}
	 := .Type()
	for  := 0;  < .NumField(); ++ {

		 := .Field()
		 := .Field()
		 := .Type.Kind()

		// nested struct (inherit states with a prefix)
		if !.Anonymous &&  == reflect.Pointer &&
			.Type.Elem().Kind() == reflect.Struct {

			if .IsNil() {
				 := reflect.New(.Type.Elem())
				.Set()
			}

			// set prefix on nested struct's StatesBase
			 := .Elem().FieldByName("StatesBase")
			if .IsValid() && .Kind() == reflect.Pointer {
				if .IsNil() {
					.Set(reflect.New(.Type().Elem()))
				}
				if ,  := .Interface().(*StatesBase);  {
					.prefix =  + .Name
				}
			}

			(
				.Elem(), , .Name, , , +.Name)

		} else if .Anonymous &&  == reflect.Pointer &&
			// embedded struct (inherit states)
			.Type.Elem().Kind() == reflect.Struct {

			if .IsNil() {
				 := reflect.New(.Type.Elem())
				.Set()
			}

			(.Elem(), , .Name, , , )

		} else if .CanSet() &&  == reflect.String {
			// local state name
			 :=  + .Name
			.SetString()
			if !slices.Contains(*, ) {
				if  != "StatesBase" {
					[] = append([], len(*))
				}
				* = append(*, )
			}
		}
	}
}

// NewStateGroups inits a *GroupsDef struct with state lists and optionally
// inherits from parent *GroupsDefs instances.
func [ any]( ,  ...any)  {
	// init nil embeds
	 := reflect.ValueOf(&).Elem()
	initNilEmbeds()

	// assign values from parent mixins into the local instance
	for  := range  {
		copyFields([], &)
	}

	return 
}

func initNilEmbeds( reflect.Value) {
	 := .Type()
	for  := 0;  < .NumField(); ++ {

		 := .Field()
		 := .Field()
		 := .Type.Kind()

		if .Anonymous &&  == reflect.Pointer &&
			.Type.Elem().Kind() == reflect.Struct {

			if .IsNil() {
				 := reflect.New(.Type.Elem())
				.Set()
			}
			(.Elem())
		}
	}
}

func copyFields(,  interface{}) {
	if  == nil {
		return
	}
	 := reflect.ValueOf()
	 := reflect.ValueOf()

	if .Kind() == reflect.Pointer {
		 = .Elem()
	}
	if .Kind() == reflect.Pointer {
		 = .Elem()
	}

	for  := 0;  < .NumField(); ++ {
		 := .Type().Field().Name
		 := .Field()
		 := .FieldByName()

		if .Kind() == reflect.Struct {
			(.Addr().Interface(), .Addr().Interface())
		} else {
			if .CanSet() {
				.Set()
			}
		}
	}
}