local ____lualib = require("lualib_bundle")
local __TS__ParseInt = ____lualib.__TS__ParseInt
local __TS__NumberIsNaN = ____lualib.__TS__NumberIsNaN
local __TS__ObjectKeys = ____lualib.__TS__ObjectKeys
local __TS__ArrayReduce = ____lualib.__TS__ArrayReduce
local __TS__ArrayFilter = ____lualib.__TS__ArrayFilter
local __TS__ArrayForEach = ____lualib.__TS__ArrayForEach
local __TS__ArrayMap = ____lualib.__TS__ArrayMap
local __TS__ArrayJoin = ____lualib.__TS__ArrayJoin
local ____exports = {}
local ____common = require("utils.common")
local isLua = ____common.isLua
--- (Lua-safe) Gets or sets the length of the array. This is a number one higher
-- than the highest index in the array.
____exports.getLength = function(arr) return isLua and __TS__ArrayReduce(
    __TS__ObjectKeys(arr),
    function(____, max, key)
        local keyAsNumber = __TS__ParseInt(key)
        if not __TS__NumberIsNaN(keyAsNumber) then
            return max > keyAsNumber and max or keyAsNumber
        end
        return max
    end,
    0
) or #arr end
--- Removes nils from the array.
____exports.compact = function(arr)
    if not isLua then
        return __TS__ArrayFilter(
            arr,
            function(____, v) return v ~= nil end
        )
    end
    local length = ____exports.getLength(arr)
    local newArr = {}
    do
        local i = 0
        while i < length do
            local val = arr[i + 1]
            if val ~= nil then
                newArr[#newArr + 1] = val
            end
            i = i + 1
        end
    end
    return newArr
end
--- (Lua-safe) Performs the specified action for each element in an array.
____exports.forEach = function(arr, fn)
    if not isLua then
        return __TS__ArrayForEach(
            arr,
            function(____, e, i) return fn(e, i) end
        )
    end
    local length = ____exports.getLength(arr)
    do
        local i = 0
        while i < length do
            fn(arr[i + 1], i)
            i = i + 1
        end
    end
end
--- (Lua-safe) Returns the elements of an array that meet the condition specified
-- in a callback function.
____exports.filter = function(arr, fn)
    if not isLua then
        return __TS__ArrayFilter(
            arr,
            function(____, e, i) return fn(e, i) end
        )
    end
    local length = ____exports.getLength(arr)
    local newArr = {}
    local n = 0
    do
        local i = 0
        while i < length do
            local v = arr[i + 1]
            if fn(v, i) then
                local ____n_0 = n
                n = ____n_0 + 1
                newArr[____n_0 + 1] = v
            end
            i = i + 1
        end
    end
    return newArr
end
--- (Lua-safe) Calls a defined callback function on each element of an array, and
-- returns an array that contains the results.
____exports.map = function(arr, fn)
    if not isLua then
        return __TS__ArrayMap(
            arr,
            function(____, e, i) return fn(e, i) end
        )
    end
    local length = ____exports.getLength(arr)
    local newArr = {}
    do
        local i = 0
        while i < length do
            newArr[i + 1] = fn(arr[i + 1], i)
            i = i + 1
        end
    end
    return newArr
end
--- (Lua-safe) Adds all the elements of an array into a string, separated by the
-- specified separator string.
____exports.join = function(arr, separator)
    if separator == nil then
        separator = ","
    end
    if not isLua then
        return __TS__ArrayJoin(arr, separator)
    end
    local length = ____exports.getLength(arr)
    local strs = {}
    do
        local i = 0
        while i < length do
            strs[#strs + 1] = tostring(arr[i + 1])
            if i < length - 1 and #separator > 0 then
                strs[#strs + 1] = separator
            end
            i = i + 1
        end
    end
    return table.concat(strs, "")
end
return ____exports
