"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.applyOnDeathEffects = exports.applyAfterDeathEffects = void 0;
const reference_data_1 = require("@firestone-hs/reference-data");
const blood_gems_1 = require("./blood-gems");
const utils_1 = require("../services/utils");
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 applyAfterDeathEffects = (deadEntity, deadEntityIndexFromRight, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState) => {
    const allSpawns = [];
    const spawnsFromSecrets = handleSecrets(deadEntity, deadEntityIndexFromRight, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState);
    allSpawns.push(...spawnsFromSecrets);
    (0, spawns_1.performEntitySpawns)(allSpawns, boardWithDeadEntity, boardWithDeadEntityHero, deadEntity, deadEntityIndexFromRight, otherBoard, otherBoardHero, gameState);
    return allSpawns;
};
exports.applyAfterDeathEffects = applyAfterDeathEffects;
const handleSecrets = (deadEntity, deadEntityIndexFromRight, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState) => {
    var _a, _b;
    const allSpawns = [];
    for (const trinket of boardWithDeadEntityHero.trinkets) {
        switch (trinket.cardId) {
            case "BG30_MagicItem_931":
                if (!trinket.scriptDataNum1) {
                    trinket.scriptDataNum1 = 6;
                }
                trinket.scriptDataNum1--;
                if (trinket.scriptDataNum1 === 0) {
                    const randomBeast = (0, utils_1.pickRandom)(gameState.cardsData.beastSpawns);
                    (0, cards_in_hand_1.addCardsInHand)(boardWithDeadEntityHero, boardWithDeadEntity, [randomBeast], gameState);
                }
                break;
            case "BG35_MagicItem_302":
                trinket.scriptDataNum1 = (trinket.scriptDataNum1 || 0) + 1;
                if (trinket.scriptDataNum1 >= 7) {
                    trinket.scriptDataNum1 = 0;
                    const randomMech = gameState.cardsData.getRandomMinionForTribe(reference_data_1.Race.MECH, (_a = boardWithDeadEntityHero.tavernTier) !== null && _a !== void 0 ? _a : 6, []);
                    if (randomMech) {
                        (0, cards_in_hand_1.addCardsInHand)(boardWithDeadEntityHero, boardWithDeadEntity, [randomMech], gameState);
                    }
                }
                break;
            case "BG35_MagicItem_430":
                if (deadEntity.taunt) {
                    const scrapsmiths = boardWithDeadEntity.filter((e) => e.health > 0 &&
                        !e.definitelyDead &&
                        (e.cardId === "BG24_707" ||
                            e.cardId === "BG24_707_G"));
                    for (const scrapsmith of scrapsmiths) {
                        (0, blood_gems_1.playBloodGemsOn)(trinket, scrapsmith, 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState);
                    }
                }
                break;
        }
    }
    for (const secret of ((_b = boardWithDeadEntityHero.secrets) !== null && _b !== void 0 ? _b : []).filter((s) => !s.triggered)) {
        switch (secret.cardId) {
            case "TB_Bacon_Secrets_08":
                secret.triggered = true;
                const avengeTarget = (0, utils_1.pickRandomAlive)(boardWithDeadEntity);
                (0, stats_1.modifyStats)(avengeTarget, secret, 3, 2, boardWithDeadEntity, boardWithDeadEntityHero, gameState);
                break;
            case "TB_Bacon_Secrets_10":
                secret.triggered = true;
                const redemptionSpawns = (0, deathrattle_spawns_1.spawnEntities)(deadEntity.cardId, 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false, false, true);
                redemptionSpawns[0].health = 1;
                allSpawns.push(...redemptionSpawns);
                gameState.spectator.registerPowerTarget(secret, redemptionSpawns[0], boardWithDeadEntity, null, null);
                break;
            case "TB_Bacon_Secrets_10b":
                secret.triggered = true;
                const betterRedemptionSpawns = (0, deathrattle_spawns_1.spawnEntities)(deadEntity.cardId, 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false, false, true);
                for (const spawn of betterRedemptionSpawns) {
                    spawn.health = deadEntity.maxHealth;
                    spawn.maxHealth = deadEntity.maxHealth;
                    spawn.attack = deadEntity.attack;
                    spawn.maxAttack = deadEntity.maxAttack;
                    spawn.enchantments = deadEntity.enchantments;
                    spawn.divineShield = deadEntity.hadDivineShield;
                }
                allSpawns.push(...betterRedemptionSpawns);
                gameState.spectator.registerPowerTarget(secret, betterRedemptionSpawns[0], boardWithDeadEntity, null, null);
                break;
            case "TB_Bacon_Secrets_05":
                secret.triggered = true;
                const minionTier = gameState.cardsData.getTavernLevel(deadEntity.cardId);
                const newMinion = gameState.cardsData.getRandomMinionForTavernTier(minionTier);
                const effigySpawns = (0, deathrattle_spawns_1.spawnEntities)(newMinion, 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false, false, true);
                allSpawns.push(...effigySpawns);
                gameState.spectator.registerPowerTarget(secret, effigySpawns[0], boardWithDeadEntity, null, null);
                break;
        }
    }
    return allSpawns;
};
const applyOnDeathEffects = (deadEntity, deadEntityIndexFromRight, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState) => {
    return [];
};
exports.applyOnDeathEffects = applyOnDeathEffects;
//# sourceMappingURL=death-effects.js.map