"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleStartOfCombat = void 0;
const pending_effects_1 = require("../pending-effects");
const stats_1 = require("../stats");
const soc_anomalies_1 = require("./soc-anomalies");
const soc_hero_power_1 = require("./soc-hero-power");
const soc_illidan_hero_power_1 = require("./soc-illidan-hero-power");
const soc_minion_1 = require("./soc-minion");
const soc_pre_combat_hero_power_1 = require("./soc-pre-combat-hero-power");
const soc_quest_reward_1 = require("./soc-quest-reward");
const soc_secret_1 = require("./soc-secret");
const soc_trinket_1 = require("./soc-trinket");
const handleStartOfCombat = (playerEntity, playerBoard, opponentEntity, opponentBoard, currentAttacker, gameState) => {
    const shouldRecomputeCurrentAttacker = true;
    if (shouldRecomputeCurrentAttacker) {
        currentAttacker =
            playerBoard.length > opponentBoard.length
                ? 0
                : opponentBoard.length > playerBoard.length
                    ? 1
                    : Math.round(Math.random());
    }
    const phases = [
        'QuestReward',
        'Anomalies',
        'Trinket',
        'PreCombatHeroPower',
        'IllidanHeroPower',
        'HeroPower',
        'Secret',
        'Minion',
    ];
    let playerBoardBefore = playerBoard.map((e) => ({ ...e }));
    let opponentBoardBefore = opponentBoard.map((e) => ({ ...e }));
    let forcedAttacker = null;
    for (const phase of phases) {
        const { attacker: newAttacker, force: newForce } = handlePhase(phase, playerEntity, playerBoard, playerBoardBefore, opponentEntity, opponentBoard, opponentBoardBefore, currentAttacker, gameState);
        currentAttacker = newAttacker;
        if (newForce) {
            forcedAttacker = newAttacker;
        }
        if (phase === 'PreCombatHeroPower') {
            playerBoardBefore = playerBoard.map((e) => ({ ...e }));
            opponentBoardBefore = opponentBoard.map((e) => ({ ...e }));
        }
    }
    playerEntity.startOfCombatDone = true;
    opponentEntity.startOfCombatDone = true;
    (0, stats_1.applyAfterStatsUpdate)(gameState);
    (0, pending_effects_1.resolvePendingEffects)(playerBoard, playerEntity, opponentBoard, opponentEntity, gameState);
    return forcedAttacker != null ? forcedAttacker : currentAttacker;
};
exports.handleStartOfCombat = handleStartOfCombat;
const handlePhase = (phase, playerEntity, playerBoard, playerBoardBefore, opponentEntity, opponentBoard, opponentBoardBefore, currentAttacker, gameState) => {
    switch (phase) {
        case 'QuestReward':
            currentAttacker = (0, soc_quest_reward_1.handleStartOfCombatQuestRewards)(playerEntity, playerBoard, opponentEntity, opponentBoard, currentAttacker, gameState);
            return { attacker: currentAttacker, force: false };
        case 'Anomalies':
            currentAttacker = (0, soc_anomalies_1.handleStartOfCombatAnomalies)(playerEntity, playerBoard, opponentEntity, opponentBoard, currentAttacker, gameState);
            return { attacker: currentAttacker, force: false };
        case 'Trinket':
            currentAttacker = (0, soc_trinket_1.handleStartOfCombatTrinkets)(playerEntity, playerBoard, opponentEntity, opponentBoard, currentAttacker, gameState);
            return { attacker: currentAttacker, force: false };
        case 'PreCombatHeroPower':
            currentAttacker = (0, soc_pre_combat_hero_power_1.handlePreCombatHeroPowers)(playerEntity, playerBoard, opponentEntity, opponentBoard, currentAttacker, gameState);
            return { attacker: currentAttacker, force: false };
        case 'IllidanHeroPower':
            const { attacker: newAttacker, force: newForce } = (0, soc_illidan_hero_power_1.handleIllidanHeroPowers)(playerEntity, playerBoard, opponentEntity, opponentBoard, currentAttacker, gameState);
            return { attacker: newAttacker, force: newForce };
        case 'HeroPower':
            currentAttacker = (0, soc_hero_power_1.handleStartOfCombatHeroPowers)(playerEntity, playerBoard, opponentEntity, opponentBoard, currentAttacker, gameState);
            return { attacker: currentAttacker, force: false };
        case 'Secret':
            currentAttacker = (0, soc_secret_1.handleStartOfCombatSecrets)(playerEntity, playerBoard, opponentEntity, opponentBoard, currentAttacker, gameState);
            return { attacker: currentAttacker, force: false };
        case 'Minion':
            currentAttacker = (0, soc_minion_1.handleStartOfCombatMinions)(playerEntity, playerBoard, opponentEntity, opponentBoard, currentAttacker, playerBoardBefore, opponentBoardBefore, gameState);
            return { attacker: currentAttacker, force: false };
    }
    return { attacker: currentAttacker, force: false };
};
//# sourceMappingURL=start-of-combat.js.map