"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMagnetizations = exports.magnetizeToTarget = void 0;
const reference_data_1 = require("@firestone-hs/reference-data");
const card_interface_1 = require("../cards/card.interface");
const _card_mappings_1 = require("../cards/impl/_card-mappings");
const utils_1 = require("../utils");
const stats_1 = require("./stats");
const magnetizeToTarget = (inputTargets, source, cardsToMagnetize, board, hero, otherBoard, otherHero, gameState) => {
    var _a, _b, _c, _d, _e, _f, _g;
    const electromagneticDevices = hero.trinkets.filter((t) => t.cardId === "BG30_MagicItem_709").length;
    const greaterElecromagneticDevices = hero.trinkets.filter((t) => t.cardId === "BG30_MagicItem_709t").length;
    const magnetMult = board.some((e) => e.cardId === "BG34_177_G")
        ? 3
        : board.some((e) => e.cardId === "BG34_177")
            ? 2
            : 1;
    for (let i = 0; i < magnetMult; i++) {
        const actualTargets = typeof inputTargets === 'function' ? inputTargets() : inputTargets;
        for (const target of actualTargets) {
            for (const cardToMagnetize of cardsToMagnetize) {
                const modularCard = typeof cardToMagnetize === 'string'
                    ? { ...gameState.allCards.getCard(cardToMagnetize) }
                    : cardToMagnetize;
                const modularBoardEntity = (0, utils_1.buildSingleBoardEntity)(modularCard.id, hero, board, gameState.allCards, hero.friendly, gameState.sharedState.currentEntityId++, false, gameState.cardsData, gameState.sharedState, null, null, {
                    referenceCardToUse: modularCard,
                });
                const onBeforeMagnetizeSelfImpl = _card_mappings_1.cardMappings[modularBoardEntity.cardId];
                if ((0, card_interface_1.hasOnBeforeMagnetizeSelf)(onBeforeMagnetizeSelfImpl)) {
                    onBeforeMagnetizeSelfImpl.onBeforeMagnetizeSelf(modularBoardEntity, {
                        board: board,
                        hero: hero,
                        magnetizeTarget: target,
                        gameState: gameState,
                    });
                }
                for (const boardEntity of board) {
                    const onBeforeMagnetizeImpl = _card_mappings_1.cardMappings[boardEntity.cardId];
                    if ((0, card_interface_1.hasOnBeforeMagnetize)(onBeforeMagnetizeImpl)) {
                        onBeforeMagnetizeImpl.onBeforeMagnetize(boardEntity, {
                            board: board,
                            hero: hero,
                            magnetizedCard: modularCard,
                            magnetizeTarget: target,
                            gameState: gameState,
                        });
                    }
                }
                (0, stats_1.modifyStats)(target, source, modularBoardEntity.attack + 2 * electromagneticDevices + 4 * greaterElecromagneticDevices, modularBoardEntity.health + 2 * electromagneticDevices + 4 * greaterElecromagneticDevices, board, hero, gameState);
                target.taunt = target.taunt || ((_a = modularCard.mechanics) === null || _a === void 0 ? void 0 : _a.includes(reference_data_1.GameTag[reference_data_1.GameTag.TAUNT]));
                target.divineShield =
                    target.divineShield || ((_b = modularCard.mechanics) === null || _b === void 0 ? void 0 : _b.includes(reference_data_1.GameTag[reference_data_1.GameTag.DIVINE_SHIELD]));
                target.poisonous = target.poisonous || ((_c = modularCard.mechanics) === null || _c === void 0 ? void 0 : _c.includes(reference_data_1.GameTag[reference_data_1.GameTag.POISONOUS]));
                target.venomous = target.venomous || ((_d = modularCard.mechanics) === null || _d === void 0 ? void 0 : _d.includes(reference_data_1.GameTag[reference_data_1.GameTag.VENOMOUS]));
                target.windfury = target.windfury || ((_e = modularCard.mechanics) === null || _e === void 0 ? void 0 : _e.includes(reference_data_1.GameTag[reference_data_1.GameTag.WINDFURY]));
                target.reborn = target.reborn || ((_f = modularCard.mechanics) === null || _f === void 0 ? void 0 : _f.includes(reference_data_1.GameTag[reference_data_1.GameTag.REBORN]));
                target.stealth = target.stealth || ((_g = modularCard.mechanics) === null || _g === void 0 ? void 0 : _g.includes(reference_data_1.GameTag[reference_data_1.GameTag.STEALTH]));
                const magneticEnchantment = modularCard.enchantmentDbfId;
                if (magneticEnchantment) {
                    const enchantment = gameState.allCards.getCard(magneticEnchantment).id;
                    target.enchantments.push({
                        cardId: enchantment,
                        originEntityId: modularCard.dbfId,
                        timing: gameState.sharedState.currentEntityId++,
                    });
                }
                if (["BG31_176", "BG31_176_G"].includes(cardToMagnetize)) {
                    const mult = cardToMagnetize === "BG31_176" ? 1 : 2;
                    target.attack += 2 * hero.globalInfo.MagnetizedThisGame * mult;
                    target.health += 2 * hero.globalInfo.MagnetizedThisGame * mult;
                }
                hero.globalInfo.MagnetizedThisGame = hero.globalInfo.MagnetizedThisGame + 1;
                for (const entity of board) {
                    const drBoomBases = (entity.cardId === "BG31_176" ? 1 : 0) +
                        entity.enchantments.filter((e) => e.cardId === "BG31_176e").length;
                    const drBoomGoldens = (entity.cardId === "BG31_176_G" ? 1 : 0) +
                        entity.enchantments.filter((e) => e.cardId === "BG31_176_Ge").length;
                    entity.attack += drBoomBases * 2 + drBoomGoldens * 4;
                    entity.health += drBoomBases * 2 + drBoomGoldens * 4;
                }
                const onAfterMagnetizeImpl = _card_mappings_1.cardMappings[modularCard.id];
                if ((0, card_interface_1.hasOnAfterMagnetize)(onAfterMagnetizeImpl)) {
                    onAfterMagnetizeImpl.onAfterMagnetize(target, {
                        board: board,
                        hero: hero,
                        otherHero: otherHero,
                        otherBoard: otherBoard,
                        magnetizedCard: modularCard,
                        magnetizeTarget: target,
                        gameState: gameState,
                    });
                }
            }
        }
    }
};
exports.magnetizeToTarget = magnetizeToTarget;
const getMagnetizations = (entity, gameState) => {
    return entity.enchantments.filter((e) => { var _a; return (_a = gameState.allCards.getCard(e.cardId).mechanics) === null || _a === void 0 ? void 0 : _a.includes(reference_data_1.GameTag[reference_data_1.GameTag.MODULAR]); }).length;
};
exports.getMagnetizations = getMagnetizations;
//# sourceMappingURL=magnetize.js.map