class_name M2MeshResourceCacheState
extends RefCounted

## Owns normalized-path references to prepared static M2 Mesh resources.
## The caller owns loading, preparation, missing-state and materialization.

var _mesh_by_normalized_path: Dictionary = {}


## Stores one prepared Mesh for a non-empty normalized M2 path. A later store
## for the same path replaces the retained reference.
func store_mesh(normalized_relative_path: String, mesh: Mesh) -> bool:
	if normalized_relative_path.is_empty() or mesh == null:
		return false
	_mesh_by_normalized_path[normalized_relative_path] = mesh
	return true


## Returns whether one normalized path currently has a retained Mesh.
func has_mesh(normalized_relative_path: String) -> bool:
	return (
		not normalized_relative_path.is_empty()
		and _mesh_by_normalized_path.has(normalized_relative_path)
	)


## Returns the exact retained Mesh reference, or null for empty/unknown paths.
func find_mesh(normalized_relative_path: String) -> Mesh:
	if not has_mesh(normalized_relative_path):
		return null
	return _mesh_by_normalized_path[normalized_relative_path] as Mesh


## Returns the number of retained static M2 Mesh resources.
func mesh_count() -> int:
	return _mesh_by_normalized_path.size()


## Returns a detached insertion-order list of cached normalized paths.
func normalized_paths_snapshot() -> Array[String]:
	var normalized_paths: Array[String] = []
	for normalized_path_variant in _mesh_by_normalized_path.keys():
		normalized_paths.append(String(normalized_path_variant))
	return normalized_paths


## Releases all retained Mesh references. The loader calls this only after
## asynchronous work has completed during final scene shutdown.
func clear() -> void:
	_mesh_by_normalized_path.clear()
