-- DO NOT EDIT --
local points = {"TOP", "BOTTOM", "LEFT", "RIGHT", "CENTER", "TOPLEFT", "TOPRIGHT", "BOTTOMLEFT", "BOTTOMRIGHT"}

local sortBy = aura_env.config.sort
local direction = aura_env.config.grow 
local spacing = aura_env.config.spacing 
local selfPoint = points[aura_env.config.selfPoint]
local anchorPoint = points[aura_env.config.anchorPoint]

local x = direction == 3 and -(aura_env.region.width+spacing) or direction == 4 and  (aura_env.region.width+spacing) or 0
local y = direction == 2 and -(aura_env.region.height+spacing) or direction == 1 and  (aura_env.region.height+spacing) or 0

local units = {}

aura_env.UpdateClone = function(unit, region)
    units[unit] = units[unit] or {}
    local needsAdding = true
    for i = #units[unit],1,-1 do
        if units[unit][i] == region then 
            needsAdding = false 
        end
        if (not units[unit][i].state.show) or (units[unit][i].id ~= aura_env.id) then
            table.remove(units[unit], i)
        end
    end
    if needsAdding then table.insert(units[unit], region) end
    table.sort(units[unit], function(a,b)
            return a.state[sortBy] < b.state[sortBy]
        end
    )
    for i, region in ipairs(units[unit]) do
        region:SetAnchor(selfPoint, C_NamePlate.GetNamePlateForUnit(unit), anchorPoint)
        region:SetOffset(aura_env.config.xOff + (x*(i-1)), aura_env.config.yOff + (y*(i-1)))
    end
end

aura_env.AddClone(aura_env.statee.unit, aura_env.region)

aura_env.UpdateClone(aura_env.statee.unit, aura_env.region)

function()
    if aura_env.statee then
        aura_env.UpdateClone(aura_env.statee.unit, aura_env.region)
    end
end