"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.grantDivineShieldToLeftmostMinions = exports.grantRandomDivineShield = exports.updateDivineShield = 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("../services/utils");
const stats_1 = require("../simulation/stats");
const utils_2 = require("../utils");
const updateDivineShield = (entity, board, hero, otherHero, newValue, gameState) => {
    var _a;
    entity.hadDivineShield = newValue || entity.divineShield || entity.hadDivineShield;
    if (entity.strongDivineShield && newValue === false) {
        entity.strongDivineShield = false;
    }
    else {
        entity.divineShield = newValue;
    }
    if (entity.divineShield && newValue) {
        const boardForDrake = board;
        const statsBonus = 6 * boardForDrake.filter((e) => e.cardId === "BG25_043").length +
            12 * boardForDrake.filter((e) => e.cardId === "BG25_043_G").length;
        entity.attack += statsBonus;
    }
    else if (!entity.divineShield) {
        const boardForDrake = board;
        const statsBonus = 6 * boardForDrake.filter((e) => e.cardId === "BG25_043").length +
            12 * boardForDrake.filter((e) => e.cardId === "BG25_043_G").length;
        entity.attack -= statsBonus;
    }
    if (entity.hadDivineShield && !entity.divineShield) {
        const adapter = hero.trinkets
            .filter((t) => t.cardId === "BG30_MagicItem_910")
            .filter((t) => t.scriptDataNum1 > 0)[0];
        if (!!adapter && (0, utils_2.hasCorrectTribe)(entity, hero, reference_data_1.Race.MECH, gameState.anomalies, gameState.allCards)) {
            (0, exports.updateDivineShield)(entity, board, hero, otherHero, true, gameState);
            adapter.scriptDataNum1--;
        }
        for (const trinket of (_a = hero.trinkets) !== null && _a !== void 0 ? _a : []) {
            const onDivineShieldUpdatedImpl = _card_mappings_1.cardMappings[trinket.cardId];
            if ((0, card_interface_1.hasOnDivineShieldUpdated)(onDivineShieldUpdatedImpl)) {
                onDivineShieldUpdatedImpl.onDivineShieldUpdated(trinket, {
                    board: board,
                    hero: hero,
                    otherHero: otherHero,
                    gameState: gameState,
                    target: entity,
                    newValue: false,
                    previousValue: true,
                });
            }
        }
        for (const boardEntity of board) {
            const onDivineShieldUpdatedImpl = _card_mappings_1.cardMappings[boardEntity.cardId];
            if ((0, card_interface_1.hasOnDivineShieldUpdated)(onDivineShieldUpdatedImpl)) {
                onDivineShieldUpdatedImpl.onDivineShieldUpdated(boardEntity, {
                    board: board,
                    hero: hero,
                    otherHero: otherHero,
                    gameState: gameState,
                    target: entity,
                    newValue: false,
                    previousValue: true,
                });
            }
        }
        for (let i = 0; i < board.length; i++) {
            if (board[i].cardId === "ICC_858") {
                (0, stats_1.modifyStats)(board[i], board[i], 2, 0, board, hero, gameState);
                gameState.spectator.registerPowerTarget(board[i], board[i], board, hero, otherHero);
            }
        }
    }
};
exports.updateDivineShield = updateDivineShield;
const grantRandomDivineShield = (source, board, hero, otherHero, gameState) => {
    const elligibleEntities = board
        .filter((entity) => !entity.divineShield)
        .filter((entity) => entity.health > 0 && !entity.definitelyDead);
    if (elligibleEntities.length > 0) {
        const chosen = (0, utils_1.pickRandom)(elligibleEntities);
        (0, exports.updateDivineShield)(chosen, board, hero, otherHero, true, gameState);
        gameState.spectator.registerPowerTarget(source, chosen, board, null, null);
    }
};
exports.grantRandomDivineShield = grantRandomDivineShield;
const grantDivineShieldToLeftmostMinions = (source, board, hero, quantity, otherHero, gameState) => {
    for (let i = 0; i < Math.min(quantity, board.length); i++) {
        (0, exports.updateDivineShield)(board[i], board, hero, otherHero, true, gameState);
        gameState.spectator.registerPowerTarget(source, board[i], board, null, null);
    }
};
exports.grantDivineShieldToLeftmostMinions = grantDivineShieldToLeftmostMinions;
//# sourceMappingURL=divine-shield.js.map