class_name WmoRenderResourceFinalizer
extends RefCounted

## Polls lightweight WMO render-cache requests and publishes terminal outcomes.
## Request admission, cache lifetime and WMO materialization remain caller-owned.

var _expected_resource_script: Script
var _minimum_format_version: int
var _resource_loader_adapter: Object


func _init(
	expected_resource_script: Script,
	minimum_format_version: int,
	resource_loader_adapter: Object = null
) -> void:
	_expected_resource_script = expected_resource_script
	_minimum_format_version = minimum_format_version
	_resource_loader_adapter = resource_loader_adapter


## Polls a detached pending snapshot in insertion order. Only terminal requests
## are removed and published as an exact Resource reference or missing outcome.
func poll_terminal_requests(render_resource_cache_state: RefCounted) -> int:
	if render_resource_cache_state == null:
		return 0
	var completed_request_count := 0
	var request_paths: Dictionary = render_resource_cache_state.call(
		"request_paths_snapshot"
	)
	for normalized_relative_path_variant in request_paths.keys():
		var normalized_relative_path := String(normalized_relative_path_variant)
		var resource_path := String(request_paths[normalized_relative_path_variant])
		var load_status := load_threaded_get_status(resource_path)
		if (
			load_status != ResourceLoader.THREAD_LOAD_LOADED
			and load_status != ResourceLoader.THREAD_LOAD_FAILED
		):
			continue
		if load_status != ResourceLoader.THREAD_LOAD_LOADED:
			render_resource_cache_state.call(
				"complete_request_as_missing",
				normalized_relative_path
			)
			completed_request_count += 1
			continue
		var loaded_resource := load_threaded_get(resource_path)
		if is_current_render_resource(loaded_resource):
			render_resource_cache_state.call(
				"complete_request_with_resource",
				normalized_relative_path,
				loaded_resource
			)
		else:
			render_resource_cache_state.call(
				"complete_request_as_missing",
				normalized_relative_path
			)
		completed_request_count += 1
	return completed_request_count


## Accepts only the exact configured script and its current-or-newer format.
func is_current_render_resource(resource: Resource) -> bool:
	return (
		resource != null
		and _expected_resource_script != null
		and resource.get_script() == _expected_resource_script
		and int(resource.get("format_version")) >= _minimum_format_version
	)


## Production ResourceLoader status boundary; injectable for synthetic tests.
func load_threaded_get_status(resource_path: String) -> int:
	if _resource_loader_adapter != null:
		return int(_resource_loader_adapter.call(
			"load_threaded_get_status",
			resource_path
		))
	return ResourceLoader.load_threaded_get_status(resource_path)


## Production ResourceLoader result boundary; injectable for synthetic tests.
func load_threaded_get(resource_path: String) -> Resource:
	if _resource_loader_adapter != null:
		return _resource_loader_adapter.call(
			"load_threaded_get",
			resource_path
		) as Resource
	return ResourceLoader.load_threaded_get(resource_path)
