local ____lualib = require("lualib_bundle")
local __TS__TypeOf = ____lualib.__TS__TypeOf
local __TS__ObjectEntries = ____lualib.__TS__ObjectEntries
local __TS__ObjectValues = ____lualib.__TS__ObjectValues
local __TS__ArrayMap = ____lualib.__TS__ArrayMap
local __TS__ArraySome = ____lualib.__TS__ArraySome
local ____exports = {}
local getSerializedTableType
local ____isaacAPIClassTypeToBrand = require("objects.isaacAPIClassTypeToBrand")
local ISAAC_API_CLASS_TYPE_TO_BRAND = ____isaacAPIClassTypeToBrand.ISAAC_API_CLASS_TYPE_TO_BRAND
local ____isaacAPIClassTypeToFunctions = require("objects.isaacAPIClassTypeToFunctions")
local ISAAC_API_CLASS_TYPE_TO_FUNCTIONS = ____isaacAPIClassTypeToFunctions.ISAAC_API_CLASS_TYPE_TO_FUNCTIONS
local ____isaacAPIClass = require("functions.isaacAPIClass")
local getIsaacAPIClassName = ____isaacAPIClass.getIsaacAPIClassName
local ____types = require("functions.types")
local isTable = ____types.isTable
local isUserdata = ____types.isUserdata
local ____utils = require("functions.utils")
local assertDefined = ____utils.assertDefined
function getSerializedTableType(self, serializedIsaacAPIClass)
    for ____, ____value in ipairs(__TS__ObjectEntries(ISAAC_API_CLASS_TYPE_TO_BRAND)) do
        local copyableIsaacAPIClassType = ____value[1]
        local serializationBrand = ____value[2]
        if serializedIsaacAPIClass[serializationBrand] ~= nil then
            return copyableIsaacAPIClassType
        end
    end
    return nil
end
--- Helper function to generically copy an Isaac API class without knowing what specific type of
-- class it is. (This is used by the save data manager.)
-- 
-- For the list of supported classes, see the `CopyableIsaacAPIClassType` enum.
function ____exports.copyIsaacAPIClass(self, isaacAPIClass)
    if not isUserdata(nil, isaacAPIClass) then
        error("Failed to copy an Isaac API class since the provided object was of type: " .. __TS__TypeOf(isaacAPIClass))
    end
    local isaacAPIClassType = getIsaacAPIClassName(nil, isaacAPIClass)
    assertDefined(nil, isaacAPIClassType, "Failed to copy an Isaac API class since it does not have a class type.")
    local copyableIsaacAPIClassType = isaacAPIClassType
    local functions = ISAAC_API_CLASS_TYPE_TO_FUNCTIONS[copyableIsaacAPIClassType]
    assertDefined(nil, functions, "Failed to copy an Isaac API class since the associated functions were not found for Isaac API class type: " .. copyableIsaacAPIClassType)
    return functions:copy(isaacAPIClass)
end
--- Helper function to generically deserialize an Isaac API class without knowing what specific type
-- of class it is. (This is used by the save data manager when reading data from the "save#.dat"
-- file.)
-- 
-- For the list of supported classes, see the `CopyableIsaacAPIClassType` enum.
function ____exports.deserializeIsaacAPIClass(self, serializedIsaacAPIClass)
    if not isTable(nil, serializedIsaacAPIClass) then
        error("Failed to deserialize an Isaac API class since the provided object was of type: " .. __TS__TypeOf(serializedIsaacAPIClass))
    end
    local copyableIsaacAPIClassType = getSerializedTableType(nil, serializedIsaacAPIClass)
    assertDefined(nil, copyableIsaacAPIClassType, "Failed to deserialize an Isaac API class since a valid class type brand was not found.")
    local functions = ISAAC_API_CLASS_TYPE_TO_FUNCTIONS[copyableIsaacAPIClassType]
    assertDefined(nil, functions, "Failed to deserialize an Isaac API class since the associated functions were not found for class type: " .. copyableIsaacAPIClassType)
    return functions:deserialize(serializedIsaacAPIClass)
end
--- Helper function to generically check if a given object is a copyable Isaac API class. (This is
-- used by the save data manager when determining what is safe to copy.)
-- 
-- For the list of supported classes, see the `CopyableIsaacAPIClassType` enum.
function ____exports.isCopyableIsaacAPIClass(self, object)
    local allFunctions = __TS__ObjectValues(ISAAC_API_CLASS_TYPE_TO_FUNCTIONS)
    local isFunctions = __TS__ArrayMap(
        allFunctions,
        function(____, functions) return functions.is end
    )
    return __TS__ArraySome(
        isFunctions,
        function(____, identityFunction) return identityFunction(nil, object) end
    )
end
--- Helper function to generically check if a given Lua table is a serialized Isaac API class. (This
-- is used by the save data manager when reading data from the "save#.dat" file.)
-- 
-- For the list of supported classes, see the `CopyableIsaacAPIClassType` enum.
function ____exports.isSerializedIsaacAPIClass(self, object)
    local allFunctions = __TS__ObjectValues(ISAAC_API_CLASS_TYPE_TO_FUNCTIONS)
    local isSerializedFunctions = __TS__ArrayMap(
        allFunctions,
        function(____, functions) return functions.isSerialized end
    )
    return __TS__ArraySome(
        isSerializedFunctions,
        function(____, identityFunction) return identityFunction(nil, object) end
    )
end
--- Helper function to generically serialize an Isaac API class without knowing what specific type of
-- class it is. (This is used by the save data manager when writing data to the "save#.dat" file.)
-- 
-- For the list of supported classes, see the `CopyableIsaacAPIClassType` enum.
function ____exports.serializeIsaacAPIClass(self, isaacAPIClass)
    if not isUserdata(nil, isaacAPIClass) then
        error("Failed to serialize an Isaac API class since the provided object was of type: " .. __TS__TypeOf(isaacAPIClass))
    end
    local isaacAPIClassType = getIsaacAPIClassName(nil, isaacAPIClass)
    assertDefined(nil, isaacAPIClassType, "Failed to serialize an Isaac API class since it does not have a class name.")
    local copyableIsaacAPIClassType = isaacAPIClassType
    local functions = ISAAC_API_CLASS_TYPE_TO_FUNCTIONS[copyableIsaacAPIClassType]
    assertDefined(nil, functions, "Failed to serialize an Isaac API class since the associated functions were not found for class type: " .. copyableIsaacAPIClassType)
    return functions:serialize(isaacAPIClass)
end
return ____exports
