var _a;
import * as mc from "@minecraft/server";
import * as utils from "../../utils/index";
import { HEART_DATA } from "../../shared/heart_data";
import { fullHealthObjective } from "../health/health";
/**
 * @remarks This class provides cleanup and manual triggering of heart abilities.
 * The ability triggering on damage is controlled by the HealthManager class.
 */
export class HeartAbilityManager {
    static activate(player) {
        const health = player.Health;
        if (health && health.currentValue > 20) {
            const slotIndex = Math.ceil((health.currentValue - 20) / 2) - 1;
            const abilityIndex = player.getScore(`Slot_${slotIndex}`);
            HEART_DATA[abilityIndex].performAbility(player);
        }
    }
    static cleanup(player) {
        var _b;
        this.clearInvulnerability(player, "noDamage");
        this.clearInvulnerability(player, "noFallDamage");
        this.clearInvulnerability(player, "noAnvilDamage");
        this.clearInvulnerability(player, "noExplosionDamage");
        if (player.hasTag("hint_menu")) {
            player.setGameMode(mc.GameMode.survival);
            player.removeTag("hint_menu");
        }
        ;
        if (player.getItemCount("ldz:heart_sorter") <= 0) {
            const item = new mc.ItemStack("ldz:heart_sorter", 1);
            item.lockMode = mc.ItemLockMode.inventory;
            item.setLore(['', 'Hold this and punch', 'to take damage and', 'use a heart power']);
            (_b = player.Inventory) === null || _b === void 0 ? void 0 : _b.addItem(item);
        }
        player.setScore(fullHealthObjective, 1);
        if (!utils.getScoreAsBool(".var", "FriendlyFire")) {
            player.addTag("friendlyFireOff");
        }
        else {
            player.removeTag("friendlyFireOff");
        }
    }
    /**
     * @remarks Adds invulnerability of a certain category to a player.
     * @param player The player to add invulnerability to.
     * @param category The category of invulnerability to add, as tags to check for in the player file.
     */
    static addInvulnerability(player, category) {
        player.addTag(category);
        utils.modifyScore(player, category, 1);
    }
    /**
     * @remarks Adds invulnerability of a certain category to all players with the friendlyFireOff tag.
     * @param category The category of invulnerability to add, as tags to check for in the player file.
     */
    static addInvulnerabilityToFriends(category) {
        if (!utils.getScoreAsBool(".var", "FriendlyFire")) {
            mc.world.getPlayers({ tags: ["friendlyFireOff"] }).forEach(player => this.addInvulnerability(player, category));
        }
    }
    /**
     * @remarks Removes invulnerability of a certain category to all players with the friendlyFireOff tag.
     * @param category The category of invulnerability to remove, as tags to check for in the player file.
     */
    static removeInvulnerabilityFromFriends(category) {
        if (!utils.getScoreAsBool(".var", "FriendlyFire")) {
            mc.world.getPlayers({ tags: ["friendlyFireOff"] }).forEach(player => this.removeInvulnerability(player, category));
        }
    }
    /**
     * @remarks Removes invulnerability of a certain category from a player. Since invulnerability can stack from multiple abilities, this pops the stack rather than clearing it.
     * @param player The player to remove invulnerability from.
     * @param category The category of invulnerability to remove, as tags to check for in the player file.
     */
    static removeInvulnerability(player, category) {
        utils.modifyScore(player, category, -1);
        if (utils.getScore(player, category) <= 0) {
            player.removeTag(category);
        }
    }
    /**
     * @remarks Removes invulnerability of a certain category from a player. This clears the invulnerability stack rather than popping it.
     * @param player The player to remove invulnerability from.
     * @param category The category of invulnerability to remove, as tags to check for in the player file.
     */
    static clearInvulnerability(player, category) {
        utils.setScore(player, category, 0);
        player.removeTag(category);
    }
}
_a = HeartAbilityManager;
(() => {
    mc.world.afterEvents.playerSpawn.subscribe(event => {
        _a.cleanup(new utils.SafetyCheckedPlayer(event.player));
    });
    utils.afterEvents.scriptEventRecieve.subscribe("ldz:activate_ability", event => {
        var _b;
        const player = (_b = event.sourceEntity) === null || _b === void 0 ? void 0 : _b.getPlayer();
        if (player) {
            _a.activate(player);
        }
    });
})();
