"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StageHistory = void 0;
const isaac_typescript_definitions_1 = require("isaac-typescript-definitions");
const cachedClasses_1 = require("../../../core/cachedClasses");
const decorators_1 = require("../../../decorators");
const ModCallbackCustom_1 = require("../../../enums/ModCallbackCustom");
const nextStage_1 = require("../../../functions/nextStage");
const stage_1 = require("../../../functions/stage");
const Feature_1 = require("../../private/Feature");
const v = {
    run: {
        stageHistory: [],
    },
};
class StageHistory extends Feature_1.Feature {
    /** @internal */
    v = v;
    /** @internal */
    constructor() {
        super();
        this.customCallbacksUsed = [
            [ModCallbackCustom_1.ModCallbackCustom.POST_NEW_LEVEL_REORDERED, this.postNewLevelReordered],
        ];
    }
    // ModCallbackCustom.POST_NEW_LEVEL_REORDERED
    postNewLevelReordered = () => {
        const level = cachedClasses_1.game.GetLevel();
        const stage = level.GetStage();
        const stageType = level.GetStageType();
        v.run.stageHistory.push({ stage, stageType });
    };
    /**
     * Helper function to get the stage type that a trapdoor or heaven door would take the player to,
     * based on the current stage, room, and game state flags.
     *
     * This function accounts for the previous floors that a player has visited thus far on the run so
     * that the next stage type can be properly calculated on The Ascent (which makes it unlike the
     * `getNextStageType` function).
     *
     * In order to use this function, you must upgrade your mod with `ISCFeature.STAGE_HISTORY`.
     *
     * @param upwards Whether the player should go up to Cathedral in the case of being on Womb 2.
     *                Default is false.
     * @public
     */
    getNextStageTypeWithHistory(upwards = false) {
        const backwardsPath = cachedClasses_1.game.GetStateFlag(isaac_typescript_definitions_1.GameStateFlag.BACKWARDS_PATH);
        if (!backwardsPath) {
            return (0, nextStage_1.getNextStageType)(upwards);
        }
        const level = cachedClasses_1.game.GetLevel();
        const stage = level.GetStage();
        const repentanceStage = (0, stage_1.onRepentanceStage)();
        const visitedDownpour1 = this.hasVisitedStage(isaac_typescript_definitions_1.LevelStage.BASEMENT_1, isaac_typescript_definitions_1.StageType.REPENTANCE);
        const visitedDross1 = this.hasVisitedStage(isaac_typescript_definitions_1.LevelStage.BASEMENT_1, isaac_typescript_definitions_1.StageType.REPENTANCE_B);
        const visitedDownpour2 = this.hasVisitedStage(isaac_typescript_definitions_1.LevelStage.BASEMENT_2, isaac_typescript_definitions_1.StageType.REPENTANCE);
        const visitedDross2 = this.hasVisitedStage(isaac_typescript_definitions_1.LevelStage.BASEMENT_2, isaac_typescript_definitions_1.StageType.REPENTANCE_B);
        const visitedMines1 = this.hasVisitedStage(isaac_typescript_definitions_1.LevelStage.CAVES_1, isaac_typescript_definitions_1.StageType.REPENTANCE);
        const visitedAshpit1 = this.hasVisitedStage(isaac_typescript_definitions_1.LevelStage.CAVES_1, isaac_typescript_definitions_1.StageType.REPENTANCE_B);
        const visitedMines2 = this.hasVisitedStage(isaac_typescript_definitions_1.LevelStage.DEPTHS_2, isaac_typescript_definitions_1.StageType.REPENTANCE);
        const visitedAshpit2 = this.hasVisitedStage(isaac_typescript_definitions_1.LevelStage.DEPTHS_2, isaac_typescript_definitions_1.StageType.REPENTANCE_B);
        if (stage === isaac_typescript_definitions_1.LevelStage.BASEMENT_2 && repentanceStage) {
            if (visitedDownpour1) {
                return isaac_typescript_definitions_1.StageType.REPENTANCE;
            }
            if (visitedDross1) {
                return isaac_typescript_definitions_1.StageType.REPENTANCE_B;
            }
        }
        if (stage === isaac_typescript_definitions_1.LevelStage.CAVES_1 && repentanceStage) {
            if (visitedDownpour2) {
                return isaac_typescript_definitions_1.StageType.REPENTANCE;
            }
            if (visitedDross2) {
                return isaac_typescript_definitions_1.StageType.REPENTANCE_B;
            }
        }
        if (stage === isaac_typescript_definitions_1.LevelStage.CAVES_2 && !repentanceStage) {
            if (visitedDownpour2) {
                return isaac_typescript_definitions_1.StageType.REPENTANCE;
            }
            if (visitedDross2) {
                return isaac_typescript_definitions_1.StageType.REPENTANCE_B;
            }
        }
        if (stage === isaac_typescript_definitions_1.LevelStage.CAVES_2 && repentanceStage) {
            if (visitedMines1) {
                return isaac_typescript_definitions_1.StageType.REPENTANCE;
            }
            if (visitedAshpit1) {
                return isaac_typescript_definitions_1.StageType.REPENTANCE_B;
            }
        }
        if (stage === isaac_typescript_definitions_1.LevelStage.DEPTHS_2 && !repentanceStage) {
            if (visitedAshpit2) {
                return isaac_typescript_definitions_1.StageType.REPENTANCE_B;
            }
            if (visitedMines2) {
                return isaac_typescript_definitions_1.StageType.REPENTANCE;
            }
        }
        const nextStage = this.getNextStageWithHistory();
        return (0, stage_1.calculateStageType)(nextStage);
    }
    /**
     * Helper function to get the stage that a trapdoor or heaven door would take the player to, based
     * on the current stage, room, and game state flags.
     *
     * This function accounts for the previous floors that a player has visited thus far on the run so
     * that the next stage can be properly calculated on The Ascent (which makes it unlike the
     * `getNextStage` function).
     *
     * In order to use this function, you must upgrade your mod with `ISCFeature.STAGE_HISTORY`.
     */
    getNextStageWithHistory() {
        const backwardsPath = cachedClasses_1.game.GetStateFlag(isaac_typescript_definitions_1.GameStateFlag.BACKWARDS_PATH);
        if (!backwardsPath) {
            return (0, nextStage_1.getNextStage)();
        }
        const level = cachedClasses_1.game.GetLevel();
        const stage = level.GetStage();
        const repentanceStage = (0, stage_1.onRepentanceStage)();
        const visitedDownpour1 = this.hasVisitedStage(isaac_typescript_definitions_1.LevelStage.BASEMENT_1, isaac_typescript_definitions_1.StageType.REPENTANCE);
        const visitedDross1 = this.hasVisitedStage(isaac_typescript_definitions_1.LevelStage.BASEMENT_1, isaac_typescript_definitions_1.StageType.REPENTANCE_B);
        const visitedDownpour2 = this.hasVisitedStage(isaac_typescript_definitions_1.LevelStage.BASEMENT_2, isaac_typescript_definitions_1.StageType.REPENTANCE);
        const visitedDross2 = this.hasVisitedStage(isaac_typescript_definitions_1.LevelStage.BASEMENT_2, isaac_typescript_definitions_1.StageType.REPENTANCE_B);
        const visitedMines1 = this.hasVisitedStage(isaac_typescript_definitions_1.LevelStage.CAVES_1, isaac_typescript_definitions_1.StageType.REPENTANCE);
        const visitedAshpit1 = this.hasVisitedStage(isaac_typescript_definitions_1.LevelStage.CAVES_1, isaac_typescript_definitions_1.StageType.REPENTANCE_B);
        const visitedMines2 = this.hasVisitedStage(isaac_typescript_definitions_1.LevelStage.DEPTHS_2, isaac_typescript_definitions_1.StageType.REPENTANCE);
        const visitedAshpit2 = this.hasVisitedStage(isaac_typescript_definitions_1.LevelStage.DEPTHS_2, isaac_typescript_definitions_1.StageType.REPENTANCE_B);
        if (stage === isaac_typescript_definitions_1.LevelStage.BASEMENT_1) {
            if (repentanceStage) {
                // From Downpour 1 to Basement 1.
                return isaac_typescript_definitions_1.LevelStage.BASEMENT_1;
            }
            // From Basement 1 to Home.
            return isaac_typescript_definitions_1.LevelStage.HOME;
        }
        if (stage === isaac_typescript_definitions_1.LevelStage.BASEMENT_2) {
            if (repentanceStage) {
                if (visitedDownpour1 || visitedDross1) {
                    // From Downpour 2 to Downpour 1.
                    return isaac_typescript_definitions_1.LevelStage.BASEMENT_1;
                }
                // From Downpour 2 to Basement 2.
                return isaac_typescript_definitions_1.LevelStage.BASEMENT_2;
            }
            // From Basement 2 to Basement 1.
            return isaac_typescript_definitions_1.LevelStage.BASEMENT_1;
        }
        if (stage === isaac_typescript_definitions_1.LevelStage.CAVES_1) {
            if (repentanceStage) {
                if (visitedDownpour2 || visitedDross2) {
                    // From Mines 1 to Downpour 1.
                    return isaac_typescript_definitions_1.LevelStage.BASEMENT_2;
                }
                // From Mines 1 to Caves 1.
                return isaac_typescript_definitions_1.LevelStage.CAVES_1;
            }
            // From Caves 1 to Basement 2.
            return isaac_typescript_definitions_1.LevelStage.BASEMENT_2;
        }
        if (stage === isaac_typescript_definitions_1.LevelStage.CAVES_2) {
            if (repentanceStage) {
                if (visitedMines1 || visitedAshpit1) {
                    // From Mines 2 to Mines 1.
                    return isaac_typescript_definitions_1.LevelStage.CAVES_1;
                }
                // From Mines 2 to Caves 2.
                return isaac_typescript_definitions_1.LevelStage.CAVES_2;
            }
            // From Caves 2 to Caves 1.
            return isaac_typescript_definitions_1.LevelStage.CAVES_1;
        }
        if (stage === isaac_typescript_definitions_1.LevelStage.DEPTHS_1) {
            if (repentanceStage) {
                if (visitedMines2 || visitedAshpit2) {
                    // From Mausoleum 1 to Mines 2.
                    return isaac_typescript_definitions_1.LevelStage.CAVES_2;
                }
                // From Mausoleum 1 to Depths 1.
                return isaac_typescript_definitions_1.LevelStage.DEPTHS_1;
            }
            // From Depths 1 to Caves 2.
            return isaac_typescript_definitions_1.LevelStage.CAVES_2;
        }
        if (stage === isaac_typescript_definitions_1.LevelStage.DEPTHS_2) {
            if (repentanceStage) {
                // From Mausoleum 2 to Depths 2.
                return isaac_typescript_definitions_1.LevelStage.DEPTHS_2;
            }
            // From Depths 2 to Depths 1.
            return isaac_typescript_definitions_1.LevelStage.DEPTHS_1;
        }
        return stage - 1;
    }
    /**
     * Helper function to get all of the stages that a player has visited thus far on this run.
     *
     * In order to use this function, you must upgrade your mod with `ISCFeature.STAGE_HISTORY`.
     *
     * @public
     */
    getStageHistory() {
        return v.run.stageHistory;
    }
    /**
     * Helper function to check if a player has previous visited a particular stage (or stage + stage
     * type combination) on this run.
     *
     * In order to use this function, you must upgrade your mod with `ISCFeature.STAGE_HISTORY`.
     *
     * @param stage The stage to check for.
     * @param stageType Optional. If provided, will check for a specific stage and stage type
     *                  combination.
     */
    hasVisitedStage(stage, stageType) {
        if (stageType === undefined) {
            return v.run.stageHistory.some((stageHistoryEntry) => stageHistoryEntry.stage === stage);
        }
        return v.run.stageHistory.some((stageHistoryEntry) => stageHistoryEntry.stage === stage &&
            stageHistoryEntry.stageType === stageType);
    }
}
exports.StageHistory = StageHistory;
__decorate([
    decorators_1.Exported
], StageHistory.prototype, "getNextStageTypeWithHistory", null);
__decorate([
    decorators_1.Exported
], StageHistory.prototype, "getNextStageWithHistory", null);
__decorate([
    decorators_1.Exported
], StageHistory.prototype, "getStageHistory", null);
__decorate([
    decorators_1.Exported
], StageHistory.prototype, "hasVisitedStage", null);
