"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.onMinionDeadQuest = exports.onMinionDeadHeroPower = exports.makeMinionsDie = void 0;
const utils_1 = require("../services/utils");
const utils_2 = require("../utils");
const avenge_1 = require("./avenge");
const cards_in_hand_1 = require("./cards-in-hand");
const deathrattle_effects_1 = require("./deathrattle-effects");
const quest_1 = require("./quest");
const remove_minion_from_board_1 = require("./remove-minion-from-board");
const stats_1 = require("./stats");
const makeMinionsDie = (board, boardHero, otherBoard, otherBoardHero, gameState) => {
    const deadMinionIndexesFromLeft = [];
    const deadMinionIndexesFromRight = [];
    const deadEntities = [];
    const initialBoardLength = board.length;
    (0, deathrattle_effects_1.handleWheneverMinionsKillEffect)(board, boardHero, otherBoard, otherBoardHero, gameState);
    for (let i = 0; i < board.length; i++) {
        if (board[i].health <= 0 || board[i].definitelyDead) {
            deadMinionIndexesFromLeft.push(i);
            deadMinionIndexesFromRight.push(initialBoardLength - (i + 1));
            deadEntities.push(board[i]);
        }
    }
    let indexesFromRightAfterDeath = [];
    for (let i = deadMinionIndexesFromRight.length - 1; i >= 0; i--) {
        const newIndex = deadMinionIndexesFromRight[i] - indexesFromRightAfterDeath.length;
        indexesFromRightAfterDeath.push(newIndex);
    }
    indexesFromRightAfterDeath = indexesFromRightAfterDeath.reverse();
    for (let i = 0; i < board.length; i++) {
        if (board[i].health <= 0 || board[i].definitelyDead) {
            (0, remove_minion_from_board_1.removeMinionFromBoard)(board, boardHero, i, gameState);
            i--;
        }
    }
    for (const deadEntity of deadEntities) {
        const indexFromRight = indexesFromRightAfterDeath[deadEntities.indexOf(deadEntity)];
        (0, avenge_1.updateAvengeCounters)(board, boardHero);
        (0, exports.onMinionDeadHeroPower)(board, boardHero, deadEntity, gameState);
        (0, exports.onMinionDeadHeroPower)(otherBoard, otherBoardHero, deadEntity, gameState);
        (0, exports.onMinionDeadQuest)(deadEntity, indexFromRight, board, boardHero, otherBoard, otherBoardHero, gameState);
        (0, exports.onMinionDeadQuest)(deadEntity, indexFromRight, otherBoard, otherBoardHero, board, boardHero, gameState);
    }
    return [deadMinionIndexesFromLeft, indexesFromRightAfterDeath, deadEntities];
};
exports.makeMinionsDie = makeMinionsDie;
const onMinionDeadHeroPower = (board, boardHero, deadEntity, gameState) => {
    for (const heroPower of boardHero.heroPowers) {
        if (heroPower.cardId === "TB_BaconShop_HP_053" &&
            heroPower.used &&
            heroPower.info2 <= 0 &&
            deadEntity.friendly !== boardHero.friendly) {
            (0, cards_in_hand_1.addCardsInHand)(boardHero, board, [deadEntity.cardId], gameState);
            heroPower.info2 = 1;
        }
    }
};
exports.onMinionDeadHeroPower = onMinionDeadHeroPower;
const onMinionDeadQuest = (deadEntity, indexFromRight, board, boardHero, otherBoard, otherBoardHero, gameState) => {
    var _a;
    const quests = (_a = boardHero.questEntities) !== null && _a !== void 0 ? _a : [];
    for (const quest of quests) {
        switch (quest.CardId) {
            case "BG24_Quest_124":
                if (deadEntity.friendly === boardHero.friendly) {
                    (0, quest_1.onQuestProgressUpdated)(boardHero, quest, board, gameState);
                }
                break;
            case "BG27_Quest_802":
                if (deadEntity.friendly !== boardHero.friendly) {
                    (0, quest_1.onQuestProgressUpdated)(boardHero, quest, board, gameState);
                }
                break;
        }
    }
    for (const trinket of boardHero.trinkets) {
        switch (trinket.cardId) {
            case "BG30_MagicItem_433":
            case "BG30_MagicItem_433t":
                if (trinket.scriptDataNum1 > 0 && deadEntity.friendly === boardHero.friendly) {
                    const quantity = trinket.cardId === "BG30_MagicItem_433" ? 1 : 2;
                    const targets = (0, utils_1.pickMultipleRandomAlive)(board, quantity);
                    for (const target of targets) {
                        (0, stats_1.modifyStats)(target, boardHero, deadEntity.maxAttack, deadEntity.maxHealth, board, boardHero, gameState);
                    }
                    trinket.scriptDataNum1--;
                }
                break;
            case "BG30_MagicItem_981":
                if (deadEntity.friendly === boardHero.friendly &&
                    (0, utils_2.getEffectiveTribesForEntity)(deadEntity, boardHero, gameState.anomalies, gameState.allCards)
                        .length === 0) {
                    (0, cards_in_hand_1.addCardsInHand)(boardHero, board, [null], gameState);
                }
                break;
        }
    }
};
exports.onMinionDeadQuest = onMinionDeadQuest;
//# sourceMappingURL=minion-death.js.map