"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeCardFromHand = exports.addCardsInHand = void 0;
const card_interface_1 = require("../cards/card.interface");
const _card_mappings_1 = require("../cards/impl/_card-mappings");
const utils_1 = require("../services/utils");
const utils_2 = require("../utils");
const blood_gems_1 = require("./blood-gems");
const quest_1 = require("./quest");
const stats_1 = require("./stats");
const addCardsInHand = (playerEntity, board, cardsAdded, gameState) => {
    var _a, _b;
    const previousCardsInHand = (_b = (_a = playerEntity.hand) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
    const sages = board.filter((e) => e.cardId === "BG20_HERO_103_Buddy");
    const sagesGolden = board.filter((e) => e.cardId === "BG20_HERO_103_Buddy_G");
    const multiplier = sages.length + 2 * sagesGolden.length;
    const cardsThatWillBeAdded = [];
    for (const cardAdded of cardsAdded) {
        const cardToAdd = (cardAdded === null || cardAdded === void 0 ? void 0 : cardAdded.cardId)
            ? (0, utils_2.buildSingleBoardEntity)(cardAdded, playerEntity, board, gameState.allCards, playerEntity.friendly, gameState.sharedState.currentEntityId++, false, gameState.cardsData, gameState.sharedState, cardAdded)
            : (0, utils_2.buildSingleBoardEntity)(cardAdded, playerEntity, board, gameState.allCards, playerEntity.friendly, gameState.sharedState.currentEntityId++, false, gameState.cardsData, gameState.sharedState, null);
        cardsThatWillBeAdded.push(cardToAdd);
        if ((0, blood_gems_1.isBloodGem)(cardToAdd.cardId)) {
            for (let i = 0; i < multiplier; i++) {
                cardsThatWillBeAdded.push({ ...cardToAdd });
            }
        }
    }
    for (let i = 0; i < cardsThatWillBeAdded.length; i++) {
        if (playerEntity.hand.length >= 10) {
            break;
        }
        playerEntity.hand.push(cardsThatWillBeAdded[i]);
        onCardAddedToHand(playerEntity, cardsThatWillBeAdded[i], board, gameState);
    }
    return cardsThatWillBeAdded;
};
exports.addCardsInHand = addCardsInHand;
const onCardAddedToHand = (playerEntity, card, board, gameState) => {
    onCardAddedToHandMinion(playerEntity, card, board, gameState);
    onCardAddedToHandQuest(playerEntity, card, board, gameState);
};
const onCardAddedToHandQuest = (playerEntity, card, board, gameState) => {
    var _a;
    const quests = (_a = playerEntity.questEntities) !== null && _a !== void 0 ? _a : [];
    for (const quest of quests) {
        switch (quest.CardId) {
            case "BG24_Quest_314":
                (0, quest_1.onQuestProgressUpdated)(playerEntity, quest, board, gameState);
                break;
        }
    }
};
const onCardAddedToHandMinion = (playerEntity, card, board, gameState) => {
    var _a;
    for (const entity of board) {
        const onCardAddedToHandImpl = _card_mappings_1.cardMappings[entity.cardId];
        if ((0, card_interface_1.hasOnCardAddedToHand)(onCardAddedToHandImpl)) {
            onCardAddedToHandImpl.onCardAddedToHand(entity, {
                addedCard: card,
                board: board,
                hero: playerEntity,
                gameState: gameState,
            });
        }
    }
    for (const trinket of (_a = playerEntity.trinkets) !== null && _a !== void 0 ? _a : []) {
        const onCardAddedToHandImpl = _card_mappings_1.cardMappings[trinket.cardId];
        if ((0, card_interface_1.hasOnCardAddedToHand)(onCardAddedToHandImpl)) {
            onCardAddedToHandImpl.onCardAddedToHand(trinket, {
                addedCard: card,
                board: board,
                hero: playerEntity,
                gameState: gameState,
            });
        }
    }
    const thornCaptains = board.filter((e) => e.cardId === "BG25_045" || e.cardId === "BG25_045_G");
    thornCaptains.forEach((captain) => {
        (0, stats_1.modifyStats)(captain, captain, 0, captain.cardId === "BG25_045_G" ? 2 : 1, board, playerEntity, gameState);
    });
};
const removeCardFromHand = (playerEntity, card) => {
    var _a;
    let cardToRemove;
    if (card === null || card === void 0 ? void 0 : card.entityId) {
        cardToRemove = playerEntity.hand.find((c) => (c === null || c === void 0 ? void 0 : c.entityId) !== card.entityId);
    }
    else if (card === null || card === void 0 ? void 0 : card.cardId) {
        cardToRemove = playerEntity.hand.find((c) => (c === null || c === void 0 ? void 0 : c.cardId) === card.cardId);
    }
    else {
        cardToRemove =
            (_a = (0, utils_1.pickRandom)(playerEntity.hand.filter((c) => !(c === null || c === void 0 ? void 0 : c.entityId) && !(c === null || c === void 0 ? void 0 : c.cardId)))) !== null && _a !== void 0 ? _a : (0, utils_1.pickRandom)(playerEntity.hand);
    }
    const index = playerEntity.hand.indexOf(cardToRemove);
    if (index !== -1) {
        playerEntity.hand.splice(index, 1);
    }
};
exports.removeCardFromHand = removeCardFromHand;
//# sourceMappingURL=cards-in-hand.js.map