local ____lualib = require("lualib_bundle")
local __TS__New = ____lualib.__TS__New
local ____exports = {}
local getBackdropPNGPath, spawnWallEntity, spawnSecondWallEntity, spawnFloorEntity, getNumFloorLayers, BackdropKind, DEFAULT_BACKDROP, ROOM_SHAPE_WALL_ANM2_LAYERS, ROOM_SHAPE_WALL_EXTRA_ANM2_LAYERS, WALL_OFFSET, L_FLOOR_ANM2_LAYERS, N_FLOOR_ANM2_LAYERS, BACKDROP_EFFECT_VARIANT, BACKDROP_EFFECT_SUB_TYPE
local ____isaac_2Dtypescript_2Ddefinitions = require("isaac-typescript-definitions")
local EffectVariant = ____isaac_2Dtypescript_2Ddefinitions.EffectVariant
local EntityFlag = ____isaac_2Dtypescript_2Ddefinitions.EntityFlag
local RoomShape = ____isaac_2Dtypescript_2Ddefinitions.RoomShape
local RoomType = ____isaac_2Dtypescript_2Ddefinitions.RoomType
local ____cachedClasses = require("core.cachedClasses")
local game = ____cachedClasses.game
local ____constants = require("core.constants")
local VectorZero = ____constants.VectorZero
local ____LadderSubTypeCustom = require("enums.LadderSubTypeCustom")
local LadderSubTypeCustom = ____LadderSubTypeCustom.LadderSubTypeCustom
local ____array = require("functions.array")
local getRandomArrayElement = ____array.getRandomArrayElement
local ____entitiesSpecific = require("functions.entitiesSpecific")
local spawnEffectWithSeed = ____entitiesSpecific.spawnEffectWithSeed
local ____rng = require("functions.rng")
local newRNG = ____rng.newRNG
local ____roomShape = require("functions.roomShape")
local isLRoomShape = ____roomShape.isLRoomShape
local isNarrowRoom = ____roomShape.isNarrowRoom
local ____string = require("functions.string")
local removeCharactersBefore = ____string.removeCharactersBefore
local trimPrefix = ____string.trimPrefix
local ____utils = require("functions.utils")
local assertDefined = ____utils.assertDefined
local eRange = ____utils.eRange
local iRange = ____utils.iRange
local ____ReadonlySet = require("types.ReadonlySet")
local ReadonlySet = ____ReadonlySet.ReadonlySet
local ____constants = require("classes.features.other.customStages.constants")
local ISAACSCRIPT_CUSTOM_STAGE_GFX_PATH = ____constants.ISAACSCRIPT_CUSTOM_STAGE_GFX_PATH
function getBackdropPNGPath(self, customStage, backdropKind, rng)
    local backdrop = customStage.backdropPNGPaths or DEFAULT_BACKDROP
    local pathArray = backdrop[backdropKind]
    local randomPath = getRandomArrayElement(nil, pathArray, rng)
    return removeCharactersBefore(nil, randomPath, "gfx/")
end
function spawnWallEntity(self, customStage, rng, isExtraWall)
    local room = game:GetRoom()
    local roomShape = room:GetRoomShape()
    local seed = 1
    local wallEffect = spawnEffectWithSeed(
        nil,
        BACKDROP_EFFECT_VARIANT,
        BACKDROP_EFFECT_SUB_TYPE,
        VectorZero,
        seed
    )
    wallEffect:AddEntityFlags(EntityFlag.RENDER_WALL)
    local sprite = wallEffect:GetSprite()
    sprite:Load(ISAACSCRIPT_CUSTOM_STAGE_GFX_PATH .. "/wall-backdrop.anm2", false)
    local wallLayersArray = isExtraWall and ROOM_SHAPE_WALL_EXTRA_ANM2_LAYERS or ROOM_SHAPE_WALL_ANM2_LAYERS
    local numWallLayers = wallLayersArray[roomShape]
    assertDefined(nil, numWallLayers, "Failed to get the layers when creating the backdrop for custom stage: " .. customStage.name)
    if isLRoomShape(nil, roomShape) then
        local cornerPNGPath = getBackdropPNGPath(nil, customStage, BackdropKind.CORNER, rng)
        sprite:ReplaceSpritesheet(0, cornerPNGPath)
    end
    for ____, layerID in ipairs(iRange(nil, 1, numWallLayers)) do
        local wallPNGPath = getBackdropPNGPath(nil, customStage, BackdropKind.WALL, rng)
        sprite:ReplaceSpritesheet(layerID, wallPNGPath)
    end
    local topLeftPos = room:GetTopLeftPos()
    local renderPos = topLeftPos + WALL_OFFSET
    local modifiedOffset = renderPos / 40 * 26
    wallEffect.SpriteOffset = modifiedOffset
    sprite:LoadGraphics()
    local roomShapeName = RoomShape[roomShape]
    local animation = trimPrefix(nil, roomShapeName, "SHAPE_")
    local modifiedAnimation = isExtraWall and animation .. "X" or animation
    sprite:Play(modifiedAnimation, true)
end
function spawnSecondWallEntity(self, customStage, rng)
    local room = game:GetRoom()
    local roomShape = room:GetRoomShape()
    local extraLayers = ROOM_SHAPE_WALL_EXTRA_ANM2_LAYERS[roomShape]
    local roomShapeHasExtraLayers = extraLayers ~= nil
    if roomShapeHasExtraLayers then
        spawnWallEntity(nil, customStage, rng, true)
    end
