local ____lualib = require("lualib_bundle")
local Map = ____lualib.Map
local __TS__New = ____lualib.__TS__New
local __TS__Iterator = ____lualib.__TS__Iterator
local ____exports = {}
local ____isaac_2Dtypescript_2Ddefinitions = require("isaac-typescript-definitions")
local DisplayFlagZero = ____isaac_2Dtypescript_2Ddefinitions.DisplayFlagZero
local ____cachedClasses = require("core.cachedClasses")
local game = ____cachedClasses.game
local ____flag = require("functions.flag")
local addFlag = ____flag.addFlag
local ____roomData = require("functions.roomData")
local getRoomDescriptor = ____roomData.getRoomDescriptor
local getRoomGridIndex = ____roomData.getRoomGridIndex
local ____rooms = require("functions.rooms")
local getRoomsInsideGrid = ____rooms.getRoomsInsideGrid
local ____types = require("functions.types")
local isInteger = ____types.isInteger
local ____utils = require("functions.utils")
local assertDefined = ____utils.assertDefined
--- Helper function to get a particular room's minimap display flags (e.g. whether it is visible and
-- so on).
-- 
-- This function automatically accounts for if MinimapAPI is being used.
-- 
-- @param roomGridIndex Optional. Default is the current room index.
-- @param minimapAPI Optional. If MinimapAPI should be used, if present. Default is true.
function ____exports.getRoomDisplayFlags(self, roomGridIndex, minimapAPI)
    if minimapAPI == nil then
        minimapAPI = true
    end
    if roomGridIndex == nil then
        roomGridIndex = getRoomGridIndex(nil)
    end
    if MinimapAPI == nil or not minimapAPI then
        local roomDescriptor = getRoomDescriptor(nil, roomGridIndex)
        return roomDescriptor.DisplayFlags
    end
    local minimapAPIRoomDescriptor = MinimapAPI:GetRoomByIdx(roomGridIndex)
    assertDefined(
        nil,
        minimapAPIRoomDescriptor,
        "Failed to get the MinimapAPI room descriptor for the room at grid index: " .. tostring(roomGridIndex)
    )
    return minimapAPIRoomDescriptor:GetDisplayFlags()
end
--- Helper function to set the minimap `DisplayFlag` value for every room on the floor at once.
-- 
-- This function automatically calls the `Level.UpdateVisibility` after setting the flags so that
-- the changes will be immediately visible.
-- 
-- This function automatically accounts for if MinimapAPI is being used.
function ____exports.setAllDisplayFlags(self, displayFlags)
    for ____, room in ipairs(getRoomsInsideGrid(nil)) do
        ____exports.setRoomDisplayFlags(nil, room.SafeGridIndex, displayFlags, false)
    end
    if MinimapAPI == nil then
        local level = game:GetLevel()
        level:UpdateVisibility()
    end
end
--- Helper function to set a particular room's minimap display flags (e.g. whether it is visible and
-- so on).
-- 
-- This function automatically accounts for if MinimapAPI is being used.
-- 
-- @param roomGridIndex Set to undefined to use the current room index.
-- @param displayFlags The bit flags value to set. (See the `DisplayFlag` enum.)
-- @param updateVisibility Optional. Whether to call the `Level.UpdateVisibility` method in order to
-- make the changes immediately visible. Default is true. Set this to false
-- if you are doing a bunch of display flag setting and then manually call
-- the `Level.UpdateVisibility` method after you are done.
function ____exports.setRoomDisplayFlags(self, roomGridIndex, displayFlags, updateVisibility)
    if updateVisibility == nil then
        updateVisibility = true
    end
    if roomGridIndex == nil then
        roomGridIndex = getRoomGridIndex(nil)
    end
    if MinimapAPI == nil then
        local roomDescriptor = getRoomDescriptor(nil, roomGridIndex)
        roomDescriptor.DisplayFlags = displayFlags
        if updateVisibility then
            local level = game:GetLevel()
            level:UpdateVisibility()
        end
    else
        local minimapAPIRoomDescriptor = MinimapAPI:GetRoomByIdx(roomGridIndex)
        assertDefined(
            nil,
            minimapAPIRoomDescriptor,
            "Failed to get the MinimapAPI room descriptor for the room at grid index: " .. tostring(roomGridIndex)
        )
        minimapAPIRoomDescriptor:SetDisplayFlags(displayFlags)
    end
end
--- Helper function to add a `DisplayFlag` to a particular room's minimap display flags (e.g. whether
-- it is visible and so on).
-- 
-- This function automatically accounts for if MinimapAPI is being used.
-- 
-- @param roomGridIndex Set to undefined to use the current room index.
-- @param displayFlag The `DisplayFlag` to set. (See the `DisplayFlag` enum.)
-- @param updateVisibility Optional. Whether to call the `Level.UpdateVisibility` method in order to
-- make the changes immediately visible. Default is true.
function ____exports.addRoomDisplayFlag(self, roomGridIndex, displayFlag, updateVisibility)
    if updateVisibility == nil then
        updateVisibility = true
    end
    local oldDisplayFlags = ____exports.getRoomDisplayFlags(nil, roomGridIndex)
    local newDisplayFlags = addFlag(nil, oldDisplayFlags, displayFlag)
    ____exports.setRoomDisplayFlags(nil, roomGridIndex, newDisplayFlags, updateVisibility)
