extends Area3D

var held_flask: DraggableObject
var flask_parent: Node

func _on_area_entered(area: Area3D) -> void:
	if held_flask:
		return
	if area.is_in_group("flask"):
		area.drag_ended_on.connect(snap_flask)


func _on_area_exited(area: Area3D) -> void:
	if held_flask:
		return
	if area.is_in_group("flask") and area.drag_ended_on.is_connected(snap_flask):
		area.drag_ended_on.disconnect(snap_flask)

func snap_flask(flask: DraggableObject) -> void:
	flask_parent = flask.get_parent()
	flask.reparent(self)
	held_flask = flask
	flask.drag_ended_on.disconnect(snap_flask)
	flask.position = Vector3.ZERO
	flask.drag_ended_on.connect(unsnap_flask)

func unsnap_flask(flask: DraggableObject) -> void:
	held_flask = null
	flask.reparent(flask_parent)
	flask.drag_ended_on.disconnect(unsnap_flask)
