-- Tracks which RLM animal records are currently being represented by a
-- walker (mid-transit between husbandries). RLM's render tick consults this
-- (via our setClusters hook) and skips visuals for any in-transit animal —
-- so during a walk you only see the walker, not the husbandry-spawned cow
-- at either end.
--
-- Keyed by the animal RECORD TABLE. RLM keeps the same table across a
-- barn<->pasture move (only rebinding its .id), so the table reference is a
-- stable identity for the whole trip. (An earlier animal.id string index +
-- refreshIdIndex existed as a "rekey fallback", but the walker's
-- hideSourceVisual nulls animal.id BEFORE we mark transit, so it was never
-- populated — dead code, removed.)

TransitTracker = {}
local TransitTracker_mt = Class(TransitTracker)

function TransitTracker.new()
    local self = setmetatable({}, TransitTracker_mt)
    self.byAnimal = {}        -- animal table reference -> walker
    return self
end

function TransitTracker:markInTransit(animal, walker)
    if animal == nil then return end
    self.byAnimal[animal] = walker
end

function TransitTracker:clearInTransit(animal)
    if animal == nil then return end
    self.byAnimal[animal] = nil
end

-- Fast: is the animal currently flagged in transit?
function TransitTracker:isInTransit(animal)
    if animal == nil then return false end
    return self.byAnimal[animal] ~= nil
end

function TransitTracker:count()
    local n = 0
    for _ in pairs(self.byAnimal) do n = n + 1 end
    return n
end

function TransitTracker:describe()
    local lines = { string.format("in-transit animals: %d", self:count()) }
    for animal, walker in pairs(self.byAnimal) do
        lines[#lines + 1] = string.format("  animal id=%s -> walker=%s",
            tostring(animal.id), tostring(walker))
    end
    return table.concat(lines, "\n")
end
