package ecs

import 

// nodeID is the ID type for graph nodes.
type nodeID uint32

// node in the archetype graph.
type node struct {
	neighbors idMap
	mask      bitMask
	id        nodeID
	archetype archetypeID
}

// newNode creates a new node for the given archetype ID.
//
// The mask gets copied.
func newNode( nodeID,  archetypeID,  *bitMask) node {
	return node{
		id:        ,
		archetype: ,
		mask:      *,
		neighbors: newIDMap(),
	}
}

// GetArchetype returns the archetype of the node, and whether the node has an archetype.
func ( *node) () (archetypeID, bool) {
	return .archetype, .archetype != maxArchetypeID
}

// Archetype graph for faster lookup of transitions.
type graph struct {
	nodes []node
}

// newGraph creates a new empty graph.
func newGraph() graph {
	 := make([]node, 0, 128)
	 = append(, newNode(0, 0, &bitMask{}))

	return graph{
		nodes: ,
	}
}

// Find the node for a given start node and components to add and remove.
//
// The bitMask argument gets modified and reflects the mask of the resulting node.
func ( *graph) ( nodeID,  []ID,  []ID,  *bitMask) *node {
	 := &.nodes[]
	 := 

	for ,  := range  {
		if !.Get(.id) {
			panic(fmt.Sprintf("entity does not have component with ID %d", .id))
		}
		.Clear(.id)
		if .neighbors.used.Get(.id) {
			 = &.nodes[.neighbors.data[.id]]
		} else {
			 := .findOrCreate()
			.neighbors.Set(.id, .id)
			 = &.nodes[.id]
			.neighbors.Set(.id, .id)
			 = 
		}
	}

	for ,  := range  {
		if .Get(.id) {
			panic(fmt.Sprintf("entity already has component with ID %d, or it was added twice", .id))
		}
		if .mask.Get(.id) {
			panic(fmt.Sprintf("component with ID %d added and removed in the same exchange operation", .id))
		}
		.Set(.id)
		if .neighbors.used.Get(.id) {
			 = &.nodes[.neighbors.data[.id]]
		} else {
			 := .findOrCreate()
			.neighbors.Set(.id, .id)
			 = &.nodes[.id]
			.neighbors.Set(.id, .id)
			 = 
		}
	}

	return 
}

// FindAdd finds the node for a given start node and components to add.
//
// The bitMask argument gets modified and reflects the mask of the resulting node.
func ( *graph) ( nodeID,  []ID,  *bitMask) *node {
	 := &.nodes[]
	 := 

	for ,  := range  {
		if .Get(.id) {
			panic(fmt.Sprintf("entity already has component with ID %d, or it was added twice", .id))
		}
		.Set(.id)
		if .neighbors.used.Get(.id) {
			 = &.nodes[.neighbors.data[.id]]
		} else {
			 := .findOrCreate()
			.neighbors.Set(.id, .id)
			 = &.nodes[.id]
			.neighbors.Set(.id, .id)
			 = 
		}
	}

	return 
}

// FindRemove finds the node for a given start node and components to remove.
//
// The bitMask argument gets modified and reflects the mask of the resulting node.
func ( *graph) ( nodeID,  []ID,  *bitMask) *node {
	 := &.nodes[]
	 := 

	for ,  := range  {
		if !.Get(.id) {
			panic(fmt.Sprintf("entity does not have component with ID %d", .id))
		}
		.Clear(.id)
		if .neighbors.used.Get(.id) {
			 = &.nodes[.neighbors.data[.id]]
		} else {
			 := .findOrCreate()
			.neighbors.Set(.id, .id)
			 = &.nodes[.id]
			.neighbors.Set(.id, .id)
			 = 
		}
	}

	return 
}

// finds or creates a node if not reachable in the current graph.
func ( *graph) ( *bitMask) *node {
	 := len(.nodes)
	for  := range  {
		 := &.nodes[]
		if .mask.Equals() {
			return 
		}
	}
	.nodes = append(.nodes, newNode(nodeID(), maxArchetypeID, ))
	return &.nodes[]
}