--[[
	A special key for property tables, which stores a reference to the instance
	in a user-provided Value object.
]]


local Package = script.Parent.Parent

local parseError = require(Package.Dependencies).utility.parseError
local xtypeof = require(Package.ObjectUtility).xtypeof

local Ref = {}
Ref.type = "SpecialKey"
Ref.kind = "Ref"
Ref.stage = "observer"

function Ref:apply(refState: any, applyTo: Instance, cleanupTasks: {})
	if xtypeof(refState) ~= "State" or refState.kind ~= "Value" then
		parseError("invalidRefType")
	else
		refState:set(applyTo)
		
		table.insert(cleanupTasks, function()
			refState:set(nil)
		end)
	end
end

return Ref
