local ____exports = {}
local ____types = require("functions.types")
local isNumber = ____types.isNumber
--- Helper function to get the name and the line number of the current calling function.
-- 
-- For this function to work properly, the "--luadebug" flag must be enabled. Otherwise, it will
-- always return undefined.
-- 
-- @param levels Optional. The amount of levels to look backwards in the call stack. Default is 3
-- (because the first level is this function, the second level is the calling
-- function, and the third level is the parent of the calling function).
function ____exports.getParentFunctionDescription(levels)
    if levels == nil then
        levels = 3
    end
    if debug ~= nil then
        local debugTable = debug.getinfo(levels)
        if debugTable ~= nil then
            return (tostring(debugTable.name) .. ":") .. tostring(debugTable.linedefined)
        end
    end
    if SandboxGetParentFunctionDescription ~= nil then
        return SandboxGetParentFunctionDescription(levels)
    end
    return nil
end
--- Helper function to avoid typing out `Isaac.DebugString()`.
-- 
-- If you have the "--luadebug" launch flag turned on, then this function will also prepend the
-- function name and the line number before the string, like this:
-- 
-- ```text
-- [INFO] - Lua Debug: saveToDisk:42494 - The save data manager wrote data to the "save#.dat" file.
-- ```
-- 
-- Subsequently, it is recommended that you turn on the "--luadebug" launch flag when developing
-- your mod so that debugging becomes a little bit easier.
-- 
-- @param msg The message to log.
-- @param includeParentFunction Optional. Whether to prefix the message with the function name and
-- line number, as shown in the above example. Default is true.
function ____exports.log(msg, includeParentFunction)
    if includeParentFunction == nil then
        includeParentFunction = true
    end
    if isNumber(nil, msg) then
        msg = tostring(msg)
    end
    local ____includeParentFunction_0
    if includeParentFunction then
        ____includeParentFunction_0 = ____exports.getParentFunctionDescription()
    else
        ____includeParentFunction_0 = nil
    end
    local parentFunctionDescription = ____includeParentFunction_0
    local debugMsg = parentFunctionDescription == nil and msg or (parentFunctionDescription .. " - ") .. msg
    Isaac.DebugString(debugMsg)
end
--- Helper function to log a message to the "log.txt" file and to print it to the screen at the same
-- time.
function ____exports.logAndPrint(self, msg)
    ____exports.log(msg)
    print(msg)
end
--- Helper function to log an error message and also print it to the console for better visibility.
-- 
-- This is useful in situations where using the `error` function would be dangerous (since it
-- prevents all of the subsequent code in the callback from running).
function ____exports.logError(msg)
    local errorMsg = "Error: " .. msg
    ____exports.logAndPrint(nil, errorMsg)
end
return ____exports
