"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PostNewRoomEarly = void 0;
const isaac_typescript_definitions_1 = require("isaac-typescript-definitions");
const cachedClasses_1 = require("../../core/cachedClasses");
const gridEntities_1 = require("../../functions/gridEntities");
const log_1 = require("../../functions/log");
const shouldFire_1 = require("../../shouldFire");
const CustomCallback_1 = require("../private/CustomCallback");
class PostNewRoomEarly extends CustomCallback_1.CustomCallback {
    currentRoomTopLeftWallPtrHash = null;
    /** The wall entity directly to the right of the top-left wall. */
    currentRoomTopLeftWallPtrHash2 = null;
    constructor() {
        super();
        this.callbacksUsed = [
            // 19
            // eslint-disable-next-line deprecation/deprecation
            [isaac_typescript_definitions_1.ModCallback.POST_NEW_ROOM, this.postNewRoom],
            // 24
            [isaac_typescript_definitions_1.ModCallback.PRE_ENTITY_SPAWN, this.preEntitySpawn],
        ];
    }
    shouldFire = shouldFire_1.shouldFireRoom;
    // ModCallback.POST_NEW_ROOM (19)
    postNewRoom = () => {
        this.checkRoomChanged();
    };
    // ModCallback.PRE_ENTITY_SPAWN (24)
    preEntitySpawn = () => {
        this.checkRoomChanged();
        return undefined;
    };
    checkRoomChanged() {
        if (this.isNewRoom()) {
            const room = cachedClasses_1.game.GetRoom();
            const roomType = room.GetType();
            this.fire(roomType);
        }
    }
    isNewRoom() {
        const room = cachedClasses_1.game.GetRoom();
        const topLeftWallGridIndex = (0, gridEntities_1.getTopLeftWallGridIndex)();
        const rightOfTopWallGridIndex = topLeftWallGridIndex + 1;
        let topLeftWall = room.GetGridEntity(topLeftWallGridIndex);
        let topLeftWall2 = room.GetGridEntity(rightOfTopWallGridIndex);
        // Sometimes, the `PRE_ENTITY_SPAWN` callback can fire before any grid entities in the room have
        // spawned, which means that the top-left wall will not exist. If ths is the case, then simply
        // spawn the top-left wall early.
        if (topLeftWall === undefined) {
            topLeftWall = (0, gridEntities_1.spawnGridEntity)(isaac_typescript_definitions_1.GridEntityType.WALL, topLeftWallGridIndex);
            if (topLeftWall === undefined) {
                (0, log_1.logError)("Failed to spawn a new wall for the POST_NEW_ROOM_EARLY callback (on the first try).");
                return false;
            }
        }
        // For some reason, the above check will rarely fail. We duplicate the check with another wall
        // segment to increase the reliability.
        if (topLeftWall2 === undefined) {
            topLeftWall2 = (0, gridEntities_1.spawnGridEntity)(isaac_typescript_definitions_1.GridEntityType.WALL, rightOfTopWallGridIndex);
            if (topLeftWall2 === undefined) {
                (0, log_1.logError)("Failed to spawn a new wall for the POST_NEW_ROOM_EARLY callback (on the second try).");
                return false;
            }
        }
        const oldTopLeftWallPtrHash = this.currentRoomTopLeftWallPtrHash;
        const oldTopLeftWallPtrHash2 = this.currentRoomTopLeftWallPtrHash2;
        this.currentRoomTopLeftWallPtrHash = GetPtrHash(topLeftWall);
        this.currentRoomTopLeftWallPtrHash2 = GetPtrHash(topLeftWall2);
        return (oldTopLeftWallPtrHash !== this.currentRoomTopLeftWallPtrHash ||
            oldTopLeftWallPtrHash2 !== this.currentRoomTopLeftWallPtrHash2);
    }
}
exports.PostNewRoomEarly = PostNewRoomEarly;
