class_name WmoSceneResourceCacheState
extends RefCounted

## Owns validated cached-WMO PackedScenes and their load state. File limits,
## ResourceLoader I/O and scene-cache validation remain caller responsibilities.

var _scenes_by_normalized_path: Dictionary = {}
var _missing_normalized_paths: Dictionary = {}
var _request_cache_paths_by_normalized_path: Dictionary = {}


## Returns the validated PackedScene for a normalized WMO path, or null.
func scene_for(normalized_relative_path: String) -> PackedScene:
	if normalized_relative_path.is_empty():
		return null
	return _scenes_by_normalized_path.get(normalized_relative_path) as PackedScene


## Returns whether a validated PackedScene is cached for the normalized path.
func contains_scene(normalized_relative_path: String) -> bool:
	return (
		not normalized_relative_path.is_empty()
		and _scenes_by_normalized_path.has(normalized_relative_path)
	)


## Returns whether the normalized path is in the negative cache.
func is_missing(normalized_relative_path: String) -> bool:
	return (
		not normalized_relative_path.is_empty()
		and _missing_normalized_paths.has(normalized_relative_path)
	)


## Returns whether the normalized path has a pending threaded load request.
func has_request(normalized_relative_path: String) -> bool:
	return (
		not normalized_relative_path.is_empty()
		and _request_cache_paths_by_normalized_path.has(normalized_relative_path)
	)


## Records one loader-started request. Occupied or empty paths are rejected.
func remember_request(normalized_relative_path: String, cache_path: String) -> bool:
	if normalized_relative_path.is_empty() or cache_path.is_empty():
		return false
	if _has_any_state(normalized_relative_path):
		return false
	_request_cache_paths_by_normalized_path[normalized_relative_path] = cache_path
	return true


## Records an absent, oversize or request-start failure without requiring a
## pending request. Occupied and empty paths are rejected.
func mark_missing(normalized_relative_path: String) -> bool:
	if normalized_relative_path.is_empty() or _has_any_state(normalized_relative_path):
		return false
	_missing_normalized_paths[normalized_relative_path] = true
	return true


## Returns a detached normalized-path to cache-path snapshot for loader polling.
func request_paths_snapshot() -> Dictionary:
	return _request_cache_paths_by_normalized_path.duplicate()


## Completes a pending request with a caller-validated PackedScene.
func complete_request_with_scene(
		normalized_relative_path: String,
		validated_scene: PackedScene
) -> bool:
	if (
			normalized_relative_path.is_empty()
			or validated_scene == null
			or not _request_cache_paths_by_normalized_path.has(normalized_relative_path)
	):
		return false
	_request_cache_paths_by_normalized_path.erase(normalized_relative_path)
	_missing_normalized_paths.erase(normalized_relative_path)
	_scenes_by_normalized_path[normalized_relative_path] = validated_scene
	return true


## Completes a pending request as missing after load or validation failure.
func complete_request_as_missing(normalized_relative_path: String) -> bool:
	if (
			normalized_relative_path.is_empty()
			or not _request_cache_paths_by_normalized_path.has(normalized_relative_path)
	):
		return false
	_request_cache_paths_by_normalized_path.erase(normalized_relative_path)
	_scenes_by_normalized_path.erase(normalized_relative_path)
	_missing_normalized_paths[normalized_relative_path] = true
	return true


## Clears pending and negative state while retaining validated PackedScenes.
func clear_transient_state() -> void:
	_request_cache_paths_by_normalized_path.clear()
	_missing_normalized_paths.clear()


## Releases every cached scene and transient entry. Safe to call repeatedly.
func clear_all() -> void:
	_scenes_by_normalized_path.clear()
	clear_transient_state()


## Returns the number of pending threaded load requests.
func pending_request_count() -> int:
	return _request_cache_paths_by_normalized_path.size()


## Returns detached, sorted scalar keys without exposing PackedScene references.
func diagnostic_snapshot() -> Dictionary:
	var scene_paths: Array = _scenes_by_normalized_path.keys()
	var missing_paths: Array = _missing_normalized_paths.keys()
	var request_paths: Array = _request_cache_paths_by_normalized_path.keys()
	scene_paths.sort()
	missing_paths.sort()
	request_paths.sort()
	return {
		"scene_paths": scene_paths.duplicate(),
		"missing_paths": missing_paths.duplicate(),
		"request_paths": request_paths.duplicate(),
	}


func _has_any_state(normalized_relative_path: String) -> bool:
	return (
		_scenes_by_normalized_path.has(normalized_relative_path)
		or _missing_normalized_paths.has(normalized_relative_path)
		or _request_cache_paths_by_normalized_path.has(normalized_relative_path)
	)
