"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.RerunDetection = void 0;
const decorators_1 = require("../../../decorators");
const ModCallbackCustom_1 = require("../../../enums/ModCallbackCustom");
const rooms_1 = require("../../../functions/rooms");
const stage_1 = require("../../../functions/stage");
const Feature_1 = require("../../private/Feature");
const v = {
    // We cannot use a "run" object since the variables would be reset when a rerun starts.
    persistent: {
        pastFirstFloor: false,
        onRerun: false,
    },
};
class RerunDetection extends Feature_1.Feature {
    /** @internal */
    v = v;
    /** @internal */
    constructor() {
        super();
        this.customCallbacksUsed = [
            [
                ModCallbackCustom_1.ModCallbackCustom.POST_GAME_STARTED_REORDERED,
                this.postGameStartedReordered,
            ],
            [ModCallbackCustom_1.ModCallbackCustom.POST_NEW_LEVEL_REORDERED, this.postNewLevelReordered],
        ];
    }
    // ModCallbackCustom.POST_GAME_STARTED_REORDERED
    postGameStartedReordered = (isContinued) => {
        if (isContinued) {
            if ((0, stage_1.onFirstFloor)() && (0, rooms_1.inStartingRoom)() && v.persistent.pastFirstFloor) {
                v.persistent.onRerun = true;
            }
        }
        else {
            v.persistent.onRerun = false;
        }
    };
    // ModCallbackCustom.POST_NEW_LEVEL_REORDERED
    postNewLevelReordered = () => {
        v.persistent.pastFirstFloor = !(0, stage_1.onFirstFloor)();
    };
    /**
     * Helper function to detect if the current run was starting using the "Rerun" option from the
     * main menu.
     *
     * Under the hood, this assumes that any run that is past the first floor and continues in the
     * starting room of the run is a rerun.
     *
     * In order to use this function, you must upgrade your mod with `ISCFeature.RERUN_DETECTION`.
     *
     * @public
     */
    onRerun() {
        return v.persistent.onRerun;
    }
}
exports.RerunDetection = RerunDetection;
__decorate([
    decorators_1.Exported
], RerunDetection.prototype, "onRerun", null);
