local _AddonName, _EchoRaidTools = ...;


-- useful function - (minimised version of function by EmptyRivers)
_EchoRaidTools.IterateGroup = function()
    local groupType = IsInRaid() and "raid" or "party"
    local numMembers = groupType == "party" and GetNumSubgroupMembers() or GetNumGroupMembers()
    local i = groupType == "party" and 0 or 1
    return function()
        local ret
        if i == 0 and groupType == "party" then
            ret = "player"
        elseif i <= numMembers and i > 0 then
            ret = groupType .. i
        end
        i = i +  1
        return ret
    end
end

_EchoRaidTools.IsGroupUnit = function(unit)
    if IsInRaid() then
        return unit:sub(1, 4) == "raid"
    else
        return unit == "player" or unit:sub(1, 5) == "party"
    end
end

_EchoRaidTools.DisambiguateUnit = function(unit)
    if not UnitExists(unit) then
        return
    elseif _EchoRaidTools.IsGroupUnit(unit) then
        return unit
    else
        if IsInRaid() then
            return "raid"..UnitInRaid(unit)
        else
            for groupUnit in _EchoRaidTools.IterateGroup() do
                if UnitIsUnit(groupUnit, unit) then
                    return groupUnit
                end
            end
        end
    end
end