extends Area3D

class_name InteractionSource

@export var interaction_dialog: ConfirmationDialog
var recievers: int = 0

signal interaction_requested
signal interaction_closed
signal interaction_reciever_entered(reicever: InteractionReciever)
signal interaction_reciever_exited(reicever: InteractionReciever)

func _ready() -> void:
	area_entered.connect(on_area_entered)
	area_exited.connect(on_area_exited)
	interaction_dialog.canceled.connect(interaction_closed.emit)
	interaction_dialog.confirmed.connect(interaction_closed.emit)

func on_area_entered(area: Area3D):
	if area is InteractionReciever:
		interaction_reciever_entered.emit(area)
		recievers += 1

func on_area_exited(area: Area3D):
	if area is InteractionReciever:
		interaction_reciever_exited.emit(area)
		recievers -= 1

func request_interaction() -> void:
	if recievers == 0:
		interaction_closed.emit()
		return
	interaction_requested.emit()
	interaction_dialog.popup_centered()
