"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.warpNextToRoomType = exports.warpToRoomType = exports.spawnTrapdoorOrCrawlSpace = exports.movePlayer = exports.listGridEntities = exports.listEntities = exports.devilAngel = exports.addHeart = void 0;
const isaac_typescript_definitions_1 = require("isaac-typescript-definitions");
const cachedClasses_1 = require("../../../../core/cachedClasses");
const HealthType_1 = require("../../../../enums/HealthType");
const direction_1 = require("../../../../functions/direction");
const gridEntities_1 = require("../../../../functions/gridEntities");
const levelGrid_1 = require("../../../../functions/levelGrid");
const logEntities_1 = require("../../../../functions/logEntities");
const playerHealth_1 = require("../../../../functions/playerHealth");
const roomData_1 = require("../../../../functions/roomData");
const rooms_1 = require("../../../../functions/rooms");
const types_1 = require("../../../../functions/types");
const roomTypeNames_1 = require("../../../../objects/roomTypeNames");
const DEFAULT_MOVE_UNITS = 0.5;
function addHeart(params, healthType) {
    let numHearts = healthType === HealthType_1.HealthType.MAX_HEARTS ? 2 : 1;
    if (params !== "") {
        const num = (0, types_1.parseIntSafe)(params);
        if (num === undefined) {
            print("That is an invalid amount of hearts to add.");
            return;
        }
        numHearts = num;
    }
    const player = Isaac.GetPlayer();
    (0, playerHealth_1.addPlayerHealthType)(player, healthType, numHearts);
}
exports.addHeart = addHeart;
function devilAngel(useDevil) {
    const level = cachedClasses_1.game.GetLevel();
    const devilAngelRoomData = (0, roomData_1.getRoomData)(isaac_typescript_definitions_1.GridRoom.DEVIL);
    if (devilAngelRoomData !== undefined) {
        const roomType = devilAngelRoomData.Type;
        const conflictingType = useDevil ? isaac_typescript_definitions_1.RoomType.ANGEL : isaac_typescript_definitions_1.RoomType.DEVIL;
        if (roomType === conflictingType) {
            // Delete the room data, which will allow the `Level.InitializeDevilAngelRoom` method to work.
            const roomDescriptor = (0, roomData_1.getRoomDescriptor)(isaac_typescript_definitions_1.GridRoom.DEVIL);
            roomDescriptor.Data = undefined;
        }
    }
    if (useDevil) {
        level.InitializeDevilAngelRoom(false, true);
    }
    else {
        level.InitializeDevilAngelRoom(true, false);
    }
    (0, rooms_1.changeRoom)(isaac_typescript_definitions_1.GridRoom.DEVIL);
}
exports.devilAngel = devilAngel;
function listEntities(params, includeBackgroundEffects) {
    let entityTypeFilter;
    if (params !== "") {
        entityTypeFilter = (0, types_1.parseIntSafe)(params);
        if (entityTypeFilter === undefined) {
            print("That is an invalid entity type to filter by.");
            return;
        }
    }
    (0, logEntities_1.logAllEntities)(includeBackgroundEffects, entityTypeFilter);
    print('Logged the entities in the room to the "log.txt" file.');
}
exports.listEntities = listEntities;
function listGridEntities(params, includeWalls) {
    let gridEntityTypeFilter;
    if (params !== "") {
        gridEntityTypeFilter = (0, types_1.parseIntSafe)(params);
        if (gridEntityTypeFilter === undefined) {
            print("That is an invalid grid entity type to filter by.");
            return;
        }
    }
    (0, logEntities_1.logAllGridEntities)(includeWalls, gridEntityTypeFilter);
    print('Logged the grid entities in the room to the "log.txt" file.');
}
exports.listGridEntities = listGridEntities;
function movePlayer(params, direction) {
    let amount = DEFAULT_MOVE_UNITS;
    if (params !== "") {
        const num = (0, types_1.parseIntSafe)(params);
        if (num === undefined) {
            print("That is an invalid amount of units to move.");
            return;
        }
        amount = num;
    }
    const player = Isaac.GetPlayer();
    const vector = (0, direction_1.directionToVector)(direction);
    const modifiedVector = vector.mul(amount);
    player.Position = player.Position.add(modifiedVector);
}
exports.movePlayer = movePlayer;
function spawnTrapdoorOrCrawlSpace(trapdoor) {
    const room = cachedClasses_1.game.GetRoom();
    const player = Isaac.GetPlayer();
    const position = room.FindFreeTilePosition(player.Position, 0);
    const gridEntityType = trapdoor
        ? isaac_typescript_definitions_1.GridEntityType.TRAPDOOR
        : isaac_typescript_definitions_1.GridEntityType.CRAWL_SPACE;
    (0, gridEntities_1.spawnGridEntity)(gridEntityType, position);
}
exports.spawnTrapdoorOrCrawlSpace = spawnTrapdoorOrCrawlSpace;
function warpToRoomType(roomType) {
    const roomTypeName = roomTypeNames_1.ROOM_TYPE_NAMES[roomType];
    const gridIndexes = (0, levelGrid_1.getRoomGridIndexesForType)(roomType);
    const firstGridIndex = gridIndexes[0];
    if (firstGridIndex === undefined) {
        print(`There are no ${roomTypeName}s on this floor.`);
        return;
    }
    (0, rooms_1.changeRoom)(firstGridIndex);
    print(`Warped to room type: ${roomTypeName} (${roomType})`);
}
exports.warpToRoomType = warpToRoomType;
function warpNextToRoomType(roomType) {
    const roomTypeName = roomTypeNames_1.ROOM_TYPE_NAMES[roomType];
    const gridIndexes = (0, levelGrid_1.getRoomGridIndexesForType)(roomType);
    const firstGridIndex = gridIndexes[0];
    if (firstGridIndex === undefined) {
        print(`There are no ${roomTypeName}s on this floor.`);
        return;
    }
    const adjacentRoomGridIndexes = (0, levelGrid_1.getRoomAdjacentGridIndexes)(firstGridIndex);
    for (const [_doorSlot, roomGridIndex] of adjacentRoomGridIndexes) {
        const roomData = (0, roomData_1.getRoomData)(roomGridIndex);
        if (roomData !== undefined && roomData.Type === isaac_typescript_definitions_1.RoomType.DEFAULT) {
            (0, rooms_1.changeRoom)(roomGridIndex);
            print(`Warped next to room type: ${roomTypeName} (${roomType})`);
            return;
        }
    }
    print(`Failed to find the room next to room type: ${roomTypeName} (${roomType})`);
}
exports.warpNextToRoomType = warpNextToRoomType;
