"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateAvengeCounters = exports.applyAvengeEffects = void 0;
const reference_data_1 = require("@firestone-hs/reference-data");
const card_interface_1 = require("../cards/card.interface");
const _card_mappings_1 = require("../cards/impl/_card-mappings");
const divine_shield_1 = require("../keywords/divine-shield");
const reborn_1 = require("../keywords/reborn");
const taunt_1 = require("../keywords/taunt");
const utils_1 = require("../services/utils");
const simulate_bgs_battle_1 = require("../simulate-bgs-battle");
const utils_2 = require("../utils");
const attack_1 = require("./attack");
const cards_in_hand_1 = require("./cards-in-hand");
const deathrattle_spawns_1 = require("./deathrattle-spawns");
const spawns_1 = require("./spawns");
const stats_1 = require("./stats");
const applyAvengeEffects = (deadEntity, deadEntityIndexFromRight, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, entitiesSpawnedFromMinionDeath) => {
    const candidatesEntitiesSpawnedFromAvenge = [];
    const avengers = boardWithDeadEntity
        .filter((e) => !!e.avengeDefault && e.avengeCurrent <= 0)
        .sort((a, b) => {
        return 0;
    });
    for (const avenger of avengers) {
        handleAvenge(boardWithDeadEntity, boardWithDeadEntityHero, deadEntity, deadEntityIndexFromRight, avenger, candidatesEntitiesSpawnedFromAvenge, otherBoard, otherBoardHero, gameState);
    }
    for (const heroPower of boardWithDeadEntityHero.heroPowers) {
        const heroAvenger = !!heroPower.avengeDefault && heroPower.avengeCurrent <= 0;
        if (heroAvenger) {
            handleHeroAvenge(deadEntity, heroPower, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, candidatesEntitiesSpawnedFromAvenge, gameState);
        }
    }
    const questRewardAvengers = boardWithDeadEntityHero.questRewardEntities.filter((e) => !!e.avengeDefault && e.avengeCurrent <= 0);
    for (const avenger of questRewardAvengers) {
        handleAvenge(boardWithDeadEntity, boardWithDeadEntityHero, deadEntity, deadEntityIndexFromRight, avenger, candidatesEntitiesSpawnedFromAvenge, otherBoard, otherBoardHero, gameState);
    }
    const trinketAvengers = boardWithDeadEntityHero.trinkets.filter((e) => !!e.avengeDefault && e.avengeCurrent <= 0);
    for (const avenger of trinketAvengers) {
        handleAvenge(boardWithDeadEntity, boardWithDeadEntityHero, deadEntity, deadEntityIndexFromRight, avenger, candidatesEntitiesSpawnedFromAvenge, otherBoard, otherBoardHero, gameState);
    }
    (0, spawns_1.performEntitySpawns)(candidatesEntitiesSpawnedFromAvenge, boardWithDeadEntity, boardWithDeadEntityHero, deadEntity, deadEntityIndexFromRight, otherBoard, otherBoardHero, gameState);
    const hasDeathrattle = (0, utils_2.hasEntityMechanic)(deadEntity, reference_data_1.GameTag.DEATHRATTLE, gameState.allCards) ||
        deadEntity.enchantments.some((e) => (0, simulate_bgs_battle_1.isValidDeathrattleEnchantment)(e.cardId));
    if (hasDeathrattle) {
        boardWithDeadEntity
            .filter((e) => e.cardId === "BG29_863" || e.cardId === "BG29_863_G")
            .forEach((ghoul) => {
            const buff = ghoul.cardId === "BG29_863_G" ? 2 : 1;
            (0, utils_2.addStatsToBoard)(ghoul, boardWithDeadEntity, boardWithDeadEntityHero, 2 * buff, 2 * buff, gameState);
        });
    }
    return candidatesEntitiesSpawnedFromAvenge;
};
exports.applyAvengeEffects = applyAvengeEffects;
const updateAvengeCounters = (board, boardWithDeadEntityHero) => {
    for (const entity of board) {
        if (!!entity.avengeDefault) {
            entity.avengeCurrent -= 1;
        }
    }
    for (const heroPower of boardWithDeadEntityHero.heroPowers) {
        if (!!heroPower.avengeDefault) {
            heroPower.avengeCurrent -= 1;
        }
    }
    for (const reward of boardWithDeadEntityHero.questRewardEntities) {
        if (!!reward.avengeDefault) {
            reward.avengeCurrent -= 1;
        }
    }
    for (const trinket of boardWithDeadEntityHero.trinkets) {
        if (!!trinket.avengeDefault) {
            trinket.avengeCurrent -= 1;
        }
    }
};
exports.updateAvengeCounters = updateAvengeCounters;
const handleAvenge = (boardWithDeadEntity, boardWithDeadEntityHero, deadEntity, deadEntityIndexFromRight, avenger, candidatesEntitiesSpawnedFromAvenge, otherBoard, otherBoardHero, gameState) => {
    var _a, _b, _c, _d;
    const avengeImpl = _card_mappings_1.cardMappings[avenger.cardId];
    if ((0, card_interface_1.hasAvenge)(avengeImpl)) {
        const newSpawns = avengeImpl.avenge(avenger, {
            board: boardWithDeadEntity,
            hero: boardWithDeadEntityHero,
            otherBoard: otherBoard,
            otherHero: otherBoardHero,
            deadEntity: deadEntity,
            gameState,
        });
        if (Array.isArray(newSpawns) && (newSpawns === null || newSpawns === void 0 ? void 0 : newSpawns.length)) {
            candidatesEntitiesSpawnedFromAvenge.push(...newSpawns);
        }
    }
    else {
        switch (avenger.cardId) {
            case "BG22_HERO_003_Buddy":
            case "BG22_HERO_003_Buddy_G":
                break;
            case "BG21_001":
                const target1 = (0, utils_2.grantRandomStats)(avenger, boardWithDeadEntity, boardWithDeadEntityHero, 6, 6, reference_data_1.Race.BEAST, true, gameState);
                if (!!target1) {
                    gameState.spectator.registerPowerTarget(avenger, target1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoardHero);
                }
                break;
            case "BG21_001_G":
                const target2 = (0, utils_2.grantRandomStats)(avenger, boardWithDeadEntity, boardWithDeadEntityHero, 12, 12, reference_data_1.Race.BEAST, true, gameState);
                if (!!target2) {
                    gameState.spectator.registerPowerTarget(avenger, target2, boardWithDeadEntity, boardWithDeadEntityHero, otherBoardHero);
                }
                break;
            case "BG21_007":
            case "BG21_007_G":
                const doomsayerCardsToAddQuantity = avenger.cardId === "BG21_007_G" ? 2 : 1;
                const doomsayerCardsToAdd = [];
                for (let i = 0; i < doomsayerCardsToAddQuantity; i++) {
                    doomsayerCardsToAdd.push((0, utils_1.pickRandom)(gameState.cardsData.demonSpawns));
                }
                (0, cards_in_hand_1.addCardsInHand)(boardWithDeadEntityHero, boardWithDeadEntity, doomsayerCardsToAdd, gameState);
                break;
            case "BG23_014":
            case "BG23_014_G":
                break;
            case "BG28_967":
            case "BG28_967_G":
                (0, cards_in_hand_1.addCardsInHand)(boardWithDeadEntityHero, boardWithDeadEntity, [null], gameState);
                break;
            case "BG20_105":
            case "BG20_105_G":
                const thorncallerToAddQuantity = avenger.cardId === "BG20_105_G" ? 2 : 1;
                const thorncallerCardsToAdd = Array(thorncallerToAddQuantity).fill("BG20_GEM");
                (0, cards_in_hand_1.addCardsInHand)(boardWithDeadEntityHero, boardWithDeadEntity, thorncallerCardsToAdd, gameState);
                break;
            case "BG21_023":
                const target = (0, utils_2.getRandomMinionWithHighestHealth)(otherBoard);
                gameState.spectator.registerPowerTarget(avenger, target, otherBoard, boardWithDeadEntityHero, otherBoardHero);
                (0, attack_1.dealDamageToMinion)(target, otherBoard, otherBoardHero, avenger, 5, boardWithDeadEntity, boardWithDeadEntityHero, gameState);
                break;
            case "BG21_023_G":
                for (let i = 0; i < 2; i++) {
                    const target = (0, utils_2.getRandomMinionWithHighestHealth)(otherBoard);
                    gameState.spectator.registerPowerTarget(avenger, target, otherBoard, boardWithDeadEntityHero, otherBoardHero);
                    (0, attack_1.dealDamageToMinion)(target, otherBoard, otherBoardHero, avenger, 5, boardWithDeadEntity, boardWithDeadEntityHero, gameState);
                }
                break;
            case "BG25_014":
            case "BG25_014_G":
                const abominationMultiplier = avenger.cardId === "BG25_014_G" ? 2 : 1;
                (0, stats_1.modifyStats)(avenger, avenger, abominationMultiplier * 1, abominationMultiplier * 2, boardWithDeadEntity, boardWithDeadEntityHero, gameState);
                break;
            case "BG27_Reward_502":
                const highestHealthMinion = [...otherBoard].sort((a, b) => b.health - a.health)[0];
                (0, attack_1.dealDamageToMinion)(highestHealthMinion, otherBoard, otherBoardHero, avenger, 10, boardWithDeadEntity, boardWithDeadEntityHero, gameState);
                gameState.spectator.registerPowerTarget(avenger, highestHealthMinion, otherBoard, boardWithDeadEntityHero, otherBoardHero);
                break;
            case "BG25_003":
            case "BG25_003_G":
                (0, reborn_1.updateReborn)(avenger, true, boardWithDeadEntity, boardWithDeadEntityHero, otherBoardHero, gameState);
                (0, taunt_1.updateTaunt)(avenger, true, boardWithDeadEntity, boardWithDeadEntityHero, otherBoardHero, gameState);
                break;
            case "BG27_010":
            case "BG27_010_G":
                (0, reborn_1.updateReborn)(avenger, true, boardWithDeadEntity, boardWithDeadEntityHero, otherBoardHero, gameState);
                (0, taunt_1.updateTaunt)(avenger, true, boardWithDeadEntity, boardWithDeadEntityHero, otherBoardHero, gameState);
                break;
            case "BG28_403":
            case "BG28_403_G":
                const phaerixLoops = avenger.cardId === "BG28_403_G" ? 2 : 1;
                for (let i = 0; i < phaerixLoops; i++) {
                    (0, divine_shield_1.grantRandomDivineShield)(avenger, boardWithDeadEntity, boardWithDeadEntityHero, otherBoardHero, gameState);
                }
                break;
            case "BG28_740":
            case "BG28_740_G":
                const AugmentedLaborerLoops = avenger.cardId === "BG28_740_G" ? 2 : 1;
                for (let i = 0; i < AugmentedLaborerLoops; i++) {
                    (0, cards_in_hand_1.addCardsInHand)(boardWithDeadEntityHero, boardWithDeadEntity, [null], gameState);
                }
                break;
            case "BG28_Reward_505":
                const tumblingDisasterEntity = (_a = boardWithDeadEntityHero.questRewardEntities) === null || _a === void 0 ? void 0 : _a.find((e) => e.cardId === "BG28_Reward_505");
                if (tumblingDisasterEntity) {
                    tumblingDisasterEntity.scriptDataNum1 = ((_b = tumblingDisasterEntity.scriptDataNum1) !== null && _b !== void 0 ? _b : 0) + 1;
                }
                break;
            case "BG28_Reward_504":
                (0, cards_in_hand_1.addCardsInHand)(boardWithDeadEntityHero, boardWithDeadEntity, [null], gameState);
                break;
            case "BG28_Reward_518":
                avenger.scriptDataNum1 = ((_c = avenger.scriptDataNum1) !== null && _c !== void 0 ? _c : 0) + 1;
                break;
            case "BG29_991":
            case "BG29_991_G":
                const murglMkStats = avenger.cardId === "BG29_991_G" ? 2 : 1;
                (0, utils_2.addStatsToBoard)(avenger, boardWithDeadEntity, boardWithDeadEntityHero, murglMkStats, murglMkStats, gameState);
                for (const e of (_d = boardWithDeadEntityHero.hand) !== null && _d !== void 0 ? _d : []) {
                    e.attack += murglMkStats;
                    e.health += murglMkStats;
                    e.maxHealth += murglMkStats;
                }
                break;
            case "BG30_MagicItem_437":
                if (boardWithDeadEntity.length > 0) {
                    const target = (0, utils_1.pickRandom)(boardWithDeadEntity.filter((e) => !e.reborn));
                    if (!!target) {
                        (0, reborn_1.updateReborn)(target, true, boardWithDeadEntity, boardWithDeadEntityHero, otherBoardHero, gameState);
                        gameState.spectator.registerPowerTarget(avenger, target, boardWithDeadEntity, boardWithDeadEntityHero, otherBoardHero);
                    }
                }
                break;
            case "BG30_MagicItem_713":
                const randomUndead = gameState.cardsData.getRandomMinionForTribe(reference_data_1.Race.UNDEAD, boardWithDeadEntityHero.tavernTier);
                (0, cards_in_hand_1.addCardsInHand)(boardWithDeadEntityHero, boardWithDeadEntity, [randomUndead], gameState);
                break;
        }
    }
    avenger.avengeCurrent += avenger.avengeDefault;
};
const handleHeroAvenge = (deadEntity, heroPower, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, candidatesEntitiesSpawnedFromAvenge, gameState) => {
    switch (heroPower.cardId) {
        case "BG22_HERO_305p":
            const broodmotherTokens = (0, deathrattle_spawns_1.spawnEntities)("BG22_HERO_305t", 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false, false, false);
            broodmotherTokens.forEach((token) => {
                token.attack = +heroPower.info;
                token.health = +heroPower.info;
                token.maxAttack = +heroPower.info;
                token.maxHealth = +heroPower.info;
            });
            heroPower.info = +heroPower.info + 1;
            candidatesEntitiesSpawnedFromAvenge.push(...broodmotherTokens);
            break;
    }
    heroPower.avengeCurrent += heroPower.avengeDefault;
};
//# sourceMappingURL=avenge.js.map