"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.spawnEntitiesFromDeathrattle = exports.spawnEntities = exports.simplifiedSpawnEntitiesWithAddToBoard = exports.simplifiedSpawnEntities = 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 utils_1 = require("../services/utils");
const utils_2 = require("../utils");
const attack_1 = require("./attack");
const cards_in_hand_1 = require("./cards-in-hand");
const deathrattle_effects_1 = require("./deathrattle-effects");
const deathrattle_on_trigger_1 = require("./deathrattle-on-trigger");
const spawns_1 = require("./spawns");
const stats_1 = require("./stats");
const golden_1 = require("./utils/golden");
const simplifiedSpawnEntities = (cardId, quantity, input, boardEntityToSpawn = null) => {
    return (0, exports.spawnEntities)(cardId, quantity, input.boardWithDeadEntity, input.boardWithDeadEntityHero, input.otherBoard, input.otherBoardHero, input.gameState, input.deadEntity.friendly, false, false, false, boardEntityToSpawn);
};
exports.simplifiedSpawnEntities = simplifiedSpawnEntities;
const simplifiedSpawnEntitiesWithAddToBoard = (cardId, quantity, input, source, indexFromRight, boardEntityToSpawn = null) => {
    const spawns = (0, exports.spawnEntities)(cardId, quantity, input.boardWithDeadEntity, input.boardWithDeadEntityHero, input.otherBoard, input.otherBoardHero, input.gameState, input.deadEntity.friendly, false, false, false, boardEntityToSpawn);
    (0, spawns_1.performEntitySpawns)(spawns, input.boardWithDeadEntity, input.boardWithDeadEntityHero, source, indexFromRight, input.otherBoard, input.otherBoardHero, input.gameState);
};
exports.simplifiedSpawnEntitiesWithAddToBoard = simplifiedSpawnEntitiesWithAddToBoard;
const spawnEntities = (cardId, quantity, boardToSpawnInto, boardToSpawnIntoHero, otherBoard, otherHero, gameState, friendly, limitSpawns, spawnReborn = false, useKhadgar = true, boardEntityToSpawn = null, originalEntity = null) => {
    var _a;
    if (!cardId) {
        console.error('Cannot spawn a minion without any cardId defined', (0, utils_2.stringifySimple)(boardToSpawnInto, gameState.allCards), new Error().stack);
    }
    if ((_a = gameState.anomalies) === null || _a === void 0 ? void 0 : _a.includes("BG27_Anomaly_801")) {
        if (!gameState.allCards.getCard(cardId).premium) {
            const premiumDbfId = gameState.allCards.getCard(cardId).battlegroundsPremiumDbfId;
            cardId = gameState.allCards.getCard(premiumDbfId).id;
        }
    }
    const spawnMultiplier = useKhadgar
        ? 2 * boardToSpawnInto.filter((entity) => entity.cardId === "BG_DAL_575").length || 1
        : 1;
    const spawnMultiplierGolden = useKhadgar
        ? 3 * boardToSpawnInto.filter((entity) => entity.cardId === "TB_BaconUps_034").length || 1
        : 1;
    const minionsToSpawn = limitSpawns
        ? Math.min(quantity * spawnMultiplier * spawnMultiplierGolden, 7 - boardToSpawnInto.length)
        : quantity * spawnMultiplier * spawnMultiplierGolden;
    const result = [];
    for (let i = 0; i < minionsToSpawn; i++) {
        const newMinion = (0, utils_2.buildSingleBoardEntity)(cardId, boardToSpawnIntoHero, boardToSpawnInto, gameState.allCards, friendly, gameState.sharedState.currentEntityId++, spawnReborn, gameState.cardsData, gameState.sharedState, boardEntityToSpawn, originalEntity);
        if (!newMinion.cardId) {
            console.warn('Invalid spawn', newMinion, cardId);
        }
        result.push(newMinion);
    }
    return result;
};
exports.spawnEntities = spawnEntities;
const spawnEntitiesFromDeathrattle = (deadEntity, deadEntityIndexFromRight, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, entitiesDeadThisAttack, gameState) => {
    var _a, _b, _c, _d, _e, _f;
    const finalSpawns = [];
    const multiplier = (0, deathrattle_effects_1.computeDeathrattleMultiplier)([...boardWithDeadEntity], boardWithDeadEntityHero, deadEntity, gameState.sharedState);
    const deathrattleTriggeredInput = {
        boardWithDeadEntity,
        boardWithDeadEntityHero,
        deadEntity,
        deadEntityIndexFromRight: deadEntityIndexFromRight,
        otherBoard,
        otherBoardHero,
        gameState,
    };
    const enchantments = [
        ...deadEntity.enchantments,
    ].sort((a, b) => a.timing - b.timing);
    const cards = [deadEntity, ...enchantments]
        .sort((a, b) => {
        var _a, _b, _c, _d;
        return ((_b = (_a = a.entityId) !== null && _a !== void 0 ? _a : a.timing) !== null && _b !== void 0 ? _b : 0) -
            ((_d = (_c = b.entityId) !== null && _c !== void 0 ? _c : b.timing) !== null && _d !== void 0 ? _d : 0);
    });
    for (let i = 0; i < multiplier; i++) {
        for (const card of cards) {
            let hasTriggered = false;
            const spawnedEntities = [];
            const refCard = gameState.allCards.getCard(card.cardId);
            if (((_a = refCard.type) === null || _a === void 0 ? void 0 : _a.toUpperCase()) === reference_data_1.CardType[reference_data_1.CardType.MINION] &&
                card.health !== undefined) {
                const cardIds = [card.cardId, ...((_b = card.additionalCards) !== null && _b !== void 0 ? _b : [])];
                for (const deadEntityCardId of cardIds) {
                    let hasTriggeredThisLoop = true;
                    const spawnEntityImpl = _card_mappings_1.cardMappings[deadEntityCardId];
                    if ((0, card_interface_1.hasDeathrattleSpawn)(spawnEntityImpl)) {
                        const spawned = spawnEntityImpl.deathrattleSpawn(deadEntity, deathrattleTriggeredInput);
                        if (spawned === null || spawned === void 0 ? void 0 : spawned.length) {
                            spawnedEntities.push(...spawned);
                        }
                    }
                    else {
                        switch (deadEntityCardId) {
                            case "BOT_445":
                                spawnedEntities.push(...(0, exports.spawnEntities)("BOT_445t", 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "TB_BaconUps_002":
                                spawnedEntities.push(...(0, exports.spawnEntities)("TB_BaconUps_002t", 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "BG29_611":
                            case "BG29_611_G":
                                spawnedEntities.push(...(0, exports.spawnEntities)(deadEntity.cardId === "BG29_611"
                                    ? "BG_BOT_312t"
                                    : "TB_BaconUps_032t", 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "BG_AV_309":
                            case "BG_AV_309_G":
                                spawnedEntities.push(...(0, exports.spawnEntities)("AV_309t", deadEntityCardId === "BG_AV_309_G" ? 2 : 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "BG25_009":
                            case "BG25_009_G":
                                spawnedEntities.push(...(0, exports.spawnEntities)(deadEntityCardId === "BG25_009_G"
                                    ? "BG25_008_G"
                                    : "BG25_008", 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "BG21_029":
                                spawnedEntities.push(...(0, exports.spawnEntities)("BRM_006t", 2, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "BG21_029_G":
                                spawnedEntities.push(...(0, exports.spawnEntities)("BRM_006t", 4, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "BG_EX1_556":
                                spawnedEntities.push(...(0, exports.spawnEntities)("skele21", 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "TB_BaconUps_006":
                                spawnedEntities.push(...(0, exports.spawnEntities)("TB_BaconUps_006t", 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "BG19_010":
                                spawnedEntities.push(...(0, exports.spawnEntities)("BG19_010t", 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "BG19_010_G":
                                spawnedEntities.push(...(0, exports.spawnEntities)("BG19_010_Gt", 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "TB_BaconShop_HERO_702_Buddy":
                            case "TB_BaconShop_HERO_702_Buddy_G":
                                const minionsToSpawnMawsworn = deadEntityCardId === "TB_BaconShop_HERO_702_Buddy_G"
                                    ? 4
                                    : 2;
                                for (let i = 0; i < minionsToSpawnMawsworn; i++) {
                                    const minionCardId = gameState.cardsData.getRandomMinionForTribe(reference_data_1.Race.UNDEAD, boardWithDeadEntityHero.tavernTier);
                                    spawnedEntities.push(...(0, exports.spawnEntities)(minionCardId, 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                }
                                break;
                            case "BG_KAR_005":
                                spawnedEntities.push(...(0, exports.spawnEntities)("BG_KAR_005a", 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "TB_BaconUps_004":
                                spawnedEntities.push(...(0, exports.spawnEntities)("TB_BaconUps_004t", 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "BG_CFM_316":
                                spawnedEntities.push(...(0, exports.spawnEntities)("BG_CFM_316t", Math.min(7, deadEntity.attack), boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "TB_BaconUps_027":
                                spawnedEntities.push(...(0, exports.spawnEntities)("TB_BaconUps_027t", Math.min(7, deadEntity.attack), boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "BGS_014":
                                spawnedEntities.push(...(0, exports.spawnEntities)("BRM_006t", 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "TB_BaconUps_113":
                                spawnedEntities.push(...(0, exports.spawnEntities)("TB_BaconUps_030t", 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "OG_216":
                                spawnedEntities.push(...(0, exports.spawnEntities)("OG_216a", 2, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "BG_BOT_312":
                                spawnedEntities.push(...(0, exports.spawnEntities)("BG_BOT_312t", 3, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "TB_BaconUps_032":
                                spawnedEntities.push(...(0, exports.spawnEntities)("TB_BaconUps_032t", 3, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "BOT_537":
                                spawnedEntities.push(...(0, exports.spawnEntities)("BOT_537t", 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "TB_BaconUps_039":
                                spawnedEntities.push(...(0, exports.spawnEntities)("TB_BaconUps_039t", 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "BG_EX1_534":
                                spawnedEntities.push(...(0, exports.spawnEntities)("BG_EX1_534t", 2, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "TB_BaconUps_049":
                                spawnedEntities.push(...(0, exports.spawnEntities)("TB_BaconUps_049t", 2, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "BG_DMF_533":
                                spawnedEntities.push(...(0, exports.spawnEntities)("BG_DMF_533t", 2, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "TB_BaconUps_309":
                                spawnedEntities.push(...(0, exports.spawnEntities)("TB_BaconUps_309t", 2, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "UNG_010":
                                spawnedEntities.push(...(0, exports.spawnEntities)("UNG_201t", 3, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "BGS_008":
                                spawnedEntities.push(...[
                                    ...(0, exports.spawnEntities)(gameState.cardsData.ghastcoilerSpawns[Math.floor(Math.random() * gameState.cardsData.ghastcoilerSpawns.length)], 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false),
                                    ...(0, exports.spawnEntities)(gameState.cardsData.ghastcoilerSpawns[Math.floor(Math.random() * gameState.cardsData.ghastcoilerSpawns.length)], 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false),
                                ]);
                                break;
                            case "TB_BaconShop_HERO_37_Buddy":
                            case "TB_BaconShop_HERO_37_Buddy_G":
                                const kilrekCardsToAddQuantity = deadEntity.cardId === "TB_BaconShop_HERO_37_Buddy_G" ? 2 : 1;
                                const kilrekCardsToAdd = [];
                                for (let i = 0; i < kilrekCardsToAddQuantity; i++) {
                                    kilrekCardsToAdd.push((0, utils_1.pickRandom)(gameState.cardsData.demonSpawns));
                                }
                                (0, cards_in_hand_1.addCardsInHand)(boardWithDeadEntityHero, boardWithDeadEntity, kilrekCardsToAdd, gameState);
                                break;
                            case "TB_BaconShop_HERO_43_Buddy":
                            case "TB_BaconShop_HERO_43_Buddy_G":
                                const brannSpawns = [];
                                const brannToSpawnQuandtity = deadEntity.cardId === "TB_BaconShop_HERO_43_Buddy_G" ? 2 : 1;
                                for (let i = 0; i < brannToSpawnQuandtity; i++) {
                                    brannSpawns.push((0, utils_1.pickRandom)(gameState.cardsData.battlecryMinions));
                                }
                                for (const brannSpawn of brannSpawns) {
                                    spawnedEntities.push(...(0, exports.spawnEntities)(brannSpawn, 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                }
                                (0, cards_in_hand_1.addCardsInHand)(boardWithDeadEntityHero, boardWithDeadEntity, brannSpawns, gameState);
                                break;
                            case "TRLA_149":
                            case "TB_BaconUps_057":
                                const ghastcoilerLoop = deadEntity.cardId === "TB_BaconUps_057" ? 4 : 2;
                                for (let i = 0; i < ghastcoilerLoop; i++) {
                                    spawnedEntities.push(...[
                                        ...(0, exports.spawnEntities)(gameState.cardsData.ghastcoilerSpawns[Math.floor(Math.random() * gameState.cardsData.ghastcoilerSpawns.length)], 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false),
                                    ]);
                                }
                                break;
                            case "BG_LOOT_368":
                                spawnedEntities.push(...(0, exports.spawnEntities)("BG_CS2_065", 3, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "TB_BaconUps_059":
                                spawnedEntities.push(...(0, exports.spawnEntities)("TB_BaconUps_059t", 3, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "BG26_ETC_321":
                            case "BG26_ETC_321_G":
                                const annoyOTroupeSpawns = deadEntity.cardId === "BG26_ETC_321_G"
                                    ? "BG_GVG_085_G"
                                    : "BG_GVG_085";
                                spawnedEntities.push(...(0, exports.spawnEntities)(annoyOTroupeSpawns, 3, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "BG21_025":
                            case "BG21_025_G":
                                const cardParam = 6;
                                const entitiesToSpawn = Math.max(0, Math.min(cardParam, 7 - boardWithDeadEntity.length - spawnedEntities.length));
                                const buffAmount = (deadEntityCardId === "BG21_025_G" ? 2 : 1) *
                                    (cardParam - entitiesToSpawn);
                                spawnedEntities.push(...(0, exports.spawnEntities)(deadEntityCardId === "BG21_025_G"
                                    ? "TB_BaconUps_032t"
                                    : "BG_BOT_312t", entitiesToSpawn, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, true));
                                (0, utils_2.addStatsToBoard)(deadEntity, boardWithDeadEntity, boardWithDeadEntityHero, buffAmount, buffAmount, gameState, reference_data_1.Race[reference_data_1.Race.MECH]);
                                (0, utils_2.addStatsToBoard)(deadEntity, spawnedEntities, boardWithDeadEntityHero, buffAmount, buffAmount, gameState, reference_data_1.Race[reference_data_1.Race.MECH]);
                                break;
                            case "BG26_804":
                            case "BG26_804_G":
                                const stats = deadEntity.scriptDataNum1 ||
                                    ((_c = gameState.sharedState.deaths.find((e) => e.friendly === deadEntity.friendly && e.cardId === deadEntity.cardId)) === null || _c === void 0 ? void 0 : _c.scriptDataNum1) ||
                                    0;
                                const octosariSpawn = deadEntity.cardId === "BG26_804_G"
                                    ? "BG26_803_Gt"
                                    : "BG26_803t";
                                const octoSpawns = (0, exports.spawnEntities)(octosariSpawn, 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false);
                                octoSpawns.forEach((b) => {
                                    b.attack = stats;
                                    b.health = stats;
                                });
                                spawnedEntities.push(...octoSpawns);
                                break;
                            case "BG27_081":
                            case "BG27_081_G":
                                const cultistStharaSpawnNumber = deadEntity.cardId === "BG27_081_G" ? 2 : 1;
                                const cultistStharaSpawnCandidates = gameState.sharedState.deaths
                                    .filter((entity) => entity.friendly === deadEntity.friendly)
                                    .filter((entity) => (0, utils_2.hasCorrectTribe)(entity, boardWithDeadEntityHero, reference_data_1.Race.DEMON, gameState.anomalies, gameState.allCards))
                                    .slice(0, cultistStharaSpawnNumber);
                                cultistStharaSpawnCandidates.forEach((candidate) => {
                                    const spawns = (0, exports.spawnEntities)(candidate.cardId, 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false, false, true);
                                    spawns.forEach((spawn) => {
                                        var _a, _b;
                                        spawn.attack = (_a = candidate.maxAttack) !== null && _a !== void 0 ? _a : candidate.attack;
                                        spawn.health = (_b = candidate.maxHealth) !== null && _b !== void 0 ? _b : candidate.health;
                                        spawn.maxHealth = spawn.health;
                                    });
                                    spawnedEntities.push(...spawns);
                                });
                                break;
                            case "BGDUO_105":
                            case "BGDUO_105_G":
                                const magnanimooseCopies = deadEntity.cardId === "BGDUO_105_G" ? 2 : 1;
                                for (let i = 0; i < magnanimooseCopies; i++) {
                                    const teammateState = (0, utils_2.getTeammateInitialState)(gameState.gameState, boardWithDeadEntityHero);
                                    const teammateBoard = (_d = teammateState === null || teammateState === void 0 ? void 0 : teammateState.board) !== null && _d !== void 0 ? _d : [];
                                    const copied = [];
                                    const minionToCopy = (0, utils_1.pickRandom)(teammateBoard.filter((e) => !copied.includes(e.entityId)));
                                    if (minionToCopy) {
                                        const copy = {
                                            ...minionToCopy,
                                            health: 1,
                                            maxHealth: 1,
                                            enchantments: [...minionToCopy.enchantments],
                                            pendingAttackBuffs: [],
                                        };
                                        spawnedEntities.push(...(0, exports.spawnEntities)(copy.cardId, 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false, false, true, copy));
                                        copied.push(copy.entityId);
                                    }
                                }
                                break;
                            case "BG26_RLK_833":
                                spawnedEntities.push(...(0, exports.spawnEntities)("RLK_833t", 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                                break;
                            case "BGS_040":
                            case "TB_BaconUps_154":
                                const nadinaMultiplier = deadEntityCardId === "TB_BaconUps_154" ? 6 : 3;
                                for (let j = 0; j < nadinaMultiplier; j++) {
                                    const validTargets = boardWithDeadEntity
                                        .filter((e) => (0, utils_2.hasCorrectTribe)(e, boardWithDeadEntityHero, reference_data_1.Race.DRAGON, gameState.anomalies, gameState.allCards))
                                        .filter((entity) => !entity.divineShield);
                                    const target = (0, utils_1.pickRandom)(validTargets);
                                    if (target) {
                                        (0, divine_shield_1.updateDivineShield)(target, boardWithDeadEntity, boardWithDeadEntityHero, otherBoardHero, true, gameState);
                                        gameState.spectator.registerPowerTarget(deadEntity, target, boardWithDeadEntity, boardWithDeadEntityHero, otherBoardHero);
                                    }
                                }
                                break;
                            case "BG_OG_256":
                                (0, utils_2.addStatsToBoard)(deadEntity, boardWithDeadEntity, boardWithDeadEntityHero, 1, 1, gameState);
                                break;
                            case "TB_BaconUps_025":
                                (0, utils_2.addStatsToBoard)(deadEntity, boardWithDeadEntity, boardWithDeadEntityHero, 2, 2, gameState);
                                break;
                            case "YOD_026":
                                (0, utils_2.grantRandomAttack)(deadEntity, boardWithDeadEntity, boardWithDeadEntityHero, deadEntity.attack, gameState);
                                break;
                            case "TB_BaconUps_112":
                                (0, utils_2.grantRandomAttack)(deadEntity, boardWithDeadEntity, boardWithDeadEntityHero, deadEntity.attack, gameState);
                                (0, utils_2.grantRandomAttack)(deadEntity, boardWithDeadEntity, boardWithDeadEntityHero, deadEntity.attack, gameState);
                                break;
                            case "BG29_815":
                            case "BG29_815_G":
                                const nightbaneLoops = deadEntityCardId === "BG29_815_G" ? 2 : 1;
                                for (let j = 0; j < nightbaneLoops; j++) {
                                    const pickedTargetEntityIds = [];
                                    for (let k = 0; k < 2; k++) {
                                        const target = (0, utils_1.pickRandomAlive)(boardWithDeadEntity
                                            .filter((e) => ![
                                            "BG29_815",
                                            "BG29_815_G",
                                        ].includes(e.cardId))
                                            .filter((e) => !pickedTargetEntityIds.includes(e.entityId)));
                                        if (!!target) {
                                            pickedTargetEntityIds.push(target.entityId);
                                            (0, stats_1.modifyStats)(target, deadEntity, deadEntity.attack, 0, boardWithDeadEntity, boardWithDeadEntityHero, gameState);
                                        }
                                    }
                                }
                                break;
                            case "BG21_000":
                            case "BG34_Giant_031":
                                (0, deathrattle_effects_1.applyLeapFroggerEffect)(boardWithDeadEntity, boardWithDeadEntityHero, deadEntity, false, gameState, deadEntity.deathrattleRepeats);
                                break;
                            case "BG21_000_G":
                            case "BG34_Giant_031_G":
                                (0, deathrattle_effects_1.applyLeapFroggerEffect)(boardWithDeadEntity, boardWithDeadEntityHero, deadEntity, true, gameState, deadEntity.deathrattleRepeats);
                                break;
                            case "BG21_001":
                                const target = (0, utils_2.grantRandomStats)(deadEntity, boardWithDeadEntity, boardWithDeadEntityHero, 6, 6, reference_data_1.Race.BEAST, true, gameState);
                                if (!!target) {
                                    gameState.spectator.registerPowerTarget(deadEntity, target, boardWithDeadEntity, boardWithDeadEntityHero, otherBoardHero);
                                }
                                break;
                            case "BG21_001_G":
                                const crocTarget = (0, utils_2.grantRandomStats)(deadEntity, boardWithDeadEntity, boardWithDeadEntityHero, 12, 12, reference_data_1.Race.BEAST, true, gameState);
                                if (!!crocTarget) {
                                    gameState.spectator.registerPowerTarget(deadEntity, crocTarget, boardWithDeadEntity, boardWithDeadEntityHero, otherBoardHero);
                                }
                                break;
                            case "BG25_022":
                            case "BG25_022_G":
                                const scarletMultiplier = deadEntityCardId === "BG25_022_G" ? 2 : 1;
                                const scarletTarget = (0, utils_2.grantRandomStats)(deadEntity, boardWithDeadEntity, boardWithDeadEntityHero, scarletMultiplier * 1, scarletMultiplier * 2, reference_data_1.Race.UNDEAD, false, gameState);
                                if (!!scarletTarget) {
                                    gameState.spectator.registerPowerTarget(deadEntity, scarletTarget, boardWithDeadEntity, boardWithDeadEntityHero, otherBoardHero);
                                }
                                break;
                            case "TB_BaconShop_HERO_17_Buddy":
                            case "TB_BaconShop_HERO_17_Buddy_G":
                                const squirrelDamage = deadEntity.cardId === "TB_BaconShop_HERO_17_Buddy_G"
                                    ? 8
                                    : 4;
                                const numberOfDeadMechsThisCombat = gameState.sharedState.deaths
                                    .filter((entity) => entity.friendly === deadEntity.friendly)
                                    .filter((entity) => (0, utils_2.hasCorrectTribe)(entity, boardWithDeadEntityHero, reference_data_1.Race.MECH, gameState.anomalies, gameState.allCards)).length;
                                for (let j = 0; j < numberOfDeadMechsThisCombat; j++) {
                                    (0, attack_1.dealDamageToRandomEnemy)(otherBoard, otherBoardHero, deadEntity, squirrelDamage, boardWithDeadEntity, boardWithDeadEntityHero, gameState);
                                }
                                break;
                            case "BG_BOT_606":
                            case "TB_BaconUps_028":
                                const kaboomLoops = deadEntity.cardId === "TB_BaconUps_028" ? 2 : 1;
                                const baseDamage = 4 +
                                    boardWithDeadEntityHero.trinkets.filter((t) => t.cardId === "BG30_MagicItem_803").length *
                                        10;
                                for (let j = 0; j < kaboomLoops; j++) {
                                    (0, attack_1.dealDamageToRandomEnemy)(otherBoard, otherBoardHero, deadEntity, baseDamage, boardWithDeadEntity, boardWithDeadEntityHero, gameState);
                                }
                                break;
                            case "BG28_968":
                            case "BG28_968_G":
                                const lighterFighterDamage = deadEntity.cardId === "BG28_968_G" ? 8 : 4;
                                for (let j = 0; j < 2; j++) {
                                    const target = (0, utils_1.pickRandomLowestHealth)(otherBoard);
                                    gameState.spectator.registerPowerTarget(deadEntity, target, otherBoard, boardWithDeadEntityHero, otherBoardHero);
                                    (0, attack_1.dealDamageToMinion)(target, otherBoard, otherBoardHero, deadEntity, lighterFighterDamage, boardWithDeadEntity, boardWithDeadEntityHero, gameState);
                                }
                                break;
                            case "BG25_165":
                            case "BG25_165_G":
                                const boomboxDamage = deadEntity.cardId === "BG25_165_G" ? 14 : 7;
                                const targets = (0, attack_1.findNearestEnemies)(boardWithDeadEntity, null, deadEntityIndexFromRight, otherBoard, 2, gameState.allCards);
                                targets.forEach((target) => {
                                    (0, attack_1.dealDamageToMinion)(target, otherBoard, otherBoardHero, deadEntity, boomboxDamage, boardWithDeadEntity, boardWithDeadEntityHero, gameState);
                                });
                                break;
                            case "BG_FP1_024":
                                const damage = 1;
                                const minionsToDamage = [...otherBoard, ...boardWithDeadEntity];
                                for (const target of minionsToDamage) {
                                    const isSameSide = target.friendly === deadEntity.friendly;
                                    const board = isSameSide ? boardWithDeadEntity : otherBoard;
                                    const hero = isSameSide ? boardWithDeadEntityHero : otherBoardHero;
                                    (0, attack_1.dealDamageToMinion)(target, board, hero, deadEntity, damage, isSameSide ? otherBoard : boardWithDeadEntity, isSameSide ? otherBoardHero : boardWithDeadEntityHero, gameState);
                                }
                                break;
                            case "BG28_900":
                            case "BG28_900_G":
                                const loops = deadEntityCardId === "BG28_900_G" ? 2 : 1;
                                const cardsToAdd = Array(loops).fill(null);
                                (0, cards_in_hand_1.addCardsInHand)(boardWithDeadEntityHero, boardWithDeadEntity, cardsToAdd, gameState);
                                break;
                            case "TB_BaconShop_HERO_41_Buddy":
                            case "TB_BaconShop_HERO_41_Buddy_G":
                                const numberToGild = deadEntityCardId === "TB_BaconShop_HERO_41_Buddy_G" ? 2 : 1;
                                const targetBoard = boardWithDeadEntity.filter((e) => !e.definitelyDead && e.health > 0);
                                for (let i = 0; i < Math.min(numberToGild, boardWithDeadEntity.length); i++) {
                                    const rightMostMinion = targetBoard[targetBoard.length - 1 - i];
                                    if (rightMostMinion) {
                                        (0, golden_1.makeMinionGolden)(rightMostMinion, deadEntity, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState);
                                    }
                                }
                                break;
                            case "BG23_017":
                            case "BG23_017_G":
                                const sanguineChampionStats = deadEntityCardId === "BG23_017" ? 1 : 2;
                                boardWithDeadEntityHero.globalInfo.BloodGemAttackBonus += sanguineChampionStats;
                                boardWithDeadEntityHero.globalInfo.BloodGemHealthBonus += sanguineChampionStats;
                                break;
                            case "BG26_160":
                            case "BG26_160_G":
                                const piperBuff = deadEntityCardId === "BG26_160" ? 1 : 2;
                                boardWithDeadEntityHero.globalInfo.BloodGemAttackBonus += piperBuff;
                                break;
                            case "BG_RLK_957":
                                (0, utils_2.addStatsToBoard)(deadEntity, boardWithDeadEntity, boardWithDeadEntityHero, 2, 1, gameState, reference_data_1.Race[reference_data_1.Race.UNDEAD]);
                                break;
                            case "BG26_GIL_513":
                                (0, utils_2.addStatsToBoard)(deadEntity, boardWithDeadEntity, boardWithDeadEntityHero, 1, 0, gameState, null);
                                break;
                            case "BG_ICC_099":
                                for (const entity of boardWithDeadEntity) {
                                    (0, attack_1.dealDamageToMinion)(entity, boardWithDeadEntity, boardWithDeadEntityHero, deadEntity, 5, otherBoard, otherBoardHero, gameState);
                                }
                                break;
                            case "BG27_080":
                            case "BG27_080_G":
                                const motleyBuff = deadEntity.cardId === "BG27_080_G" ? 2 : 1;
                                (0, utils_2.grantStatsToMinionsOfEachType)(deadEntity, boardWithDeadEntity, boardWithDeadEntityHero, motleyBuff * 1, motleyBuff * 1, gameState);
                                break;
                            case "BG28_304":
                            case "BG28_304_G":
                                const moroesBuffAtk = deadEntity.cardId === "BG28_304_G" ? 8 : 4;
                                const moroesBuffHealth = deadEntity.cardId === "BG28_304_G" ? 12 : 6;
                                (0, utils_2.addStatsToBoard)(deadEntity, boardWithDeadEntity, boardWithDeadEntityHero, moroesBuffAtk, moroesBuffHealth, gameState, reference_data_1.Race[reference_data_1.Race.UNDEAD]);
                                break;
                            case "BG28_306":
                            case "BG28_306_G":
                                const steadfastSpiritBuff = deadEntity.cardId === "BG28_306_G" ? 2 : 1;
                                (0, utils_2.addStatsToBoard)(deadEntity, boardWithDeadEntity, boardWithDeadEntityHero, steadfastSpiritBuff, steadfastSpiritBuff, gameState);
                                break;
                            case "BG26_148":
                            case "BG26_148_G":
                                const scraperToAddQuantity = deadEntity.cardId === "BG26_148_G" ? 2 : 1;
                                const scraperCardsToAdd = [];
                                for (let i = 0; i < scraperToAddQuantity; i++) {
                                    scraperCardsToAdd.push((0, utils_1.pickRandom)(gameState.cardsData.scrapScraperSpawns));
                                }
                                (0, cards_in_hand_1.addCardsInHand)(boardWithDeadEntityHero, boardWithDeadEntity, scraperCardsToAdd, gameState);
                                break;
                            case "BG29_862":
                            case "BG29_862_G":
                                const conjurerToAddQuantity = deadEntity.cardId === "BG29_862_G" ? 2 : 1;
                                const conjurerCardsToAdd = [];
                                for (let i = 0; i < conjurerToAddQuantity; i++) {
                                    conjurerCardsToAdd.push((0, utils_1.pickRandom)(gameState.cardsData.battlecryMinions));
                                }
                                (0, cards_in_hand_1.addCardsInHand)(boardWithDeadEntityHero, boardWithDeadEntity, conjurerCardsToAdd, gameState);
                                break;
                            case "BG29_808":
                            case "BG29_808_G":
                                const spikedSaviorLoops = deadEntity.cardId === "BG29_808_G" ? 2 : 1;
                                for (let j = 0; j < spikedSaviorLoops; j++) {
                                    const targetBoard = [...boardWithDeadEntity];
                                    for (const entity of targetBoard) {
                                        (0, stats_1.modifyStats)(entity, deadEntity, 0, 1, boardWithDeadEntity, boardWithDeadEntityHero, gameState);
                                    }
                                    for (const entity of targetBoard) {
                                        (0, attack_1.dealDamageToMinion)(entity, boardWithDeadEntity, boardWithDeadEntityHero, deadEntity, 1, otherBoard, otherBoardHero, gameState);
                                    }
                                }
                                break;
                            default:
                                if ((_e = deadEntity.additionalCards) === null || _e === void 0 ? void 0 : _e.includes(deadEntityCardId)) {
                                    if (!(0, utils_2.hasMechanic)(gameState.allCards.getCard(deadEntityCardId), reference_data_1.GameTag.DEATHRATTLE)) {
                                        hasTriggeredThisLoop = false;
                                    }
                                }
                                else {
                                    const source = [deadEntity, ...enchantments].find((e) => e.cardId === deadEntityCardId);
                                    if (!(0, utils_2.hasEntityMechanic)(source, reference_data_1.GameTag.DEATHRATTLE, gameState.allCards)) {
                                        hasTriggeredThisLoop = false;
                                    }
                                }
                                break;
                        }
                    }
                    hasTriggered = hasTriggered || hasTriggeredThisLoop;
                    if ((spawnedEntities === null || spawnedEntities === void 0 ? void 0 : spawnedEntities.length) > 0) {
                        const actualSpawns = (0, spawns_1.performEntitySpawns)(spawnedEntities, boardWithDeadEntity, boardWithDeadEntityHero, deadEntity, deadEntityIndexFromRight, otherBoard, otherBoardHero, gameState);
                        finalSpawns.push(...actualSpawns);
                    }
                }
            }
            else {
                const spawnedEntities = [];
                let hasTriggeredThisLoop = true;
                const enchantment = card;
                const deathrattleImpl = _card_mappings_1.cardMappings[enchantment.cardId];
                if ((0, card_interface_1.hasDeathrattleSpawnEnchantment)(deathrattleImpl)) {
                    const spawns = deathrattleImpl.deathrattleSpawnEnchantmentEffect(enchantment, deadEntity, deathrattleTriggeredInput);
                    if (!!(spawns === null || spawns === void 0 ? void 0 : spawns.length)) {
                        spawnedEntities.push(...spawns);
                    }
                }
                else {
                    switch (enchantment.cardId) {
                        case "BG22_HERO_001_Buddy_e1":
                            (0, deathrattle_effects_1.applyEarthInvocationEnchantment)(boardWithDeadEntity, deadEntity, deadEntity, gameState);
                            break;
                        case "BG22_HERO_001_Buddy_e2":
                            (0, deathrattle_effects_1.applyFireInvocationEnchantment)(boardWithDeadEntity, boardWithDeadEntityHero, deadEntity, deadEntity, gameState);
                            break;
                        case "BG22_HERO_001_Buddy_e3":
                            (0, deathrattle_effects_1.applyWaterInvocationEnchantment)(boardWithDeadEntity, boardWithDeadEntityHero, otherBoardHero, deadEntity, deadEntity, gameState);
                            break;
                        case "BG22_HERO_001_Buddy_e4":
                            (0, deathrattle_effects_1.applyLightningInvocationEnchantment)(boardWithDeadEntity, boardWithDeadEntityHero, deadEntity, otherBoard, otherBoardHero, gameState);
                            break;
                        case "BG_BOT_312e":
                            spawnedEntities.push(...(0, exports.spawnEntities)("BG_BOT_312t", 3, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                            break;
                        case "TB_BaconUps_032e":
                            spawnedEntities.push(...(0, exports.spawnEntities)("TB_BaconUps_032t", 3, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                            break;
                        case "UNG_999t2e":
                            spawnedEntities.push(...(0, exports.spawnEntities)("UNG_999t2t1", 2, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                            break;
                        case "BG22_HERO_001p_t1e":
                            spawnedEntities.push(...(0, exports.spawnEntities)("BG22_HERO_001p_t1et", 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                            break;
                        case "BG28_603e":
                            spawnedEntities.push(...(0, exports.spawnEntities)("BG28_603t", 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                            break;
                        case "BG29_875e":
                            spawnedEntities.push(...(0, exports.spawnEntities)("BG29_875t", 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                            break;
                        case "BG29_875_Ge":
                            spawnedEntities.push(...(0, exports.spawnEntities)("BG29_875_Gt", 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                            break;
                        case "BG21_HERO_030pe":
                            const tavernTier = (_f = deadEntity.tavernTier) !== null && _f !== void 0 ? _f : gameState.cardsData.getTavernLevel(deadEntity.cardId);
                            spawnedEntities.push(...(0, exports.spawnEntities)(gameState.cardsData.getRandomMinionForTavernTier(Math.max(1, tavernTier - 1)), 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                            break;
                        case "BG30_MagicItem_952e":
                            spawnedEntities.push(...(0, exports.spawnEntities)("BG26_537", 1, boardWithDeadEntity, boardWithDeadEntityHero, otherBoard, otherBoardHero, gameState, deadEntity.friendly, false));
                            break;
                        case "BG30_MagicItem_917e":
                            (0, cards_in_hand_1.addCardsInHand)(boardWithDeadEntityHero, boardWithDeadEntity, [null], gameState);
                            break;
                        case "BG30_MagicItem_411e":
                            (0, cards_in_hand_1.addCardsInHand)(boardWithDeadEntityHero, boardWithDeadEntity, ["BG20_GEM"], gameState);
                            break;
                        case "BG21_000e":
                        case "BG21_000_Ge":
                            (0, deathrattle_effects_1.applyLeapFroggerEffect)(boardWithDeadEntity, boardWithDeadEntityHero, deadEntity, enchantment.cardId === "BG21_000_Ge", gameState, enchantment.repeats || 1);
                            break;
                        default:
                            hasTriggeredThisLoop = false;
                            break;
                    }
                }
                hasTriggered = hasTriggered || hasTriggeredThisLoop;
                if ((spawnedEntities === null || spawnedEntities === void 0 ? void 0 : spawnedEntities.length) > 0) {
                    const actualSpawns = (0, spawns_1.performEntitySpawns)(spawnedEntities, boardWithDeadEntity, boardWithDeadEntityHero, deadEntity, deadEntityIndexFromRight, otherBoard, otherBoardHero, gameState);
                    finalSpawns.push(...actualSpawns);
                }
            }
            if (hasTriggered) {
                (0, deathrattle_on_trigger_1.onDeathrattleTriggered)(deathrattleTriggeredInput);
            }
        }
    }
    return finalSpawns;
};
exports.spawnEntitiesFromDeathrattle = spawnEntitiesFromDeathrattle;
//# sourceMappingURL=deathrattle-spawns.js.map