class_name EquipmentHandler extends Node3D

@export var equipment: Equipment

var primary_node: Node
var secondary_node: Node

func _ready():
	equipment.unequipped_primary.connect(_on_unequipped_primary)
	equipment.equipped_primary.connect(_on_equipped_primary)

func equip(item: EquippableItem):
	equipment.set_primary(Option.from(item))

func unequip():
	equipment.set_primary(Option.none)

func remove_equipment():
	if primary_node != null:
		primary_node.queue_free()
		primary_node = null

func equip_item(item: EquippableItem):
	remove_equipment()
	if item != null:
		primary_node = item.scene.instantiate()
		add_child(primary_node)

func _on_unequipped_primary(_item: EquippableItem):
	remove_equipment()

func _on_equipped_primary(item: EquippableItem):
	equip_item(item)

func use_primary():
	if primary_node != null:
		primary_node.use()

