"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isBloodGem = exports.applyBloodGemEnchantment = exports.playBloodGemsOn = void 0;
const card_interface_1 = require("../cards/card.interface");
const _card_mappings_1 = require("../cards/impl/_card-mappings");
const divine_shield_1 = require("../keywords/divine-shield");
const utils_1 = require("../services/utils");
const utils_2 = require("../utils");
const stats_1 = require("./stats");
const playBloodGemsOn = (source, target, quantity, board, hero, otherBoard, otherHero, gameState, registerTarget = true) => {
    var _a, _b, _c, _d;
    if (registerTarget) {
        gameState.spectator.registerPowerTarget(source, target, board, null, null);
    }
    const bloodGemBaseAttack = 1 +
        ((_b = (_a = hero.globalInfo) === null || _a === void 0 ? void 0 : _a.BloodGemAttackBonus) !== null && _b !== void 0 ? _b : 0) +
        hero.trinkets.filter((t) => t.cardId === "BG30_MagicItem_988").length * 3 +
        hero.trinkets.filter((t) => t.cardId === "BG30_MagicItem_988t")
            .length *
            2;
    const bloodGemBaseHealth = 1 +
        ((_d = (_c = hero.globalInfo) === null || _c === void 0 ? void 0 : _c.BloodGemHealthBonus) !== null && _d !== void 0 ? _d : 0) +
        hero.trinkets.filter((t) => t.cardId === "BG30_MagicItem_988t")
            .length *
            2;
    const crones = board.filter((e) => e.cardId === "BG32_432").length;
    const goldenCrones = board.filter((e) => e.cardId === "BG32_432_G").length;
    const cronesMult = goldenCrones > 0 ? 3 : crones > 0 ? 2 : 1;
    const bloodGemAttack = bloodGemBaseAttack * cronesMult;
    const bloodGemHealth = bloodGemBaseHealth * cronesMult;
    (0, exports.applyBloodGemEnchantment)("BG20_GEMe", target, source, quantity, bloodGemAttack, bloodGemHealth);
    (0, exports.applyBloodGemEnchantment)("BG20_GEMe2", target, source, quantity, bloodGemAttack, bloodGemHealth);
    (0, stats_1.modifyStats)(target, null, quantity * bloodGemAttack, quantity * bloodGemHealth, board, hero, gameState, false);
    for (let i = 0; i < quantity; i++) {
        switch (target.cardId) {
            case "BG20_102":
            case "BG20_102_G":
                if (!target.divineShield) {
                    (0, divine_shield_1.updateDivineShield)(target, board, hero, null, true, gameState);
                    gameState.spectator.registerPowerTarget(target, target, board, null, null);
                }
                break;
            case "BG28_583":
            case "BG28_583_G":
                const roogugTargets = board.filter((e) => e.cardId !== "BG28_583" && e.cardId !== "BG28_583_G");
                const roogugTarget = (0, utils_1.pickRandom)(roogugTargets);
                if (roogugTarget) {
                    const roogugBuff = target.cardId === "BG28_583_G" ? 2 : 1;
                    (0, exports.playBloodGemsOn)(target, roogugTarget, roogugBuff, board, hero, otherBoard, otherHero, gameState, false);
                }
                break;
            case "BG20_302":
            case "BG20_302_G":
                const aggemGemsToPlay = target.cardId === "BG20_302_G" ? 2 : 1;
                const candidates = (0, utils_2.getMinionsOfDifferentTypes)(board.filter((e) => e.cardId !== "BG20_302" &&
                    e.cardId !== "BG20_302_G"), hero, gameState);
                for (const candidate of candidates) {
                    (0, exports.playBloodGemsOn)(target, candidate, aggemGemsToPlay, board, hero, otherBoard, otherHero, gameState, false);
                }
                break;
            default:
                const playedBloodGemsOnMeImpl = _card_mappings_1.cardMappings[target.cardId];
                if ((0, card_interface_1.hasPlayedBloodGemsOnMe)(playedBloodGemsOnMeImpl)) {
                    playedBloodGemsOnMeImpl.playedBloodGemsOnMe(target, {
                        board: board,
                        hero: hero,
                        otherBoard: otherBoard,
                        otherHero: otherHero,
                        gameState: gameState,
                    });
                }
        }
        for (const boardEntity of board) {
            const playedBloodGemsOnAnyImpl = _card_mappings_1.cardMappings[boardEntity.cardId];
            if ((0, card_interface_1.hasPlayedBloodGemsOnAny)(playedBloodGemsOnAnyImpl)) {
                playedBloodGemsOnAnyImpl.playedBloodGemsOnAny(boardEntity, {
                    source: source,
                    target: target,
                    board: board,
                    hero: hero,
                    otherBoard: otherBoard,
                    otherHero: otherHero,
                    gameState: gameState,
                });
            }
        }
    }
};
exports.playBloodGemsOn = playBloodGemsOn;
const applyBloodGemEnchantment = (enchantmentCardId, target, source, quantity, bloodGemAttack, bloodGemHealth) => {
    let enchantment = (0, utils_1.findLast)(target.enchantments, (e) => e.cardId === enchantmentCardId);
    if (!enchantment) {
        enchantment = {
            cardId: enchantmentCardId,
            originEntityId: source.entityId,
            timing: 0,
            tagScriptDataNum1: 0,
            tagScriptDataNum2: 0,
        };
        target.enchantments.push(enchantment);
    }
    for (let i = 0; i < quantity; i++) {
        enchantment.tagScriptDataNum1 += bloodGemAttack;
        enchantment.tagScriptDataNum2 += bloodGemHealth;
    }
};
exports.applyBloodGemEnchantment = applyBloodGemEnchantment;
const isBloodGem = (cardId) => {
    return (cardId === "BG20_GEM" ||
        cardId === "BG20_GEM_Reborn" ||
        cardId === "BG20_GEM_Reborn_G" ||
        cardId === "BG20_GEM_Taunt" ||
        cardId === "BG20_GEM_Taunt_G" ||
        cardId === "BG20_GEM_DivineShield" ||
        cardId === "BG20_GEM_DivineShield_G");
};
exports.isBloodGem = isBloodGem;
//# sourceMappingURL=blood-gems.js.map