package ecs

import (
	
	
	
)

var nilDummy *struct{} = nil

// capPow2 calculated the next power-of-2 capacity value.
func capPow2( uint32) uint32 {
	if  == 0 {
		return 1
	}
	--
	 |=  >> 1
	 |=  >> 2
	 |=  >> 4
	 |=  >> 8
	 |=  >> 16
	return  + 1
}

// get the component for an entity from a component storage.
//
// Returns nil if the entity does not have the component.
func get[ any]( *componentStorage,  *entityIndex) * {
	 := .columns[.table]
	if  == nil {
		return nil
	}
	return (*)(.Get(uintptr(.row)))
}

// copyPtr copies from one pointer to another.
// This is not GC-safe. Use only for trivial/value types.
func copyPtr(,  unsafe.Pointer,  uintptr) {
	 := (*[math.MaxInt32]byte)()[::]
	 := (*[math.MaxInt32]byte)()[::]
	copy(, )
}

// copyValue copies an item between two reflect arrays using typedmemmove.
// This is GC-safe. Use for non-trivial types.
func copyValue(,  *column, ,  uintptr) {
	 := unsafe.Add(.pointer, *.itemSize)
	 := unsafe.Add(.pointer, *.itemSize)
	typedmemmove(.typePtr, , )
}

// copyRange copies a range of items from one reflect array to another.
// Copies src[:count] to dst[start:].
// This is GC-safe. Use for non-trivial types.
func copyRange(,  *column, ,  uint32) {
	if  <= 64 {
		copyRangeSmall(, , uintptr(), uintptr())
	} else {
		copyRangeLarge(, , int(), int())
	}
}

// copyRangeSmall copies a range of items from one reflect array to another.
// Copies src[:count] to dst[start:].
// This is GC-safe. Use for non-trivial types.
//
// Should be used for small ranges (<=64).
func copyRangeSmall(,  *column, ,  uintptr) {
	 := .itemSize
	 := unsafe.Add(.pointer, *)
	 := .pointer

	for range  {
		typedmemmove(.typePtr, , )
		 = unsafe.Add(, )
		 = unsafe.Add(, )
	}
}

// copyRangeLarge copies a range of items from one reflect array to another.
// Copies src[:count] to dst[start:].
// This is GC-safe. Use for non-trivial types.
//
// Should be used for large ranges (>64).
func copyRangeLarge(,  *column, ,  int) {
	 := .data.Slice(0, )
	 := .data.Slice(, +)
	reflect.Copy(, )
}

//go:linkname memclrNoHeapPointers runtime.memclrNoHeapPointers
func memclrNoHeapPointers( unsafe.Pointer,  uintptr)

//go:linkname typedmemclr reflect.typedmemclr
//go:noescape
func typedmemclr( unsafe.Pointer,  unsafe.Pointer)

//go:linkname typedmemmove runtime.typedmemmove
//go:noescape
func typedmemmove( unsafe.Pointer, ,  unsafe.Pointer)

type ifaceWords struct {
	typ  uintptr
	data uintptr
}

//nolint:staticcheck,govet
func rtypePtr( reflect.Type) unsafe.Pointer {
	// reflect.Type is interface (type word, data word) in current runtime.
	// We need the data word, i.e. pointer to runtime type info.
	 := *(*ifaceWords)(unsafe.Pointer(&))
	return unsafe.Pointer(.data)
}

// isRelation determines whether a type is a relation component.
func isRelation( reflect.Type) bool {
	if .Kind() != reflect.Struct || .NumField() == 0 {
		return false
	}
	 := .Field(0)
	return .Type == relationType && .Name == relationType.Name()
}

// isTrivial checks if a type is "trivial" (contains no pointers, slices, maps, strings, or channels).
// It also returns false if the type itself is one of these.
func isTrivial( reflect.Type) bool {
	// Base case: If the type is invalid, return false
	if  == nil {
		return false
	}

	// Check if the type itself is a pointer, slice, map, or channel
	switch .Kind() {
	//nolint:govet
	case reflect.Ptr, reflect.Slice, reflect.Map, reflect.Chan, reflect.Interface, reflect.String:
		return false
	}

	// If it's a struct, check its fields recursively
	if .Kind() == reflect.Struct {
		for  := range .NumField() {
			 := .Field().Type
			if !() {
				return false
			}
		}
	}

	// If it's an array, check its elements recursively
	if .Kind() == reflect.Array {
		 := .Elem()
		if !() {
			return false
		}
	}

	// If none of the above conditions matched, it's trivial
	return true
}

// pagedSlice is a paged collection working with pages of length 32 slices.
// its primary purpose is pointer persistence, which is not given using simple slices.
//
// Implements [archetypes].
type pagedSlice[ any] struct {
	pages    [][]
	len      int32
	lenLast  int32
	pageSize int32
}

func newPagedSlice[ any]( int32) pagedSlice[] {
	return pagedSlice[]{
		pageSize: ,
	}
}

// Add adds a value to the paged slice.
func ( *pagedSlice[]) ( ) {
	if .len == 0 || .lenLast == .pageSize {
		.pages = append(.pages, make([], .pageSize))
		.lenLast = 0
	}
	.pages[len(.pages)-1][.lenLast] = 
	.len++
	.lenLast++
}

// Get returns the value at the given index.
func ( *pagedSlice[]) ( int32) * {
	return &.pages[/.pageSize][%.pageSize]
}

// Set sets the value at the given index.
func ( *pagedSlice[]) ( int32,  ) {
	.pages[/.pageSize][%.pageSize] = 
}

// Len returns the current number of items in the paged slice.
func ( *pagedSlice[]) () int32 {
	return .len
}