"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleSharptoothSnapperForPlayer = exports.handleSummonsWhenSpace = void 0;
const reference_data_1 = require("@firestone-hs/reference-data");
const taunt_1 = require("../keywords/taunt");
const utils_1 = require("../services/utils");
const utils_2 = require("../utils");
const add_minion_to_board_1 = require("./add-minion-to-board");
const deathrattle_spawns_1 = require("./deathrattle-spawns");
const diremuck_forager_1 = require("../cards/impl/minion/diremuck-forager");
const spawns_1 = require("./spawns");
const handleSummonsWhenSpace = (playerBoard, playerEntity, opponentBoard, opponentEntity, gameState) => {
    let shouldRecomputeCurrentAttacker = false;
    if (Math.random() < 0.5) {
        shouldRecomputeCurrentAttacker =
            handleSummonsWhenSpaceForPlayer(playerEntity, playerBoard, playerEntity, opponentBoard, opponentEntity, gameState) || shouldRecomputeCurrentAttacker;
        shouldRecomputeCurrentAttacker =
            handleSummonsWhenSpaceForPlayer(opponentEntity, opponentBoard, opponentEntity, playerBoard, playerEntity, gameState) || shouldRecomputeCurrentAttacker;
    }
    else {
        shouldRecomputeCurrentAttacker = handleSummonsWhenSpaceForPlayer(opponentEntity, opponentBoard, opponentEntity, playerBoard, playerEntity, gameState);
        shouldRecomputeCurrentAttacker =
            handleSummonsWhenSpaceForPlayer(playerEntity, playerBoard, playerEntity, opponentBoard, opponentEntity, gameState) || shouldRecomputeCurrentAttacker;
    }
    return shouldRecomputeCurrentAttacker;
};
exports.handleSummonsWhenSpace = handleSummonsWhenSpace;
const handleSummonsWhenSpaceForPlayer = (targetEntity, playerBoard, playerEntity, opponentBoard, opponentEntity, gameState) => {
    var _a, _b, _c;
    let shouldRecomputeCurrentAttacker = false;
    if ((_a = targetEntity.questRewards) === null || _a === void 0 ? void 0 : _a.includes("BG28_Reward_518")) {
        shouldRecomputeCurrentAttacker =
            handleStableAmalgamationForPlayer(playerBoard, playerEntity, opponentBoard, opponentEntity, gameState) ||
                shouldRecomputeCurrentAttacker;
    }
    if ((_b = targetEntity.secrets) === null || _b === void 0 ? void 0 : _b.some((s) => s.cardId === "BG28_603")) {
        shouldRecomputeCurrentAttacker =
            handleBoonOfBeetlesForPlayer(playerBoard, playerEntity, opponentBoard, opponentEntity, gameState) ||
                shouldRecomputeCurrentAttacker;
    }
    for (const heroPower of targetEntity.heroPowers) {
        if (heroPower.cardId === "BG23_HERO_201p" && heroPower.ready) {
            shouldRecomputeCurrentAttacker =
                handleOzumatForPlayer(heroPower, playerBoard, playerEntity, opponentBoard, opponentEntity, targetEntity.friendly, gameState) || shouldRecomputeCurrentAttacker;
        }
        else if (heroPower.cardId === "BG22_HERO_002p" &&
            gameState.currentTurn >= 7 &&
            heroPower.ready &&
            !heroPower.activated) {
            shouldRecomputeCurrentAttacker =
                handleFrostwolfFervorForPlayer(heroPower, playerBoard, playerEntity, opponentBoard, opponentEntity, targetEntity.friendly, gameState) || shouldRecomputeCurrentAttacker;
        }
        else if (heroPower.cardId === "BG22_HERO_003p" &&
            gameState.currentTurn >= 7 &&
            heroPower.ready &&
            !heroPower.activated) {
            shouldRecomputeCurrentAttacker =
                handleStormpikeStrengthForPlayer(heroPower, playerBoard, playerEntity, opponentBoard, opponentEntity, targetEntity.friendly, gameState) || shouldRecomputeCurrentAttacker;
        }
        else if (heroPower.cardId === "BG22_HERO_000p_Alt" &&
            !heroPower.activated &&
            heroPower.ready) {
            shouldRecomputeCurrentAttacker =
                !!handleLockAndLoadForPlayer(heroPower, playerBoard, playerEntity, opponentBoard, opponentEntity, targetEntity.friendly, gameState) || shouldRecomputeCurrentAttacker;
        }
        else if (heroPower.cardId === "BG25_HERO_103p" &&
            ((_c = heroPower.info) === null || _c === void 0 ? void 0 : _c.cardId)) {
            shouldRecomputeCurrentAttacker =
                handleRapidReanimationForPlayer(heroPower, playerBoard, playerEntity, opponentBoard, opponentEntity, gameState) || shouldRecomputeCurrentAttacker;
        }
    }
    targetEntity.trinkets
        .filter((t) => t.cardId === "BG30_MagicItem_822" ||
        t.cardId === "BG30_MagicItem_822t2")
        .forEach((t) => {
        handleTwinSkyLanternsForPlayer(t, playerBoard, playerEntity, opponentBoard, opponentEntity, gameState);
    });
    targetEntity.trinkets
        .filter((t) => t.cardId === "BG30_MagicItem_440")
        .forEach((t) => {
        handleBoomControllerForPlayer(t, playerBoard, playerEntity, opponentBoard, opponentEntity, gameState);
    });
    playerBoard
        .filter((e) => e.cardId === "BG32_201" || e.cardId === "BG32_201_G")
        .forEach((e) => {
        (0, exports.handleSharptoothSnapperForPlayer)(e, playerBoard, playerEntity, opponentBoard, opponentEntity, gameState);
    });
    (0, diremuck_forager_1.handleDiremuckPendingSummons)(playerBoard, playerEntity, opponentBoard, opponentEntity, gameState);
    return shouldRecomputeCurrentAttacker;
};
const handleSharptoothSnapperForPlayer = (entity, playerBoard, playerEntity, opponentBoard, opponentEntity, gameState) => {
    while (playerBoard.length < 7 && entity.abiityChargesLeft > 0) {
        const candidates = (0, deathrattle_spawns_1.spawnEntities)("BG32_201t", 1, playerBoard, playerEntity, opponentBoard, opponentEntity, gameState, playerEntity.friendly, true);
        candidates.forEach((t) => (t.attackImmediately = true));
        const actualSpawns = (0, spawns_1.performEntitySpawns)(candidates, playerBoard, playerEntity, playerEntity, playerBoard.length - 1 - playerBoard.indexOf(entity), opponentBoard, opponentEntity, gameState);
        entity.abiityChargesLeft--;
    }
};
exports.handleSharptoothSnapperForPlayer = handleSharptoothSnapperForPlayer;
const handleBoomControllerForPlayer = (trinket, playerBoard, playerEntity, opponentBoard, opponentEntity, gameState) => {
    if (playerBoard.length < 7 && trinket.scriptDataNum1 > 0) {
        const candidate = gameState.sharedState.deaths
            .filter((d) => d.friendly === playerEntity.friendly)
            .filter((d) => (0, utils_2.hasCorrectTribe)(d, playerEntity, reference_data_1.Race.MECH, gameState.anomalies, gameState.allCards))[0];
        if (!!candidate) {
            const spawn = (0, utils_2.copyEntity)(candidate);
            (0, add_minion_to_board_1.removeAurasFromSelf)(spawn, playerBoard, playerEntity, gameState);
            const initialIndexFromLeft = candidate.indexFromLeftAtTimeOfDeath;
            const indexFromRight = initialIndexFromLeft == null
                ? 0
                : Math.min(playerBoard.length, Math.max(0, playerBoard.length - initialIndexFromLeft));
            const target = (0, deathrattle_spawns_1.spawnEntities)(spawn.cardId, 1, playerBoard, playerEntity, opponentBoard, opponentEntity, gameState, playerEntity.friendly, true, false, false, spawn);
            const actualSpawns = (0, spawns_1.performEntitySpawns)(target, playerBoard, playerEntity, playerEntity, indexFromRight, opponentBoard, opponentEntity, gameState);
            actualSpawns.forEach((t) => gameState.spectator.registerPowerTarget(playerEntity, t, playerBoard, playerEntity, opponentEntity));
            trinket.scriptDataNum1 = 0;
        }
    }
};
const handleTwinSkyLanternsForPlayer = (trinket, playerBoard, playerEntity, opponentBoard, opponentEntity, gameState) => {
    var _a;
    const spawnNumber = (_a = trinket.scriptDataNum1) !== null && _a !== void 0 ? _a : 0;
    const canTrigger = spawnNumber >= 1;
    if (playerBoard.length <= 7 - spawnNumber && trinket.rememberedMinion && canTrigger) {
        trinket.scriptDataNum1 = 0;
        const spawn = (0, utils_2.copyEntity)(trinket.rememberedMinion);
        (0, add_minion_to_board_1.removeAurasFromSelf)(spawn, playerBoard, playerEntity, gameState);
        const target = (0, deathrattle_spawns_1.spawnEntities)(spawn.cardId, spawnNumber, playerBoard, playerEntity, opponentBoard, opponentEntity, gameState, playerEntity.friendly, true, false, false, spawn);
        (0, spawns_1.performEntitySpawns)(target, playerBoard, playerEntity, playerEntity, 0, opponentBoard, opponentEntity, gameState);
        target.forEach((t) => gameState.spectator.registerPowerTarget(playerEntity, t, playerBoard, playerEntity, opponentEntity));
    }
};
const handleOzumatForPlayer = (heroPower, playerBoard, playerEntity, opponentBoard, opponentEntity, friendly, gameState) => {
    if (playerBoard.length < 7 && heroPower.activated === false) {
        const tentacularSize = +heroPower.info;
        const tentacular = (0, deathrattle_spawns_1.spawnEntities)("BG23_HERO_201pt", 1, playerBoard, playerEntity, opponentBoard, opponentEntity, gameState, friendly, true, false, false);
        tentacular[0].attack = tentacularSize;
        tentacular[0].health = tentacularSize;
        tentacular[0].maxHealth = tentacularSize;
        tentacular[0].maxAttack = tentacularSize;
        const indexFromRight = 0;
        (0, spawns_1.performEntitySpawns)(tentacular, playerBoard, playerEntity, playerEntity, indexFromRight, opponentBoard, opponentEntity, gameState);
        gameState.spectator.registerPowerTarget(playerEntity, tentacular[0], playerBoard, playerEntity, opponentEntity);
        heroPower.activated = true;
    }
    return true;
};
const handleFrostwolfFervorForPlayer = (heroPower, playerBoard, playerEntity, opponentBoard, opponentEntity, friendly, gameState) => {
    if (playerBoard.length < 7) {
        heroPower.activated = true;
        const target = (0, utils_1.pickRandomHighestAttack)(playerBoard);
        if (!!target) {
            const copy = (0, utils_2.copyEntity)(target);
            const indexFromRight = playerBoard.length - (playerBoard.indexOf(target) + 1);
            const newMinions = (0, deathrattle_spawns_1.spawnEntities)(copy.cardId, 1, playerBoard, playerEntity, opponentBoard, opponentEntity, gameState, playerEntity.friendly, false, false, false, copy);
            const spawns = (0, spawns_1.performEntitySpawns)(newMinions, playerBoard, playerEntity, playerEntity, indexFromRight, opponentBoard, opponentEntity, gameState);
            gameState.spectator.registerPowerTarget(playerEntity, spawns[0], playerBoard, playerEntity, opponentEntity);
            return true;
        }
    }
    return false;
};
const handleLockAndLoadForPlayer = (heroPower, playerBoard, playerEntity, opponentBoard, opponentEntity, friendly, gameState) => {
    if (playerBoard.length < 7) {
        const summoned = heroPower.info;
        heroPower.activated = true;
        if (!!(summoned === null || summoned === void 0 ? void 0 : summoned.cardId)) {
            const copy = (0, utils_2.copyEntity)(summoned);
            copy.attackImmediately = true;
            const indexFromRight = 0;
            const newMinions = (0, deathrattle_spawns_1.spawnEntities)(copy.cardId, 1, playerBoard, playerEntity, opponentBoard, opponentEntity, gameState, playerEntity.friendly, false, false, false, copy);
            const spawns = (0, spawns_1.performEntitySpawns)(newMinions, playerBoard, playerEntity, playerEntity, indexFromRight, opponentBoard, opponentEntity, gameState);
            gameState.spectator.registerPowerTarget(playerEntity, spawns[0], playerBoard, playerEntity, opponentEntity);
            if (spawns[0]) {
                spawns[0].hasAttacked = undefined;
            }
            return true;
        }
    }
};
const handleStormpikeStrengthForPlayer = (heroPower, playerBoard, playerEntity, opponentBoard, opponentEntity, friendly, gameState) => {
    if (playerBoard.length < 7) {
        heroPower.activated = true;
        const target = (0, utils_1.pickRandomHighestHealth)(playerBoard);
        if (!!target) {
            const copy = (0, utils_2.copyEntity)(target);
            const indexFromRight = playerBoard.length - (playerBoard.indexOf(target) + 1);
            const newMinions = (0, deathrattle_spawns_1.spawnEntities)(copy.cardId, 1, playerBoard, playerEntity, opponentBoard, opponentEntity, gameState, playerEntity.friendly, false, false, false, copy);
            const spawns = (0, spawns_1.performEntitySpawns)(newMinions, playerBoard, playerEntity, playerEntity, indexFromRight, opponentBoard, opponentEntity, gameState);
            gameState.spectator.registerPowerTarget(playerEntity, spawns[0], playerBoard, playerEntity, opponentEntity);
            return true;
        }
    }
    return false;
};
const handleBoonOfBeetlesForPlayer = (playerBoard, playerEntity, opponentBoard, opponentEntity, gameState) => {
    const secretEntity = playerEntity.secrets.find((entity) => entity.cardId === "BG28_603");
    if (secretEntity && secretEntity.triggersLeft > 0) {
        while (secretEntity.triggersLeft > 0) {
            const hasSummoned = handleSummon(playerBoard, playerEntity, opponentBoard, opponentEntity, gameState, "BG28_603t", 0, true);
            if (hasSummoned) {
                hasSummoned.forEach((entity) => {
                    (0, taunt_1.updateTaunt)(entity, true, playerBoard, playerEntity, opponentEntity, gameState);
                });
                secretEntity.triggersLeft--;
            }
            else {
                break;
            }
        }
    }
    return false;
};
const handleStableAmalgamationForPlayer = (playerBoard, playerEntity, opponentBoard, opponentEntity, gameState) => {
    const rewardEntity = playerEntity.questRewardEntities.find((entity) => entity.cardId === "BG28_Reward_518");
    if (rewardEntity && rewardEntity.scriptDataNum1 > 0) {
        while (rewardEntity.scriptDataNum1 > 0) {
            const hasSummoned = handleSummon(playerBoard, playerEntity, opponentBoard, opponentEntity, gameState, "BG28_Reward_518t", 0, true);
            if (hasSummoned) {
                rewardEntity.scriptDataNum1--;
            }
            else {
                break;
            }
        }
    }
    return false;
};
const handleRapidReanimationForPlayer = (heroPower, playerBoard, playerEntity, opponentBoard, opponentEntity, gameState) => {
    const indexFromRight = heroPower.info2;
    const rapidReanimationMinion = heroPower.info;
    const hasSummoned = handleSummon(playerBoard, playerEntity, opponentBoard, opponentEntity, gameState, rapidReanimationMinion.cardId, indexFromRight, true, rapidReanimationMinion);
    if (hasSummoned) {
        heroPower.info = null;
    }
    return false;
};
const handleSummon = (playerBoard, playerEntity, opponentBoard, opponentEntity, gameState, cardId, indexFromRight, applyAuras, minion = null) => {
    if (playerBoard.length >= 7) {
        return null;
    }
    const newMinion = (0, utils_2.buildSingleBoardEntity)(cardId, playerEntity, playerBoard, gameState.allCards, playerEntity.friendly, gameState.sharedState.currentEntityId++, false, gameState.cardsData, gameState.sharedState, minion, null);
    const spawned = (0, spawns_1.performEntitySpawns)([newMinion], playerBoard, playerEntity, playerEntity, indexFromRight, opponentBoard, opponentEntity, gameState, applyAuras);
    gameState.spectator.registerPowerTarget(playerEntity, newMinion, playerBoard, null, null);
    return spawned;
};
//# sourceMappingURL=summon-when-space.js.map