package ecs

import (
	
	
	
)

// componentRegistry keeps track of type IDs.
type registry struct {
	Used       bitMask                // Mapping from IDs to used status.
	Types      []reflect.Type         // Mapping from IDs to types.
	IDs        []uint8                // List of IDs.
	Components map[reflect.Type]uint8 // Mapping from types to IDs.
}

// newComponentRegistry creates a new ComponentRegistry.
func newRegistry() registry {
	return registry{
		Components: map[reflect.Type]uint8{},
		Types:      make([]reflect.Type, maskTotalBits),
		Used:       bitMask{},
		IDs:        []uint8{},
	}
}

// ComponentID returns the ID for a component type, and registers it if not already registered.
// The second return value indicates if it is a newly created ID.
func ( *registry) ( reflect.Type) (uint8, bool) {
	if ,  := .Components[];  {
		return , false
	}
	return .registerComponent(, maskTotalBits), true
}

// ComponentType returns the type of a component by ID.
func ( *registry) ( uint8) (reflect.Type, bool) {
	return .Types[], .Used.Get()
}

// Count returns the total number of reserved IDs. It is the maximum ID plus 1.
func ( *registry) () int {
	return len(.Components)
}

// registerComponent registers a components and assigns an ID for it.
func ( *registry) ( reflect.Type,  int) uint8 {
	 := len(.Components)
	if  >=  {
		panic(fmt.Sprintf("exceeded the maximum of %d component types or resource types", ))
	}
	 := uint8()
	 := id()
	.Components[], .Types[] = , 
	.Used.Set(.id)
	.IDs = append(.IDs, )
	return 
}

// unregisterLastComponent unregistered the component type that was registered last.
func ( *registry) () {
	 := uint8(len(.Components) - 1)
	 := id8()
	,  := .ComponentType()
	delete(.Components, )
	.Types[] = nil
	.Used.Clear(.id)
	.IDs = .IDs[:len(.IDs)-1]
}

// componentRegistry keeps track of component IDs.
// In addition to [registry], it determines whether types
// are relation components and/or contain (or are) pointers.
type componentRegistry struct {
	registry
	IsRelation []bool
	IsTrivial  []bool
	Archetypes []int  // Number of archetypes for each component.
	version    uint32 // Generation to indicate changes to archetype count per component.
}

// newComponentRegistry creates a new ComponentRegistry.
func newComponentRegistry() componentRegistry {
	return componentRegistry{
		registry:   newRegistry(),
		IsRelation: make([]bool, maskTotalBits),
		IsTrivial:  make([]bool, maskTotalBits),
		Archetypes: make([]int, maskTotalBits),
		version:    1,
	}
}

// ComponentID returns the ID for a component type, and registers it if not already registered.
// The second return value indicates if it is a newly created ID.
func ( *componentRegistry) ( reflect.Type) (uint8, bool) {
	if ,  := .Components[];  {
		return , false
	}
	return .registerComponent(, maskTotalBits), true
}

// registerComponent registers a components and assigns an ID for it.
func ( *componentRegistry) ( reflect.Type,  int) uint8 {
	 := .registry.registerComponent(, )
	.IsRelation[] = isRelation()
	.IsTrivial[] = isTrivial()
	return 
}

// unregisterLastComponent unregistered the component type that was registered last.
func ( *componentRegistry) () {
	 := uint8(len(.Components) - 1)
	.registry.unregisterLastComponent()
	.IsRelation[] = false
}

// addArchetype increments the archetype counter for an entity
// and the registry's version number.
func ( *componentRegistry) ( uint8) {
	.Archetypes[]++
	.version++
}

// Returns the ID of the component present in the smallest number of archetypes.
func ( *componentRegistry) ( []ID) ID {
	 := math.MaxInt
	var  ID
	for ,  := range  {
		 := .Archetypes[.id]
		if  <  {
			 = 
			 = 
		}
	}
	return 
}