--Internal interface

local function InitialReference(self)
	--Disconnect current reference connection
	if self.cur_p_con ~= nil then
		self.cur_p_con:Disconnect()
		self.cur_p_con = nil
	end

	--Find next current reference
	self.cur_p = self.parent
	for _,v in pairs(self.spl_dir) do
		local n = self.cur_p:FindFirstChild(v)
		if n ~= nil then
			self.cur_p = n
		else
			self.cur_p = nil
			break
		end
	end

	--Connect if reference was found
	if self.cur_p ~= nil then
		self.cur_p_con = self.cur_p:GetPropertyChangedSignal("Parent"):Connect(function()
			InitialReference(self)
		end)
	end
end

local function GetKey(parent, directory)
	return parent:GetFullName().."\\"..directory
end

local function Register(parent, directory)
	local key = GetKey(parent, directory)
	if cur_ref_reg[key] ~= nil then
		--Increment registration count
		cur_ref_reg[key].count += 1
		return cur_ref_reg[key].instance, true
	else
		--Create new registry
		local self = setmetatable({}, {__index = global_reference})
		cur_ref_reg[key] = {instance = self, count = 1}
		return self, false
	end
end

local function Deregister(parent, directory)
	local key = GetKey(parent, directory)
	if cur_ref_reg[key] ~= nil then
		--Decrement registration count
		cur_ref_reg[key].count -= 1
		if cur_ref_reg[key].count <= 0 then
			--Destroy registry since there's no more references
			cur_ref_reg[key] = nil
			return false
		else
			--Registry hasn't been destroyed yet
			return true
		end
	else
		return true
	end
end
