"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DiremuckForager = exports.handleDiremuckPendingSummons = void 0;
const reference_data_1 = require("@firestone-hs/reference-data");
const hand_minion_priority_1 = require("../../../services/hand-minion-priority");
const deathrattle_spawns_1 = require("../../../simulation/deathrattle-spawns");
const spawns_1 = require("../../../simulation/spawns");
const utils_1 = require("../../../utils");
const handMinionPredicate = (allCards) => (e) => !!e.maxHealth && (0, hand_minion_priority_1.isHandMinionCandidate)(e, (id) => { var _a; return (_a = allCards.getCard(id).type) === null || _a === void 0 ? void 0 : _a.toUpperCase(); });
const summonHandMinionFromDiremuck = (forager, handTarget, playerBoard, playerEntity, opponentBoard, opponentEntity, gameState) => {
    var _a;
    handTarget.locked = true;
    const friendly = (_a = forager === null || forager === void 0 ? void 0 : forager.friendly) !== null && _a !== void 0 ? _a : playerEntity.friendly;
    const newMinions = (0, deathrattle_spawns_1.spawnEntities)(handTarget.cardId, 1, playerBoard, playerEntity, opponentBoard, opponentEntity, gameState, friendly, false, false, true, { ...handTarget });
    for (const s of newMinions) {
        s.onCanceledSummon = () => (handTarget.locked = false);
    }
    const spawnSource = forager !== null && forager !== void 0 ? forager : playerEntity;
    const indexFromRight = forager != null && playerBoard.includes(forager) ? playerBoard.length - (playerBoard.indexOf(forager) + 1) : 0;
    (0, spawns_1.performEntitySpawns)(newMinions, playerBoard, playerEntity, spawnSource, indexFromRight, opponentBoard, opponentEntity, gameState);
};
const handleDiremuckPendingSummons = (playerBoard, playerEntity, opponentBoard, opponentEntity, gameState) => {
    var _a, _b;
    const raw = (_a = playerEntity.globalInfo) === null || _a === void 0 ? void 0 : _a.DiremuckPendingSummons;
    if (!(raw === null || raw === void 0 ? void 0 : raw.length)) {
        return;
    }
    const allCards = gameState.allCards;
    const predicate = handMinionPredicate(allCards);
    const nextQueue = [];
    for (const entry of raw) {
        let remaining = entry.remaining;
        const forager = (_b = playerBoard.find((e) => e.entityId === entry.foragerEntityId)) !== null && _b !== void 0 ? _b : null;
        while (playerBoard.length < 7 && remaining > 0) {
            const candidates = playerEntity.hand.filter((e) => (0, utils_1.hasCorrectTribe)(e, playerEntity, reference_data_1.Race.MURLOC, gameState.anomalies, gameState.allCards));
            const targets = (0, hand_minion_priority_1.getHighestAttackHandMinions)(candidates, 1, predicate);
            const handTarget = targets[0];
            if (!(handTarget === null || handTarget === void 0 ? void 0 : handTarget.cardId) || handTarget.locked) {
                break;
            }
            summonHandMinionFromDiremuck(forager, handTarget, playerBoard, playerEntity, opponentBoard, opponentEntity, gameState);
            remaining--;
        }
        if (remaining > 0) {
            nextQueue.push({ foragerEntityId: entry.foragerEntityId, remaining });
        }
    }
    playerEntity.globalInfo.DiremuckPendingSummons = nextQueue.length ? nextQueue : undefined;
};
exports.handleDiremuckPendingSummons = handleDiremuckPendingSummons;
exports.DiremuckForager = {
    cardIds: ["BG27_556", "BG27_556_G"],
    startOfCombatTiming: 'start-of-combat',
    startOfCombat: (minion, input) => {
        var _a;
        const allCards = input.gameState.allCards;
        const totalWanted = minion.cardId === "BG27_556_G" ? 2 : 1;
        const predicate = handMinionPredicate(allCards);
        let remaining = totalWanted;
        while (remaining > 0 && input.playerBoard.length < 7) {
            const targets = (0, hand_minion_priority_1.getHighestAttackHandMinions)(input.playerEntity.hand, 1, predicate);
            const target = targets[0];
            if (!(target === null || target === void 0 ? void 0 : target.cardId) || target.locked) {
                break;
            }
            summonHandMinionFromDiremuck(minion, target, input.playerBoard, input.playerEntity, input.opponentBoard, input.opponentEntity, input.gameState);
            remaining--;
        }
        if (remaining > 0) {
            input.playerEntity.globalInfo.DiremuckPendingSummons = [
                ...((_a = input.playerEntity.globalInfo.DiremuckPendingSummons) !== null && _a !== void 0 ? _a : []),
                { foragerEntityId: minion.entityId, remaining },
            ];
        }
        return true;
    },
};
//# sourceMappingURL=diremuck-forager.js.map