"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.PonyDetection = void 0;
const isaac_typescript_definitions_1 = require("isaac-typescript-definitions");
const decorators_1 = require("../../../decorators");
const ModCallbackCustom_1 = require("../../../enums/ModCallbackCustom");
const flag_1 = require("../../../functions/flag");
const playerDataStructures_1 = require("../../../functions/playerDataStructures");
const playerIndex_1 = require("../../../functions/playerIndex");
const Feature_1 = require("../../private/Feature");
const FLAGS_WHEN_PONY_IS_ACTIVE = [
    isaac_typescript_definitions_1.EntityFlag.NO_KNOCKBACK,
    isaac_typescript_definitions_1.EntityFlag.NO_PHYSICS_KNOCKBACK,
    isaac_typescript_definitions_1.EntityFlag.NO_DAMAGE_BLINK, // 1 << 36
];
const v = {
    run: {
        playersIsPonyActive: new Set(),
    },
};
class PonyDetection extends Feature_1.Feature {
    /** @internal */
    v = v;
    /** @internal */
    constructor() {
        super();
        this.customCallbacksUsed = [
            [
                ModCallbackCustom_1.ModCallbackCustom.POST_PEFFECT_UPDATE_REORDERED,
                this.postPEffectUpdateReordered,
            ],
        ];
    }
    // ModCallbackCustom.POST_PEFFECT_UPDATE_REORDERED
    postPEffectUpdateReordered = (player) => {
        const effects = player.GetEffects();
        const entityFlags = player.GetEntityFlags();
        const hasPonyCollectibleEffect = effects.HasCollectibleEffect(isaac_typescript_definitions_1.CollectibleType.PONY) ||
            effects.HasCollectibleEffect(isaac_typescript_definitions_1.CollectibleType.WHITE_PONY);
        const isPonyActiveOnPreviousFrame = (0, playerDataStructures_1.setHasPlayer)(v.run.playersIsPonyActive, player);
        const hasPonyFlags = (0, flag_1.hasFlag)(entityFlags, ...FLAGS_WHEN_PONY_IS_ACTIVE);
        const isPonyActiveNow = hasPonyCollectibleEffect || (isPonyActiveOnPreviousFrame && hasPonyFlags);
        if (isPonyActiveNow) {
            (0, playerDataStructures_1.setAddPlayer)(v.run.playersIsPonyActive, player);
        }
        else {
            (0, playerDataStructures_1.setDeletePlayer)(v.run.playersIsPonyActive, player);
        }
    };
    /**
     * Helper function to see if the player is under the effects of A Pony or White Pony charge.
     * Detecting this is difficult, as the temporary effect will disappear upon entering a new room.
     *
     * In order to use this function, you must upgrade your mod with `ISCFeature.PONY_DETECTION`.
     *
     * @public
     */
    isPlayerUsingPony(player) {
        return (0, playerDataStructures_1.setHasPlayer)(v.run.playersIsPonyActive, player);
    }
    /**
     * Helper function to see if any player is under the effects of A Pony or White Pony charge.
     * Detecting this is difficult, as the temporary effect will disappear upon entering a new room.
     *
     * In order to use this function, you must upgrade your mod with `ISCFeature.PONY_DETECTION`.
     *
     * @public
     */
    anyPlayerUsingPony() {
        const players = (0, playerIndex_1.getPlayers)();
        return players.some((player) => this.isPlayerUsingPony(player));
    }
}
exports.PonyDetection = PonyDetection;
__decorate([
    decorators_1.Exported
], PonyDetection.prototype, "isPlayerUsingPony", null);
__decorate([
    decorators_1.Exported
], PonyDetection.prototype, "anyPlayerUsingPony", null);