end
--- Helper function to set the value of `DisplayFlag` for every room on the floor to 0.
-- 
-- This function automatically accounts for if MinimapAPI is being used.
-- 
-- This function automatically calls the `Level.UpdateVisibility` after setting the flags so that
-- the changes will be immediately visible.
function ____exports.clearFloorDisplayFlags(self)
    ____exports.setAllDisplayFlags(nil, DisplayFlagZero)
end
--- Helper function to set the value of `DisplayFlag` for a room 0.
-- 
-- This function automatically accounts for if MinimapAPI is being used.
-- 
-- This function automatically calls the `Level.UpdateVisibility` after setting the flags so that
-- the changes will be immediately visible.
-- 
-- Note that if you clear the display flags of a room but then the player travels to the room (or an
-- adjacent room), the room will appear on the minimap again. If you want to permanently hide the
-- room even in this circumstance, use the `hideRoomOnMinimap` helper function instead.
function ____exports.clearRoomDisplayFlags(self, roomGridIndex)
    ____exports.setRoomDisplayFlags(nil, roomGridIndex, DisplayFlagZero)
end
--- Helper function to get the minimap `DisplayFlag` value for every room on the floor. Returns a map
-- that is indexed by the room's safe grid index.
-- 
-- This function automatically accounts for if MinimapAPI is being used.
-- 
-- @param minimapAPI Optional. If MinimapAPI should be used, if present. Default is true.
function ____exports.getFloorDisplayFlags(self, minimapAPI)
    if minimapAPI == nil then
        minimapAPI = true
    end
    local displayFlagsMap = __TS__New(Map)
    for ____, roomDescriptor in ipairs(getRoomsInsideGrid(nil)) do
        local roomGridIndex = roomDescriptor.SafeGridIndex
        local displayFlags = ____exports.getRoomDisplayFlags(nil, roomGridIndex, minimapAPI)
        displayFlagsMap:set(roomGridIndex, displayFlags)
    end
    return displayFlagsMap
end
--- Helper function to hide a specific room on the minimap.
-- 
-- If you want the room to be permanently hidden, you must to call this function on every new room.
-- This is because if the player enters into the room or walks into an adjacent room, the room will
-- reappear on the minimap.
-- 
-- This function automatically accounts for if MinimapAPI is being used.
function ____exports.hideRoomOnMinimap(self, roomGridIndex)
    ____exports.clearRoomDisplayFlags(nil, roomGridIndex)
    if MinimapAPI ~= nil then
        local minimapAPIRoomDescriptor = MinimapAPI:GetRoomByIdx(roomGridIndex)
        assertDefined(
            nil,
            minimapAPIRoomDescriptor,
            "Failed to get the MinimapAPI room descriptor for the room at grid index: " .. tostring(roomGridIndex)
        )
        minimapAPIRoomDescriptor.Hidden = true
    end
end
--- Helper function to check if a given room is visible on the minimap.
-- 
-- @param roomGridIndexOrRoomDescriptor The room to check.
-- @param minimapAPI Optional. Whether MinimapAPI should be used, if present. Default is true.
function ____exports.isRoomVisible(self, roomGridIndexOrRoomDescriptor, minimapAPI)
    if minimapAPI == nil then
        minimapAPI = true
    end
    local roomGridIndex = isInteger(nil, roomGridIndexOrRoomDescriptor) and roomGridIndexOrRoomDescriptor or roomGridIndexOrRoomDescriptor.SafeGridIndex
    local roomDisplayFlags = ____exports.getRoomDisplayFlags(nil, roomGridIndex, minimapAPI)
    return roomDisplayFlags ~= DisplayFlagZero
end
--- Helper function to set the minimap `DisplayFlag` value for multiple rooms at once.
-- 
-- This function automatically calls the `Level.UpdateVisibility` after setting the flags so that
-- the changes will be immediately visible.
-- 
-- This function automatically accounts for if MinimapAPI is being used.
-- 
-- @param displayFlagsMap A map of the display flags that is indexed by the room's safe grid index.
function ____exports.setFloorDisplayFlags(self, displayFlagsMap)
    for ____, ____value in __TS__Iterator(displayFlagsMap) do
        local roomGridIndex = ____value[1]
        local displayFlags = ____value[2]
        ____exports.setRoomDisplayFlags(nil, roomGridIndex, displayFlags, false)
    end
    if MinimapAPI == nil then
        local level = game:GetLevel()
        level:UpdateVisibility()
    end
end
return ____exports
