local ____lualib = require("lualib_bundle")
local __TS__StringSplit = ____lualib.__TS__StringSplit
local __TS__StringIncludes = ____lualib.__TS__StringIncludes
local __TS__ArrayFilter = ____lualib.__TS__ArrayFilter
local ____exports = {}
local errorWithTraceback, slimTracebackOutput, removeLinesContaining, LINE_SEPARATOR, USELESS_TRACEBACK_MESSAGES, vanillaError
local ____debugFunctions = require("functions.debugFunctions")
local getTraceback = ____debugFunctions.getTraceback
local isLuaDebugEnabled = ____debugFunctions.isLuaDebugEnabled
function errorWithTraceback(message, level)
    if level == nil then
        level = 1
    end
    if vanillaError == nil then
        error(message, level)
    end
    local tracebackOutput = getTraceback()
    local slimmedTracebackOutput = slimTracebackOutput(nil, tracebackOutput)
    message = message .. "\n"
    message = message .. slimmedTracebackOutput
    return vanillaError(message, level + 1)
end
function slimTracebackOutput(self, tracebackOutput)
    for ____, msg in ipairs(USELESS_TRACEBACK_MESSAGES) do
        tracebackOutput = removeLinesContaining(nil, tracebackOutput, msg)
    end
    return tracebackOutput
end
function removeLinesContaining(self, msg, containsMsg)
    local lines = __TS__StringSplit(msg, LINE_SEPARATOR)
    local linesThatDontContain = __TS__ArrayFilter(
        lines,
        function(____, line) return not __TS__StringIncludes(line, containsMsg) end
    )
    return table.concat(linesThatDontContain, LINE_SEPARATOR or ",")
end
LINE_SEPARATOR = "\n"
USELESS_TRACEBACK_MESSAGES = {"in upvalue 'getTraceback'", "in function 'sandbox.GetTraceback'", "in function 'error'"}
--- In Lua, the `error` function will tell you the line number of the error, but not give you a full
-- traceback of the parent functions, which is unlike how JavaScript works. This function monkey
-- patches the `error` function to add this functionality.
-- 
-- Traceback functionality can only be added if the "--luadebug" flag is turned on, so this function
-- does nothing if the "--luadebug" flag is disabled.
function ____exports.patchErrorFunction(self)
    if not isLuaDebugEnabled(nil) then
        return
    end
    if __PATCHED_ERROR ~= nil then
        return
    end
    __PATCHED_ERROR = true
    vanillaError = error
    error = errorWithTraceback
end
return ____exports
