"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.processMinionDeath = exports.bumpEntities = exports.getDefendingEntity = exports.dealDamageToMinion = exports.dealDamageToRandomEnemy = exports.getLeftNeighbour = exports.getNeighbours = exports.findNearestEnemies = exports.doFullAttack = exports.simulateAttack = void 0;
const card_interface_1 = require("../cards/card.interface");
const _card_mappings_1 = require("../cards/impl/_card-mappings");
const debug_state_1 = require("../debug-state");
const divine_shield_1 = require("../keywords/divine-shield");
const venomous_1 = require("../keywords/venomous");
const utils_1 = require("../services/utils");
const utils_2 = require("../utils");
const after_attack_1 = require("./after-attack");
const damage_effects_1 = require("./damage-effects");
const deathrattle_effects_1 = require("./deathrattle-effects");
const deathrattle_orchestration_1 = require("./deathrattle-orchestration");
const deathrattle_spawns_1 = require("./deathrattle-spawns");
const minion_death_1 = require("./minion-death");
const minion_kill_1 = require("./minion-kill");
const on_attack_1 = require("./on-attack");
const on_being_attacked_1 = require("./on-being-attacked");
const spawns_1 = require("./spawns");
const stats_1 = require("./stats");
const summon_when_space_1 = require("./summon-when-space");
const entity_utils_1 = require("./utils/entity-utils");
const simulateAttack = (attackingBoard, attackingBoardHero, defendingBoard, defendingBoardHero, gameState, forceTarget = null) => {
    if (attackingBoard.length === 0 || defendingBoard.length === 0) {
        return;
    }
    const attackingEntity = getAttackingEntity(attackingBoard, gameState.allCards);
    if (attackingEntity) {
        gameState.sharedState.currentAttackerEntityId = attackingEntity.entityId;
        const attackingEntityIndex = attackingBoard.indexOf(attackingEntity);
        const attackingEntitiesToTheLeft = attackingBoard.slice(0, attackingEntityIndex);
        const isAttackingImmediately = attackingEntity.attackImmediately;
        if (!isAttackingImmediately) {
            attackingEntitiesToTheLeft.forEach((entity) => (entity.hasAttacked = 2));
        }
        else {
            attackingEntity.attackImmediately = false;
        }
        const numberOfAttacks = attackingEntity.windfury ? 2 : 1;
        for (let i = 0; i < numberOfAttacks; i++) {
            if (attackingBoard.length === 0 || defendingBoard.length === 0) {
                break;
            }
            if (attackingBoard.find((entity) => entity.entityId === attackingEntity.entityId)) {
                const defendingEntity = forceTarget !== null && forceTarget !== void 0 ? forceTarget : (0, exports.getDefendingEntity)(defendingBoard, attackingEntity);
                if (defendingEntity && defendingEntity.health > 0 && !defendingEntity.definitelyDead) {
                    (0, exports.doFullAttack)(attackingEntity, attackingBoard, attackingBoardHero, defendingEntity, defendingBoard, defendingBoardHero, gameState);
                }
                else {
                    attackingEntity.attackImmediately = false;
                }
            }
        }
        gameState.sharedState.currentAttackerEntityId = null;
        attackingEntity.hasAttacked = 1;
    }
    return attackingEntity;
};
exports.simulateAttack = simulateAttack;
const doFullAttack = (attackingEntity, attackingBoard, attackingBoardHero, defendingEntity, defendingBoard, defendingBoardHero, gameState) => {
    var _a;
    const isAttackingImmediately = attackingEntity.attackImmediately;
    gameState.spectator.registerAttack(attackingEntity, defendingEntity, attackingBoard, defendingBoard, attackingBoardHero, defendingBoardHero);
    (0, on_being_attacked_1.applyOnBeingAttackedBuffs)(attackingEntity, attackingBoard, attackingBoardHero, defendingEntity, defendingBoard, defendingBoardHero, gameState);
    const { damageDoneByAttacker: damageDoneByAttacker1, damageDoneByDefender: damageDoneByDefender1 } = (0, on_attack_1.applyOnAttackEffects)(attackingEntity, attackingBoard, attackingBoardHero, defendingEntity, defendingBoard, defendingBoardHero, gameState);
    const { damageDoneByAttacker: damageDoneByAttacker2, damageDoneByDefender: damageDoneByDefender2 } = performAttack(attackingEntity, defendingEntity, attackingBoard, attackingBoardHero, defendingBoard, defendingBoardHero, gameState);
    const damageDoneByAttacker = damageDoneByAttacker1 + damageDoneByAttacker2;
    const damageDoneByDefender = damageDoneByDefender1 + damageDoneByDefender2;
    (0, after_attack_1.applyAfterAttackEffects)(attackingEntity, attackingBoard, attackingBoardHero, defendingEntity, defendingBoard, defendingBoardHero, damageDoneByAttacker, damageDoneByDefender, gameState);
    (0, exports.processMinionDeath)(attackingBoard, attackingBoardHero, defendingBoard, defendingBoardHero, gameState, isAttackingImmediately);
    (0, after_attack_1.applyAfterAttackTrinkets)(attackingEntity, attackingBoard, attackingBoardHero, defendingEntity, defendingBoard, defendingBoardHero, damageDoneByAttacker, damageDoneByDefender, gameState);
    (0, stats_1.applyAfterStatsUpdate)(gameState);
    attackingEntity.immuneWhenAttackCharges = Math.max(0, ((_a = attackingEntity.immuneWhenAttackCharges) !== null && _a !== void 0 ? _a : 0) - 1);
};
exports.doFullAttack = doFullAttack;
const performAttack = (attackingEntity, defendingEntity, attackingBoard, attackingBoardHero, defendingBoard, defendingBoardHero, gameState) => {
    let damageDoneByAttacker = 0;
    let damageDoneByDefender = 0;
    if (["BG22_001", "BG22_001_G"].includes(attackingEntity.cardId)) {
        const spawns = (0, deathrattle_spawns_1.spawnEntities)(attackingEntity.cardId === "BG22_001_G"
            ? "BG22_001_G"
            : "BG22_001t2", 1, attackingBoard, attackingBoardHero, defendingBoard, defendingBoardHero, gameState, true, true);
        if (spawns.length > 0) {
            const sourceIndex = attackingBoard.indexOf(attackingEntity);
            const actualSpawns = (0, spawns_1.performEntitySpawns)(spawns, attackingBoard, attackingBoardHero, attackingEntity, sourceIndex, defendingBoard, defendingBoardHero, gameState);
            for (const actualSpawn of actualSpawns) {
                if (defendingEntity.health > 0 && !defendingEntity.definitelyDead) {
                    performAttack(actualSpawn, defendingEntity, attackingBoard, attackingBoardHero, defendingBoard, defendingBoardHero, gameState);
                }
            }
        }
    }
    const defenderAliveBeforeAttack = defendingEntity.health > 0 && !defendingEntity.definitelyDead;
    const attackerHadDivineShield = attackingEntity.divineShield;
    const defenderHadDivineShield = defendingEntity.divineShield;
    const defenderNeighbours = (0, exports.getNeighbours)(defendingBoard, defendingEntity);
    if (defenderAliveBeforeAttack) {
        if (!attackingEntity.immuneWhenAttackCharges) {
            damageDoneByDefender += (0, exports.bumpEntities)(attackingEntity, defendingEntity, attackingBoard, attackingBoardHero, defendingBoard, defendingBoardHero, gameState);
        }
        damageDoneByAttacker += (0, exports.bumpEntities)(defendingEntity, attackingEntity, defendingBoard, defendingBoardHero, attackingBoard, attackingBoardHero, gameState);
        if (defendingEntity.attack > 0 && attackerHadDivineShield && !attackingEntity.immuneWhenAttackCharges) {
            (0, divine_shield_1.updateDivineShield)(attackingEntity, attackingBoard, attackingBoardHero, defendingBoardHero, false, gameState);
        }
        if (attackingEntity.attack > 0 && defenderHadDivineShield) {
            (0, divine_shield_1.updateDivineShield)(defendingEntity, defendingBoard, defendingBoardHero, attackingBoardHero, false, gameState);
        }
        if (damageDoneByDefender > 0) {
            (0, damage_effects_1.onEntityDamaged)(attackingEntity, attackingBoard, attackingBoardHero, defendingBoard, defendingBoardHero, defendingEntity, damageDoneByDefender, gameState);
        }
        if (damageDoneByAttacker > 0) {
            (0, damage_effects_1.onEntityDamaged)(defendingEntity, defendingBoard, defendingBoardHero, attackingBoard, attackingBoardHero, attackingEntity, damageDoneByAttacker, gameState);
        }
        if (defendingEntity.health <= 0 || defendingEntity.definitelyDead) {
            const { dmgDoneByAttacker, dmgDoneByDefender } = (0, minion_kill_1.onMinionKill)(attackingEntity, true, defendingEntity, attackingBoard, attackingBoardHero, defendingBoard, defendingBoardHero, defenderNeighbours, gameState);
            damageDoneByAttacker += dmgDoneByAttacker;
            damageDoneByDefender += dmgDoneByDefender;
        }
        if (attackingEntity.health <= 0 || attackingEntity.definitelyDead) {
            const { dmgDoneByAttacker, dmgDoneByDefender } = (0, minion_kill_1.onMinionKill)(defendingEntity, false, attackingEntity, defendingBoard, defendingBoardHero, attackingBoard, attackingBoardHero, defenderNeighbours, gameState);
            damageDoneByAttacker += dmgDoneByAttacker;
            damageDoneByDefender += dmgDoneByDefender;
        }
    }
    if (attackingEntity.cleave) {
        for (const neighbour of defenderNeighbours) {
            const thisAttackDamage = (0, exports.bumpEntities)(neighbour, attackingEntity, defendingBoard, defendingBoardHero, attackingBoard, attackingBoardHero, gameState);
            damageDoneByAttacker += thisAttackDamage;
            if (attackingEntity.attack > 0 && neighbour.divineShield) {
                (0, divine_shield_1.updateDivineShield)(neighbour, defendingBoard, defendingBoardHero, attackingBoardHero, false, gameState);
            }
            if (thisAttackDamage > 0) {
                (0, damage_effects_1.onEntityDamaged)(neighbour, defendingBoard, defendingBoardHero, attackingBoard, attackingBoardHero, attackingEntity, thisAttackDamage, gameState);
            }
            if (neighbour.health <= 0 || neighbour.definitelyDead) {
                const { dmgDoneByAttacker, dmgDoneByDefender } = (0, minion_kill_1.onMinionKill)(attackingEntity, true, neighbour, attackingBoard, attackingBoardHero, defendingBoard, defendingBoardHero, defenderNeighbours, gameState);
                damageDoneByAttacker += dmgDoneByAttacker;
                damageDoneByDefender += dmgDoneByDefender;
            }
        }
    }
    attackingEntity.attackImmediately = false;
    if (attackingEntity.enchantments.some((e) => e.cardId === "BG24_Reward_364e")) {
        attackingEntity.definitelyDead = true;
    }
    return { damageDoneByAttacker, damageDoneByDefender };
};
const getAttackingEntity = (attackingBoard, allCards) => {
    let validAttackers = attackingBoard.filter((entity) => (0, entity_utils_1.canAttack)(entity));
    if (validAttackers.length === 0) {
        return null;
    }
    if (validAttackers.some((entity) => entity.attackImmediately)) {
        validAttackers = validAttackers.filter((entity) => entity.attackImmediately);
    }
    else if (validAttackers.every((e) => e.hasAttacked)) {
        attackingBoard.forEach((e) => (e.hasAttacked = 0));
    }
    else {
        validAttackers = validAttackers.filter((entity) => !entity.hasAttacked);
    }
    const attacker = validAttackers[0];
    return attacker;
};
const findNearestEnemies = (attackingBoard, entity, entityIndexFromRight, defendingBoard, numberOfTargets, allCards) => {
    const result = [];
    if (defendingBoard.length > 0) {
        const attackerIndex = attackingBoard.length - entityIndexFromRight - 1;
        const targetIndex = attackerIndex - (attackingBoard.length - defendingBoard.length) / 2;
        for (let i = 0; i < numberOfTargets; i++) {
            const possibleTargets = defendingBoard
                .filter((e) => !e.definitelyDead && e.health > 0)
                .filter((e) => !result.includes(e));
            if (!possibleTargets.length) {
                break;
            }
            const targetGroups = (0, utils_1.groupByFunction)((e) => Math.abs(defendingBoard.indexOf(e) - targetIndex))(possibleTargets);
            const distances = Object.keys(targetGroups)
                .map((k) => +k)
                .sort();
            const nearestDistance = distances[0];
            if (nearestDistance != null) {
                const target = (0, utils_1.pickRandom)(targetGroups[nearestDistance]);
                result.push(target);
            }
        }
    }
    return result.filter((e) => !!e);
};
exports.findNearestEnemies = findNearestEnemies;
const getNeighbours = (board, entity, deadEntityIndexFromRight) => {
    const neighbours = [];
    if (deadEntityIndexFromRight != null && !board.includes(entity)) {
        const leftNeighbourIndex = board.length - 1 - deadEntityIndexFromRight;
        const leftNeighbour = board[leftNeighbourIndex];
        if (leftNeighbour) {
            neighbours.push(leftNeighbour);
        }
        const rightNeighbourIndex = board.length - 1 - (deadEntityIndexFromRight - 1);
        const rightNeighbour = board[rightNeighbourIndex];
        if (rightNeighbour) {
            neighbours.push(rightNeighbour);
        }
    }
    else {
        const index = board.map((e) => e.entityId).indexOf(entity.entityId);
        if (index - 1 >= 0) {
            neighbours.push(board[index - 1]);
        }
        if (index + 1 < board.length) {
            neighbours.push(board[index + 1]);
        }
    }
    return neighbours;
};
exports.getNeighbours = getNeighbours;
const getLeftNeighbour = (board, entity, deadEntityIndexFromRight) => {
    if (deadEntityIndexFromRight != null && !board.includes(entity)) {
        const leftNeighbourIndex = board.length - 1 - deadEntityIndexFromRight;
        const leftNeighbour = board[leftNeighbourIndex];
        if (leftNeighbour) {
            return leftNeighbour;
        }
    }
    else {
        const index = board.map((e) => e.entityId).indexOf(entity.entityId);
        if (index - 1 >= 0) {
            return board[index - 1];
        }
    }
    return null;
};
exports.getLeftNeighbour = getLeftNeighbour;
const dealDamageToRandomEnemy = (boardToBeDamaged, boardToBeDamagedHero, damageSource, damage, boardWithAttackOrigin, boardWithAttackOriginHero, gameState) => {
    if (boardToBeDamaged.length === 0) {
        return;
    }
    const validTargets = boardToBeDamaged.filter((e) => e.health > 0 && !e.definitelyDead);
    const defendingEntity = (0, utils_1.pickRandom)(validTargets);
    if (defendingEntity) {
        gameState.spectator.registerPowerTarget(damageSource, defendingEntity, boardToBeDamaged, boardToBeDamagedHero, boardWithAttackOriginHero);
        (0, exports.dealDamageToMinion)(defendingEntity, boardToBeDamaged, boardToBeDamagedHero, damageSource, damage, boardWithAttackOrigin, boardWithAttackOriginHero, gameState);
    }
};
exports.dealDamageToRandomEnemy = dealDamageToRandomEnemy;
const dealDamageToMinion = (target, board, hero, damageSource, damage, otherBoard, otherHero, gameState) => {
    if (!target) {
        return 0;
    }
    const isDeadBeforeDamage = target.definitelyDead || target.health <= 0;
    const spawns = [];
    const fakeAttacker = {
        ...(damageSource || {}),
        entityId: -1,
        attack: damage,
    };
    const actualDamageDone = (0, exports.bumpEntities)(target, fakeAttacker, board, hero, otherBoard, otherHero, gameState);
    if (fakeAttacker.attack > 0 && target.divineShield) {
        (0, divine_shield_1.updateDivineShield)(target, board, hero, otherHero, false, gameState);
    }
    if (actualDamageDone > 0) {
        const newSpawns = (0, damage_effects_1.onEntityDamaged)(target, board, hero, otherBoard, otherHero, damageSource, actualDamageDone, gameState);
    }
    if (!isDeadBeforeDamage && actualDamageDone > 0 && 'attack' in damageSource && 'health' in damageSource) {
        target.lastAffectedByEntity = damageSource;
        if (target.health <= 0 || target.definitelyDead) {
            (0, minion_kill_1.onMinionKill)(damageSource, false, target, otherBoard, otherHero, board, hero, [], gameState);
        }
    }
    const defendingEntityIndex = board.map((entity) => entity.entityId).indexOf(target.entityId);
    board[defendingEntityIndex] = target;
    return actualDamageDone;
};
exports.dealDamageToMinion = dealDamageToMinion;
const getDefendingEntity = (defendingBoard, attackingEntity, ignoreTaunts = false) => {
    if (debug_state_1.debugState.active) {
        for (const forcedFaceOff of debug_state_1.debugState.forcedFaceOff) {
            if (debug_state_1.debugState.isCorrectEntity(forcedFaceOff.attacker, attackingEntity)) {
                const def = defendingBoard.find((e) => debug_state_1.debugState.isCorrectEntity(forcedFaceOff.defender, e));
                if (!!def) {
                    debug_state_1.debugState.forcedFaceOff = debug_state_1.debugState.forcedFaceOff.filter((f) => f != forcedFaceOff);
                    return def;
                }
            }
        }
    }
    let possibleDefenders;
    if (attackingEntity.cardId === "BGS_022" ||
        attackingEntity.cardId === "TB_BaconUps_091" ||
        attackingEntity.cardId === "BG33_845" ||
        attackingEntity.cardId === "BG33_845_G") {
        const minAttack = Math.min(...defendingBoard.map((entity) => entity.attack));
        possibleDefenders = defendingBoard.filter((entity) => entity.attack === minAttack);
    }
    else if (attackingEntity.cardId === "BG26_921" ||
        attackingEntity.cardId === "BG26_921_G") {
        possibleDefenders = defendingBoard
            .filter((entity) => entity.health <= attackingEntity.attack || attackingEntity.venomous || attackingEntity.poisonous)
            .filter((e) => !e.divineShield);
        if (!possibleDefenders.length) {
            possibleDefenders = defendingBoard;
        }
    }
    else {
        possibleDefenders = defendingBoard.filter((e) => !e.stealth);
        if (!ignoreTaunts) {
            const taunts = possibleDefenders.filter((entity) => entity.taunt);
            possibleDefenders = taunts.length > 0 ? taunts : possibleDefenders;
        }
    }
    const chosenDefender = (0, utils_1.pickRandom)(possibleDefenders);
    return chosenDefender;
};
exports.getDefendingEntity = getDefendingEntity;
const bumpEntities = (entity, bumpInto, entityBoard, entityBoardHero, otherBoard, otherHero, gameState, applyVenomous = true) => {
    const debug = bumpInto.cardId === 'BG26_888';
    if (bumpInto.attack === 0) {
        return 0;
    }
    if (entity.abiityChargesLeft > 0 &&
        (entity.cardId === "BG28_404" || entity.cardId === "BG28_404_G")) {
        entity.abiityChargesLeft--;
        const newTarget = (0, utils_1.pickRandom)(otherBoard);
        if (newTarget) {
            gameState.spectator.registerPowerTarget(entity, newTarget, otherBoard, entityBoardHero, otherHero);
            const newSource = {
                ...entity,
                attack: bumpInto.attack,
            };
            const defenderHadDivineShield = newTarget.divineShield;
            const damageDone = (0, exports.bumpEntities)(newTarget, newSource, otherBoard, otherHero, entityBoard, entityBoardHero, gameState, false);
            if (newSource.attack > 0 && defenderHadDivineShield) {
                (0, divine_shield_1.updateDivineShield)(newTarget, otherBoard, otherHero, entityBoardHero, false, gameState);
            }
            if (damageDone > 0) {
                (0, damage_effects_1.onEntityDamaged)(newTarget, otherBoard, otherHero, entityBoard, entityBoardHero, newSource, damageDone, gameState);
            }
            return damageDone;
        }
    }
    if (entity.divineShield) {
        gameState.spectator.registerDamageDealt(bumpInto, entity, 0, entityBoard);
        return 0;
    }
    const damageDealt = (entity.damageMultiplier || 1) * bumpInto.attack;
    entity.health = entity.health - damageDealt;
    gameState.spectator.registerDamageDealt(bumpInto, entity, damageDealt, entityBoard);
    if (bumpInto.poisonous) {
        entity.definitelyDead = true;
    }
    if (bumpInto.venomous && applyVenomous) {
        entity.definitelyDead = true;
        (0, venomous_1.updateVenomous)(bumpInto, false, otherBoard, otherHero, entityBoardHero, gameState);
    }
    entity.lastAffectedByEntity = bumpInto;
    return bumpInto.attack;
};
exports.bumpEntities = bumpEntities;
const processMinionDeath = (board1, board1Hero, board2, board2Hero, gameState, skipSummonWhenSpace = false) => {
    const [deadMinionIndexesFromLeft1, deadMinionIndexesFromRights1, deadEntities1] = (0, minion_death_1.makeMinionsDie)(board1, board1Hero, board2, board2Hero, gameState);
    const [deadMinionIndexesFromLeft2, deadMinionIndexesFromRights2, deadEntities2] = (0, minion_death_1.makeMinionsDie)(board2, board2Hero, board1, board1Hero, gameState);
    if (deadEntities1.length === 0 && deadEntities2.length === 0) {
        return;
    }
    gameState.spectator.registerDeadEntities(deadMinionIndexesFromRights1, deadEntities1, board1, deadMinionIndexesFromRights2, deadEntities2, board2);
    gameState.sharedState.deaths.push(...deadEntities1.map((e, index) => (0, utils_2.addImpliedMechanics)({
        ...e,
        health: e.maxHealth,
        definitelyDead: false,
        indexFromLeftAtTimeOfDeath: deadMinionIndexesFromLeft1[index],
    }, gameState.cardsData)));
    gameState.sharedState.deaths.push(...deadEntities2.map((e, index) => (0, utils_2.addImpliedMechanics)({
        ...e,
        health: e.maxHealth,
        definitelyDead: false,
        indexFromLeftAtTimeOfDeath: deadMinionIndexesFromLeft2[index],
    }, gameState.cardsData)));
    for (const deadEntity of deadEntities1) {
        const onDeathImpl = _card_mappings_1.cardMappings[deadEntity.cardId];
        if ((0, card_interface_1.hasOnDeath)(onDeathImpl)) {
            onDeathImpl.onDeath(deadEntity, {
                hero: board1Hero,
                board: board1,
                gameState: gameState,
            });
        }
    }
    for (const deadEntity of deadEntities2) {
        const onDeathImpl = _card_mappings_1.cardMappings[deadEntity.cardId];
        if ((0, card_interface_1.hasOnDeath)(onDeathImpl)) {
            onDeathImpl.onDeath(deadEntity, {
                hero: board2Hero,
                board: board2,
                gameState: gameState,
            });
        }
    }
    board1Hero.globalInfo.EternalKnightsDeadThisGame =
        board1Hero.globalInfo.EternalKnightsDeadThisGame +
            deadEntities1.filter((e) => e.cardId === "BG25_008" || e.cardId === "BG25_008_G").length;
    board2Hero.globalInfo.EternalKnightsDeadThisGame =
        board2Hero.globalInfo.EternalKnightsDeadThisGame +
            deadEntities2.filter((e) => e.cardId === "BG25_008" || e.cardId === "BG25_008_G").length;
    (0, deathrattle_orchestration_1.orchestrateMinionDeathEffects)({
        gameState: gameState,
        playerDeadEntities: board1Hero.friendly ? deadEntities1 : deadEntities2,
        playerDeadEntityIndexesFromRight: board1Hero.friendly
            ? deadMinionIndexesFromRights1
            : deadMinionIndexesFromRights2,
        opponentDeadEntities: board1Hero.friendly ? deadEntities2 : deadEntities1,
        opponentDeadEntityIndexesFromRight: board1Hero.friendly
            ? deadMinionIndexesFromRights2
            : deadMinionIndexesFromRights1,
    });
    (0, exports.processMinionDeath)(board1, board1Hero, board2, board2Hero, gameState);
    if (!skipSummonWhenSpace) {
        (0, summon_when_space_1.handleSummonsWhenSpace)(board1, board1Hero, board2, board2Hero, gameState);
    }
    handleAfterMinionsDeaths(board1, deadEntities1, board1Hero, board2, deadEntities2, board2Hero, gameState);
    board1
        .filter((entity) => (0, utils_2.isFish)(entity))
        .forEach((entity) => (0, deathrattle_effects_1.rememberDeathrattles)(entity, deadEntities1, gameState.cardsData, gameState.allCards, gameState.sharedState));
    board2
        .filter((entity) => (0, utils_2.isFish)(entity))
        .forEach((entity) => (0, deathrattle_effects_1.rememberDeathrattles)(entity, deadEntities2, gameState.cardsData, gameState.allCards, gameState.sharedState));
};
exports.processMinionDeath = processMinionDeath;
const handleAfterMinionsDeaths = (board1, deadEntities1, heroEntity1, board2, deadEntities2, heroEntity2, gameState) => {
    const random = Math.random() > 0.5;
    handleAfterMinionsDeathsForBoard(random ? board1 : board2, random ? deadEntities1 : deadEntities2, random ? heroEntity1 : heroEntity2, random ? board2 : board1, random ? deadEntities2 : deadEntities1, random ? heroEntity2 : heroEntity1, gameState);
    handleAfterMinionsDeathsForBoard(!random ? board1 : board2, !random ? deadEntities1 : deadEntities2, !random ? heroEntity1 : heroEntity2, !random ? board2 : board1, !random ? deadEntities2 : deadEntities1, !random ? heroEntity2 : heroEntity1, gameState);
};
const handleAfterMinionsDeathsForBoard = (friendlyBoard, friendlyDeadEntities, friendlyHeroEntity, otherBoard, otherDeadEntities, otherHeroEntity, gameState) => {
    var _a;
    const candidateEntities = [];
    for (const trinket of (_a = friendlyHeroEntity.trinkets) !== null && _a !== void 0 ? _a : []) {
        const onAfterDeathImpl = _card_mappings_1.cardMappings[trinket.cardId];
        if ((0, card_interface_1.hasOnAfterDeath)(onAfterDeathImpl)) {
            onAfterDeathImpl.onAfterDeath(trinket, {
                hero: friendlyHeroEntity,
                board: friendlyBoard,
                otherHero: otherHeroEntity,
                otherBoard: otherBoard,
                deadEntities: friendlyDeadEntities,
                gameState: gameState,
            });
        }
    }
    for (const entity of friendlyBoard) {
        const onAfterDeathImpl = _card_mappings_1.cardMappings[entity.cardId];
        if ((0, card_interface_1.hasOnAfterDeath)(onAfterDeathImpl)) {
            onAfterDeathImpl.onAfterDeath(entity, {
                hero: friendlyHeroEntity,
                board: friendlyBoard,
                otherHero: otherHeroEntity,
                otherBoard: otherBoard,
                deadEntities: friendlyDeadEntities,
                gameState: gameState,
            });
        }
    }
    const secretTriggered = null;
    (0, spawns_1.performEntitySpawns)(candidateEntities, friendlyBoard, friendlyHeroEntity, secretTriggered, 0, otherBoard, otherHeroEntity, gameState);
};
//# sourceMappingURL=attack.js.map