"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.CharacterHealthConversion = void 0;
const isaac_typescript_definitions_1 = require("isaac-typescript-definitions");
const decorators_1 = require("../../../decorators");
const ModCallbackCustom_1 = require("../../../enums/ModCallbackCustom");
const pickups_1 = require("../../../functions/pickups");
const Feature_1 = require("../../private/Feature");
class CharacterHealthConversion extends Feature_1.Feature {
    characterHealthReplacementMap = new Map();
    /** @internal */
    constructor() {
        super();
        this.callbacksUsed = [
            // 38
            [
                isaac_typescript_definitions_1.ModCallback.PRE_PICKUP_COLLISION,
                this.prePickupCollisionHeart,
                [isaac_typescript_definitions_1.PickupVariant.HEART],
            ],
        ];
        this.customCallbacksUsed = [
            [
                ModCallbackCustom_1.ModCallbackCustom.POST_PEFFECT_UPDATE_REORDERED,
                this.postPEffectUpdateReordered,
            ],
        ];
    }
    // ModCallback.PRE_PICKUP_COLLISION (38)
    // PickupVariant.HEART (10)
    prePickupCollisionHeart = (pickup, collider) => {
        if (!(0, pickups_1.isRedHeart)(pickup)) {
            return undefined;
        }
        const player = collider.ToPlayer();
        if (player === undefined) {
            return undefined;
        }
        const character = player.GetPlayerType();
        const conversionHeartSubType = this.characterHealthReplacementMap.get(character);
        if (conversionHeartSubType === undefined) {
            return undefined;
        }
        // Prevent internal code from running, which will prevent the player from picking up the heart,
        // but will still allow the heart to bounce off of the player.
        return false;
    };
    // ModCallbackCustom.POST_PEFFECT_UPDATE_REORDERED
    postPEffectUpdateReordered = (player) => {
        const character = player.GetPlayerType();
        const conversionHeartSubType = this.characterHealthReplacementMap.get(character);
        if (conversionHeartSubType === undefined) {
            return undefined;
        }
        convertRedHeartContainers(player, conversionHeartSubType);
        removeRedHearts(player);
    };
    /**
     * Helper function to make a character that has the same health mechanic as Blue Baby (red heart
     * containers --> soul hearts) or Dark Judas (red heart containers --> black hearts).
     *
     * Call this function once at the beginning of your mod to declare the health conversion type.
     *
     * In order to use this function, you must upgrade your mod with
     * `ISCFeature.CHARACTER_HEALTH_CONVERSION`.
     *
     * @public
     */
    registerCharacterHealthConversion(playerType, conversionHeartSubType) {
        if (this.characterHealthReplacementMap.has(playerType)) {
            error(`Failed to register a character of type ${playerType} because there is already an existing registered character with that type.`);
        }
        this.characterHealthReplacementMap.set(playerType, conversionHeartSubType);
    }
}
exports.CharacterHealthConversion = CharacterHealthConversion;
__decorate([
    decorators_1.Exported
], CharacterHealthConversion.prototype, "registerCharacterHealthConversion", null);
function convertRedHeartContainers(player, heartSubType) {
    const maxHearts = player.GetMaxHearts();
    if (maxHearts === 0) {
        return;
    }
    player.AddMaxHearts(maxHearts * -1, false);
    switch (heartSubType) {
        case isaac_typescript_definitions_1.HeartSubType.SOUL: {
            player.AddSoulHearts(maxHearts);
            break;
        }
        case isaac_typescript_definitions_1.HeartSubType.BLACK: {
            player.AddBlackHearts(maxHearts);
            break;
        }
    }
}
/**
 * We also have to check for normal red hearts, so that the player is not able to fill bone hearts
 * (by e.g. picking up a healing item like Breakfast).
 */
function removeRedHearts(player) {
    const hearts = player.GetHearts();
    if (hearts > 0) {
        player.AddHearts(hearts * -1);
    }
}
