class_name ControlBinding extends CheckerContainer
## Input for rebinding an action.

const ACTION_KEY_FMT := "ACTION_{0}"
const SCENE := preload("res://src/ui/menus/settings_menu/control_binding/control_binding.tscn")

@export var key: StringName

@onready var action: Label = %Action
@onready var binding: Label = %Binding


func _ready() -> void:
	# gdlint:ignore = private-method-call
	super._ready()

	# Set action label text
	var loc_action := tr(ACTION_KEY_FMT.format([key]))
	# Fall back to just the key if no localization exists
	@warning_ignore("incompatible_ternary")
	action.text = loc_action if loc_action else key

	# Set the binding label
	var actions := InputMap.action_get_events(key)
	if actions:
		var primary := actions[0]
		binding.text = PromptMap.from_event(primary)


static func create(_key: StringName) -> ControlBinding:
	var instance: ControlBinding = SCENE.instantiate()
	instance.key = _key
	return instance