end
function spawnFloorEntity(self, customStage, rng)
    local room = game:GetRoom()
    local roomShape = room:GetRoomShape()
    local seed = 1
    local floorEffect = spawnEffectWithSeed(
        nil,
        BACKDROP_EFFECT_VARIANT,
        0,
        VectorZero,
        seed
    )
    floorEffect:AddEntityFlags(EntityFlag.RENDER_FLOOR)
    local sprite = floorEffect:GetSprite()
    sprite:Load(ISAACSCRIPT_CUSTOM_STAGE_GFX_PATH .. "/floor-backdrop.anm2", false)
    local numFloorLayers = getNumFloorLayers(nil, roomShape)
    if numFloorLayers ~= nil then
        for ____, layerID in ipairs(eRange(nil, numFloorLayers)) do
            local wallPNGPath = getBackdropPNGPath(nil, customStage, BackdropKind.WALL, rng)
            sprite:ReplaceSpritesheet(layerID, wallPNGPath)
        end
    elseif isLRoomShape(nil, roomShape) then
        for ____, layerID in ipairs(L_FLOOR_ANM2_LAYERS) do
            local LFloorPNGPath = getBackdropPNGPath(nil, customStage, BackdropKind.L_FLOOR, rng)
            sprite:ReplaceSpritesheet(layerID, LFloorPNGPath)
        end
    elseif isNarrowRoom(nil, roomShape) then
        for ____, layerID in ipairs(N_FLOOR_ANM2_LAYERS) do
            local NFloorPNGPath = getBackdropPNGPath(nil, customStage, BackdropKind.N_FLOOR, rng)
            sprite:ReplaceSpritesheet(layerID, NFloorPNGPath)
        end
    end
    local topLeftPos = room:GetTopLeftPos()
    local renderPos = topLeftPos
    local modifiedOffset = renderPos / 40 * 26
    floorEffect.SpriteOffset = modifiedOffset
    sprite:LoadGraphics()
    local roomShapeName = RoomShape[roomShape]
    local animation = trimPrefix(nil, roomShapeName, "SHAPE_")
    sprite:Play(animation, true)
end
function getNumFloorLayers(self, roomShape)
    repeat
        local ____switch22 = roomShape
        local ____cond22 = ____switch22 == RoomShape.SHAPE_1x1
        if ____cond22 then
            do
                return 4
            end
        end
        ____cond22 = ____cond22 or (____switch22 == RoomShape.SHAPE_1x2 or ____switch22 == RoomShape.SHAPE_2x1)
        if ____cond22 then
            do
                return 8
            end
        end
        ____cond22 = ____cond22 or ____switch22 == RoomShape.SHAPE_2x2
        if ____cond22 then
            do
                return 16
            end
        end
        do
            do
                return nil
            end
        end
    until true
end
BackdropKind = {}
BackdropKind.N_FLOOR = "nFloors"
BackdropKind.L_FLOOR = "lFloors"
BackdropKind.WALL = "walls"
BackdropKind.CORNER = "corners"
DEFAULT_BACKDROP = {nFloors = {ISAACSCRIPT_CUSTOM_STAGE_GFX_PATH .. "/backdrop/nfloor.png"}, lFloors = {ISAACSCRIPT_CUSTOM_STAGE_GFX_PATH .. "/backdrop/lfloor.png"}, walls = {ISAACSCRIPT_CUSTOM_STAGE_GFX_PATH .. "/backdrop/wall.png"}, corners = {ISAACSCRIPT_CUSTOM_STAGE_GFX_PATH .. "/backdrop/corner.png"}}
ROOM_SHAPE_WALL_ANM2_LAYERS = {
    [RoomShape.SHAPE_1x1] = 44,
    [RoomShape.IH] = 36,
    [RoomShape.IV] = 28,
    [RoomShape.SHAPE_1x2] = 58,
    [RoomShape.IIV] = 42,
    [RoomShape.SHAPE_2x1] = 63,
    [RoomShape.IIH] = 62,
    [RoomShape.SHAPE_2x2] = 63,
    [RoomShape.LTL] = 63,
    [RoomShape.LTR] = 63,
    [RoomShape.LBL] = 63,
    [RoomShape.LBR] = 63
}
ROOM_SHAPE_WALL_EXTRA_ANM2_LAYERS = {
    [RoomShape.SHAPE_2x1] = 7,
    [RoomShape.SHAPE_2x2] = 21,
    [RoomShape.LTL] = 19,
    [RoomShape.LTR] = 19,
    [RoomShape.LBL] = 19,
    [RoomShape.LBR] = 19
}
WALL_OFFSET = Vector(-80, -80)
L_FLOOR_ANM2_LAYERS = {16, 17}
N_FLOOR_ANM2_LAYERS = {18, 19}
BACKDROP_EFFECT_VARIANT = EffectVariant.LADDER
BACKDROP_EFFECT_SUB_TYPE = LadderSubTypeCustom.CUSTOM_BACKDROP
local BACKDROP_ROOM_TYPE_SET = __TS__New(ReadonlySet, {RoomType.DEFAULT, RoomType.BOSS, RoomType.MINI_BOSS})
function ____exports.setCustomStageBackdrop(self, customStage)
    local room = game:GetRoom()
    local roomType = room:GetType()
    local decorationSeed = room:GetDecorationSeed()
    local rng = newRNG(nil, decorationSeed)
    if not BACKDROP_ROOM_TYPE_SET:has(roomType) then
        return
    end
    spawnWallEntity(nil, customStage, rng, false)
    spawnSecondWallEntity(nil, customStage, rng)
    spawnFloorEntity(nil, customStage, rng)
end
return ____exports
