local v=require((game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")):WaitForChild("\078\101\118\101\114\109\111\114\101"))local E=v("\066\097\115\101\079\098\106\101\099\116")local F=v("\086\097\108\117\101\079\098\106\101\099\116")local N=v("\066\105\110\100\101\114\085\116\105\108")local Q=setmetatable({},E)Q.ClassName="\066\111\117\110\100\065\110\099\101\115\116\111\114\084\114\097\099\107\101\114"Q.__index=Q function Q.new(v,N)local c=setmetatable(E.new(),Q)c._child=N or error("\078\111 \099\104\105\108\100")c._binder=v or error("\078\111 \098\105\110\100\101\114")c.Class=F.new()c._maid:GiveTask(c.Class)c._maid:GiveTask((c._binder:GetClassRemovingSignal()):Connect(function(v)if v==c.Class.Value then c.Class.Value=nil end end))c._maid:GiveTask((c._binder:GetClassAddedSignal()):Connect(function(v,E)if c._child:IsDescendantOf(E)then c:_update()end end))c._maid:GiveTask(c._child.AncestryChanged:Connect(function()c:_update()end))c:_update()return c end function Q._update(v)local E=v._child.Parent if not E then v.Class.Value=nil return end v.Class.Value=N.findFirstAncestor(v._binder,E)end return Q
--- Tracks a parent bound to a specific binder
-- @classmod BoundAncestorTracker

local require = require(game:GetService("ReplicatedStorage"):WaitForChild("Nevermore"))

local BaseObject = require("BaseObject")
local ValueObject = require("ValueObject")
local BinderUtil = require("BinderUtil")

local BoundAncestorTracker = setmetatable({}, BaseObject)
BoundAncestorTracker.ClassName = "BoundAncestorTracker"
BoundAncestorTracker.__index = BoundAncestorTracker

function BoundAncestorTracker.new(binder, child)
	local self = setmetatable(BaseObject.new(), BoundAncestorTracker)

	self._child = child or error("No child")
	self._binder = binder or error("No binder")

	-- Bound value
	self.Class = ValueObject.new()
	self._maid:GiveTask(self.Class)

	-- Handle instance removing
	self._maid:GiveTask(self._binder:GetClassRemovingSignal():Connect(function(class)
		if class == self.Class.Value then
			self.Class.Value = nil
		end
	end))

	self._maid:GiveTask(self._binder:GetClassAddedSignal():Connect(function(class, instance)
		if self._child:IsDescendantOf(instance) then
			self:_update()
		end
	end))

	-- Perform update
	self._maid:GiveTask(self._child.AncestryChanged:Connect(function()
		self:_update()
	end))
	self:_update()

	return self
end

function BoundAncestorTracker:_update()
	local parent = self._child.Parent
	if not parent then
		self.Class.Value = nil
		return
	end

	self.Class.Value = BinderUtil.findFirstAncestor(self._binder, parent)
end

return BoundAncestorTracker