@tool
extends Label
## A prompt with the input symbol bound to the given action.

const PROMPT_FORMAT := "{0} - {1}"
const HOLD_FORMAT := "{0} {1}"

const UNKNOWN_LABEL_SYM := "[unknown]"

const ACTION_HOLD_KEY := "ACTION_HOLD_MODIFIER"

@export var action: StringName:
	set(value):
		action = value
		_update()

@export var hold := false:
	set(value):
		hold = value
		_update()

@export var show_name := true:
	set(value):
		show_name = value
		_update()

@export var event_index := 0:
	set(value):
		event_index = value
		_update()


func _ready() -> void:
	_update()


func _update() -> void:
	var input_symbol: String
	var actions := InputMap.action_get_events(action)
	if actions and event_index < len(actions):
		var primary := actions[event_index]
		input_symbol = PromptMap.from_event(primary)

	var loc_action := tr(ControlBinding.ACTION_KEY_FMT.format([action]))

	if not input_symbol:
		input_symbol = str(PromptMap.UNKNOWN_INPUT_SYMBOL)

	if hold:
		input_symbol = HOLD_FORMAT.format([input_symbol, tr(ACTION_HOLD_KEY)])

	if show_name:
		text = (
			PROMPT_FORMAT
			. format(
				[
					input_symbol,
					loc_action if loc_action else str(action),
				]
			)
		)
	else:
		text = input_symbol
