"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleIllidanHeroPowers = void 0;
const attack_1 = require("../attack");
const stats_1 = require("../stats");
const summon_when_space_1 = require("../summon-when-space");
const handleIllidanHeroPowers = (playerEntity, playerBoard, opponentEntity, opponentBoard, currentAttacker, gameState) => {
    let shouldForce = false;
    if (Math.random() < 0.5) {
        const { attacker: newAttacker, force: force1 } = handlePlayerIllidanHeroPowers(playerEntity, playerBoard, opponentEntity, opponentBoard, currentAttacker, true, gameState);
        const { attacker: newAttacker2, force: force2 } = handlePlayerIllidanHeroPowers(opponentEntity, opponentBoard, playerEntity, playerBoard, newAttacker, false, gameState);
        shouldForce = force1 || force2;
        currentAttacker = newAttacker2;
    }
    else {
        const { attacker: newAttacker, force: force1 } = handlePlayerIllidanHeroPowers(opponentEntity, opponentBoard, playerEntity, playerBoard, currentAttacker, false, gameState);
        const { attacker: newAttacker2, force: force2 } = handlePlayerIllidanHeroPowers(playerEntity, playerBoard, opponentEntity, opponentBoard, newAttacker, true, gameState);
        shouldForce = force1 || force2;
        currentAttacker = newAttacker2;
    }
    (0, attack_1.processMinionDeath)(playerBoard, playerEntity, opponentBoard, opponentEntity, gameState);
    const shouldRecomputeCurrentAttacker = (0, summon_when_space_1.handleSummonsWhenSpace)(playerBoard, playerEntity, opponentBoard, opponentEntity, gameState);
    if (shouldForce) {
        return { attacker: currentAttacker, force: true };
    }
    if (shouldRecomputeCurrentAttacker) {
        currentAttacker =
            playerBoard.length > opponentBoard.length
                ? 0
                : opponentBoard.length > playerBoard.length
                    ? 1
                    : Math.round(Math.random());
    }
    return { attacker: currentAttacker, force: false };
};
exports.handleIllidanHeroPowers = handleIllidanHeroPowers;
const handlePlayerIllidanHeroPowers = (playerEntity, playerBoard, opponentEntity, opponentBoard, currentAttacker, friendly, gameState) => {
    var _a;
    const loops = ((_a = playerEntity.trinkets) === null || _a === void 0 ? void 0 : _a.some((t) => t.cardId === "BG32_MagicItem_365"))
        ? 2
        : 1;
    for (let i = 0; i < loops; i++) {
        if (playerEntity.hpLeft <= 0) {
            return { attacker: currentAttacker, force: false };
        }
        for (const heroPower of playerEntity.heroPowers) {
            if (heroPower.cardId === "TB_BaconShop_HP_069" && playerBoard.length > 0) {
                handleIllidanForPlayer(playerBoard, playerEntity, opponentBoard, opponentEntity, gameState, currentAttacker);
                currentAttacker = friendly ? 1 : 0;
                return { attacker: currentAttacker, force: true };
            }
        }
    }
    return { attacker: currentAttacker, force: false };
};
const handleIllidanForPlayer = (playerBoard, playerEntity, opponentBoard, opponentEntity, gameState, currentAttacker) => {
    if (playerEntity.startOfCombatDone) {
        return;
    }
    const minionsAtStart = playerBoard.length;
    const firstAttacker = playerBoard[0];
    const secondAttacker = minionsAtStart > 1 ? playerBoard[playerBoard.length - 1] : null;
    (0, stats_1.modifyStats)(firstAttacker, playerEntity, 2, 1, playerBoard, playerEntity, gameState);
    if (!!secondAttacker && !secondAttacker.definitelyDead && secondAttacker.health > 0) {
        (0, stats_1.modifyStats)(secondAttacker, playerEntity, 2, 1, playerBoard, playerEntity, gameState);
    }
    firstAttacker.attackImmediately = true;
    (0, attack_1.simulateAttack)(playerBoard, playerEntity, opponentBoard, opponentEntity, gameState);
    firstAttacker.hasAttacked = 0;
    firstAttacker.attackImmediately = false;
    if (!!secondAttacker && !secondAttacker.definitelyDead && secondAttacker.health > 0) {
        secondAttacker.attackImmediately = true;
        (0, attack_1.simulateAttack)(playerBoard, playerEntity, opponentBoard, opponentEntity, gameState);
        secondAttacker.hasAttacked = 0;
        secondAttacker.attackImmediately = false;
    }
};
//# sourceMappingURL=soc-illidan-hero-power.js.map