class_name PromptContainer
extends Control
## Free itself when every prompt has been used


## If true, automatically frees itself when there are no more input prompts among children
@export var remove_when_prompts_depleted := true

var _nb_prompts: int


func _ready() -> void:
	_nb_prompts = 0
	for child in get_children():
		if child is InputPrompt:
			child.pressed.connect(child.queue_free)
		if child is InputPrompt or child is PromptContainer:
			child.tree_exited.connect(_on_child_exited_tree)
			_nb_prompts += 1


func switch(enable: bool) -> void:
	if enable:
		enable()
	else:
		disable()


# Show and process
func enable() -> void:
	set_visible(true)
	set_process_mode(Node.PROCESS_MODE_PAUSABLE)


# Hide and stop processing
func disable() -> void:
	set_visible(false)
	set_process_mode(Node.PROCESS_MODE_DISABLED)


func remove_if_no_more_prompt() -> void:
	_nb_prompts -= 1
	print(_nb_prompts, " remaining prompts")
	if _nb_prompts <= 0 and remove_when_prompts_depleted:
		# Only label remaining
		print("Destruction !")
		queue_free()


func _on_child_exited_tree() -> void:
	remove_if_no_more_prompt()
