"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.performEntitySpawns = void 0;
const add_minion_to_board_1 = require("./add-minion-to-board");
const attack_1 = require("./attack");
const spawn_fail_1 = require("./spawn-fail");
const performEntitySpawns = (candidateEntities, boardWithKilledMinion, boardWithKilledMinionHero, spawnSourceEntity, spawnSourceEntityIndexFromRight, opponentBoard, opponentBoardHero, gameState, applySelfAuras = true, forceTarget = null) => {
    var _a;
    const aliveEntites = candidateEntities.filter((entity) => entity.health > 0 && !entity.definitelyDead);
    const spawnedEntities = [];
    for (let i = 0; i < aliveEntites.length; i++) {
        const newMinion = aliveEntites[i];
        if (boardWithKilledMinion.length >= 7) {
            for (let j = i; j < aliveEntites.length; j++) {
                (0, spawn_fail_1.onMinionFailedToSpawn)(aliveEntites[j], boardWithKilledMinion, boardWithKilledMinionHero, gameState);
            }
            break;
        }
        spawnSourceEntityIndexFromRight = (_a = newMinion.spawnIndexFromRight) !== null && _a !== void 0 ? _a : spawnSourceEntityIndexFromRight;
        const indexToSpawnAt = Math.max(0, boardWithKilledMinion.length - spawnSourceEntityIndexFromRight);
        (0, add_minion_to_board_1.addMinionToBoard)(boardWithKilledMinion, boardWithKilledMinionHero, opponentBoard, opponentBoardHero, indexToSpawnAt, newMinion, gameState, true, applySelfAuras);
        if (newMinion.attackImmediately) {
            const actualAttacker = (0, attack_1.simulateAttack)(boardWithKilledMinion, boardWithKilledMinionHero, opponentBoard, opponentBoardHero, gameState, { forceTarget: forceTarget });
            if (actualAttacker) {
                actualAttacker.attackImmediately = false;
            }
            else {
                newMinion.attackImmediately = false;
            }
        }
        if (newMinion.health > 0 && !newMinion.definitelyDead) {
            spawnedEntities.push(newMinion);
        }
    }
    gameState.spectator.registerMinionsSpawn(spawnSourceEntity, boardWithKilledMinion, spawnedEntities);
    return spawnedEntities;
};
exports.performEntitySpawns = performEntitySpawns;
//# sourceMappingURL=spawns.js.map