"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.onEntityDamaged = 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 divine_shield_1 = require("../keywords/divine-shield");
const utils_1 = require("../services/utils");
const utils_2 = require("../utils");
const deathrattle_spawns_1 = require("./deathrattle-spawns");
const frenzy_1 = require("./frenzy");
const spawns_1 = require("./spawns");
const stats_1 = require("./stats");
const onEntityDamaged = (damagedEntity, board, hero, otherBoard, otherHero, damageSource, damage, gameState) => {
    var _a, _b, _c, _d;
    const spawnedEntities = [];
    const friendlyBoard = ((_a = board === null || board === void 0 ? void 0 : board[0]) === null || _a === void 0 ? void 0 : _a.friendly) === damagedEntity.friendly
        ? board
        : ((_b = otherBoard === null || otherBoard === void 0 ? void 0 : otherBoard[0]) === null || _b === void 0 ? void 0 : _b.friendly) === damagedEntity.friendly
            ? otherBoard
            : [];
    const enemyBoard = ((_c = board === null || board === void 0 ? void 0 : board[0]) === null || _c === void 0 ? void 0 : _c.friendly) === damagedEntity.friendly
        ? otherBoard
        : ((_d = board === null || board === void 0 ? void 0 : board[0]) === null || _d === void 0 ? void 0 : _d.friendly) === damagedEntity.friendly
            ? board
            : [];
    const friendlyHero = friendlyBoard === board ? hero : otherHero;
    const enemyHero = friendlyBoard === board ? otherHero : hero;
    if (damagedEntity.frenzyChargesLeft > 0 && damagedEntity.health > 0 && !damagedEntity.definitelyDead) {
        (0, frenzy_1.applyFrenzy)(damagedEntity, friendlyBoard, friendlyHero, gameState);
        damagedEntity.frenzyChargesLeft--;
    }
    const wheneverDamagedImpl = _card_mappings_1.cardMappings[damagedEntity.cardId];
    if ((0, card_interface_1.hasOnDamaged)(wheneverDamagedImpl)) {
        wheneverDamagedImpl.onDamaged(damagedEntity, {
            damagedEntity: damagedEntity,
            damageDealer: damageSource,
            damage: damage,
            board: board,
            hero: hero,
            gameState,
        });
    }
    switch (damagedEntity.cardId) {
        case "BG29_844":
        case "BG29_844_G":
            if (damagedEntity.abiityChargesLeft > 0) {
                const wingedChimeraModifier = damagedEntity.cardId === "BG29_844_G" ? 2 : 1;
                (0, utils_2.grantStatsToMinionsOfEachType)(damagedEntity, board, hero, wingedChimeraModifier * 1, wingedChimeraModifier * 1, gameState);
                damagedEntity.abiityChargesLeft--;
            }
            break;
        case "BG29_878":
        case "BG29_878_G":
            (0, divine_shield_1.updateDivineShield)(damagedEntity, board, hero, otherHero, true, gameState);
            gameState.spectator.registerPowerTarget(damagedEntity, damagedEntity, board, hero, otherHero);
            break;
        case "BG29_800":
        case "BG29_800_G":
            const trustyPupStats = damagedEntity.cardId === "BG29_800_G" ? 2 : 1;
            (0, stats_1.modifyStats)(damagedEntity, damagedEntity, trustyPupStats, 0, board, hero, gameState);
            break;
        case "BG29_801":
        case "BG29_801_G":
            spawnedEntities.push(...(0, deathrattle_spawns_1.spawnEntities)(damagedEntity.cardId === "BG29_801_G"
                ? "BG29_801_Gt"
                : "BG29_801t", 1, board, hero, otherBoard, otherHero, gameState, damagedEntity.friendly, false));
            break;
    }
    handleOtherEntityEffects(damagedEntity, board, hero, otherBoard, otherHero, spawnedEntities, gameState);
    const finalSpawns = (0, spawns_1.performEntitySpawns)(spawnedEntities, board, hero, damagedEntity, board.length - (board.indexOf(damagedEntity) + 1), otherBoard, otherHero, gameState);
    const entityRightToSpawns = board[board.indexOf(damagedEntity) + 1];
    finalSpawns.forEach((e) => {
        var _a;
        e.hasAttacked = damagedEntity.hasAttacked > 1 ? 1 : (_a = entityRightToSpawns === null || entityRightToSpawns === void 0 ? void 0 : entityRightToSpawns.hasAttacked) !== null && _a !== void 0 ? _a : 0;
    });
    if (damage > 0) {
        for (const entity of board) {
            const afterDealDamageImpl = _card_mappings_1.cardMappings[entity.cardId];
            if ((0, card_interface_1.hasAfterDealDamage)(afterDealDamageImpl)) {
                afterDealDamageImpl.afterDealDamage(entity, {
                    damagedEntity: damagedEntity,
                    damageDealer: damageSource,
                    damage: damage,
                    board: board,
                    hero: hero,
                    gameState,
                });
            }
        }
        for (const entity of otherBoard) {
            const afterDealDamageImpl = _card_mappings_1.cardMappings[entity.cardId];
            if ((0, card_interface_1.hasAfterDealDamage)(afterDealDamageImpl)) {
                afterDealDamageImpl.afterDealDamage(entity, {
                    damagedEntity: damagedEntity,
                    damageDealer: damageSource,
                    damage: damage,
                    board: otherBoard,
                    hero: otherHero,
                    gameState,
                });
            }
        }
    }
    return finalSpawns;
};
exports.onEntityDamaged = onEntityDamaged;
const handleOtherEntityEffects = (entity, board, hero, otherBoard, otherHero, spawnedEntities, gameState) => {
    if ((0, utils_2.hasCorrectTribe)(entity, hero, reference_data_1.Race.BEAST, gameState.anomalies, gameState.allCards)) {
        board
            .filter((e) => e.cardId === "BG29_806" ||
            e.cardId === "BG29_806_G")
            .forEach((e) => {
            const stats = e.cardId === "BG29_806_G" ? 2 : 1;
            const target = (0, utils_1.pickRandom)(board.filter((e) => e.entityId !== entity.entityId));
            if (!!target) {
                (0, stats_1.modifyStats)(target, e, 2 * stats, 1 * stats, board, hero, gameState);
            }
        });
        board
            .filter((e) => e.cardId === "BG29_807" || e.cardId === "BG29_807_G")
            .filter((e) => e.entityId !== entity.entityId)
            .forEach((e) => {
            const stats = e.cardId === "BG29_807_G" ? 2 : 1;
            (0, stats_1.modifyStats)(e, e, 0, 2 * stats, board, hero, gameState);
        });
    }
    hero.trinkets
        .filter((t) => t.cardId === "BG30_MagicItem_427" ||
        t.cardId === "BG30_MagicItem_427t")
        .forEach((carving) => {
        const target = (0, utils_1.pickRandomAlive)(board);
        if (!!target) {
            const buff = carving.cardId === "BG30_MagicItem_427t" ? 4 : 2;
            (0, stats_1.modifyStats)(target, carving, buff, 0, board, hero, gameState);
        }
    });
};
//# sourceMappingURL=damage-effects.js.map