"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: friendlyBoard,
            hero: friendlyHero,
            otherHero: enemyHero,
            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, friendlyBoard, friendlyHero, wingedChimeraModifier * 1, wingedChimeraModifier * 1, gameState);
                damagedEntity.abiityChargesLeft--;
            }
            break;
        case "BG29_878":
        case "BG29_878_G":
            (0, divine_shield_1.updateDivineShield)(damagedEntity, friendlyBoard, friendlyHero, enemyHero, true, gameState);
            gameState.spectator.registerPowerTarget(damagedEntity, damagedEntity, friendlyBoard, friendlyHero, enemyHero);
            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, friendlyBoard, friendlyHero, 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, friendlyBoard, friendlyHero, enemyBoard, enemyHero, gameState, damagedEntity.friendly, false));
            break;
    }
    handleOtherEntityEffects(damagedEntity, friendlyBoard, friendlyHero, enemyBoard, enemyHero, spawnedEntities, gameState);
    const finalSpawns = (0, spawns_1.performEntitySpawns)(spawnedEntities, friendlyBoard, friendlyHero, damagedEntity, friendlyBoard.length - (friendlyBoard.indexOf(damagedEntity) + 1), enemyBoard, enemyHero, gameState);
    const entityRightToSpawns = friendlyBoard[friendlyBoard.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 : undefined;
    });
    if (damage > 0) {
        for (const entity of friendlyBoard) {
            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: friendlyBoard,
                    hero: friendlyHero,
                    gameState,
                });
            }
        }
        for (const entity of enemyBoard) {
            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: enemyBoard,
                    hero: enemyHero,
                    gameState,
                });
            }
        }
    }
    return finalSpawns;
};
exports.onEntityDamaged = onEntityDamaged;
const handleOtherEntityEffects = (entity, board, hero, otherBoard, otherHero, spawnedEntities, gameState) => {
    var _a;
    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, 3 * 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);
        });
    }
    for (const trinket of (_a = hero.trinkets) !== null && _a !== void 0 ? _a : []) {
        const trinketImpl = _card_mappings_1.cardMappings[trinket.cardId];
        if ((0, card_interface_1.hasAfterFriendlyMinionDamagedTrinket)(trinketImpl)) {
            trinketImpl.afterFriendlyMinionDamaged(trinket, {
                damagedEntity: entity,
                board,
                hero,
                gameState,
            });
        }
    }
};
//# sourceMappingURL=damage-effects.js.map