"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleRebornForEntity = void 0;
const card_interface_1 = require("../cards/card.interface");
const _card_mappings_1 = require("../cards/impl/_card-mappings");
const sinrunner_blanchy_1 = require("../cards/impl/minion/sinrunner-blanchy");
const utils_1 = require("../utils");
const deathrattle_spawns_1 = require("./deathrattle-spawns");
const spawns_1 = require("./spawns");
const stats_1 = require("./stats");
const handleRebornForEntity = (boardWithKilledMinion, boardWithKilledMinionHero, deadEntity, deadMinionIndexFromRight, opponentBoard, opponentBoardHero, gameState) => {
    var _a, _b;
    const numberOfReborns = 1;
    let entityToSpawn = null;
    if ((0, sinrunner_blanchy_1.isSinrunnerBlanchy)(deadEntity.cardId) ||
        ((_a = deadEntity.enchantments) === null || _a === void 0 ? void 0 : _a.some((e) => e.cardId === "BG31_HERO_801ptje"))) {
        entityToSpawn = (0, sinrunner_blanchy_1.buildSinrunnerBlanchyRebornEntity)(deadEntity);
    }
    const entitiesFromReborn = deadEntity.reborn && deadMinionIndexFromRight >= 0
        ? (0, deathrattle_spawns_1.spawnEntities)(deadEntity.cardId, numberOfReborns, boardWithKilledMinion, boardWithKilledMinionHero, opponentBoard, opponentBoardHero, gameState, deadEntity.friendly, false, true, true, entityToSpawn, deadEntity)
        : [];
    if (!entitiesFromReborn.length) {
        return;
    }
    if (deadEntity.cardId === "BG30_109" ||
        deadEntity.cardId === "BG30_109_G") {
        entitiesFromReborn.forEach((e) => {
            const attack = deadEntity.maxAttack;
            const health = deadEntity.cardId === "BG30_109_G" ? deadEntity.maxHealth : 1;
            e.attack = attack;
            e.health = health;
        });
    }
    for (const entity of entitiesFromReborn) {
        const rebornImpl = _card_mappings_1.cardMappings[entity.cardId];
        if ((0, card_interface_1.hasRebornSelfEffect)(rebornImpl)) {
            rebornImpl.rebornSelfEffect(entity, {
                rebornEntity: deadEntity,
                boardWithKilledMinion,
                boardWithKilledMinionHero,
                opponentBoard,
                opponentBoardHero,
                gameState,
            });
        }
    }
    for (const entity of boardWithKilledMinion) {
        const rebornImpl = _card_mappings_1.cardMappings[entity.cardId];
        if ((0, card_interface_1.hasRebornEffect)(rebornImpl)) {
            rebornImpl.rebornEffect(entity, {
                rebornEntity: deadEntity,
                boardWithKilledMinion,
                boardWithKilledMinionHero,
                opponentBoard,
                opponentBoardHero,
                gameState,
            });
        }
    }
    const entitiesThatWereReborn = (0, spawns_1.performEntitySpawns)(entitiesFromReborn, boardWithKilledMinion, boardWithKilledMinionHero, deadEntity, deadMinionIndexFromRight, opponentBoard, opponentBoardHero, gameState);
    entitiesThatWereReborn.forEach((e) => (e.rebornFromEntityId = deadEntity.entityId));
    const entityRightToSpawns = deadMinionIndexFromRight === 0
        ? null
        : boardWithKilledMinion[boardWithKilledMinion.length - deadMinionIndexFromRight];
    entitiesThatWereReborn.forEach((entity) => {
        var _a;
        entity.hasAttacked = deadEntity.hasAttacked > 1 ? 1 : (_a = entityRightToSpawns === null || entityRightToSpawns === void 0 ? void 0 : entityRightToSpawns.hasAttacked) !== null && _a !== void 0 ? _a : undefined;
    });
    for (const rebornEntity of entitiesThatWereReborn) {
        for (const entity of boardWithKilledMinion) {
            const rebornImpl = _card_mappings_1.cardMappings[entity.cardId];
            if ((0, card_interface_1.hasAfterReborn)(rebornImpl)) {
                rebornImpl.afterReborn(entity, {
                    rebornEntity: rebornEntity,
                    boardWithKilledMinion,
                    boardWithKilledMinionHero,
                    opponentBoard,
                    opponentBoardHero,
                    gameState,
                });
            }
        }
        for (const trinket of (_b = boardWithKilledMinionHero.trinkets) !== null && _b !== void 0 ? _b : []) {
            const rebornImpl = _card_mappings_1.cardMappings[trinket.cardId];
            if ((0, card_interface_1.hasAfterReborn)(rebornImpl)) {
                rebornImpl.afterReborn(trinket, {
                    rebornEntity: rebornEntity,
                    boardWithKilledMinion,
                    boardWithKilledMinionHero,
                    opponentBoard,
                    opponentBoardHero,
                    gameState,
                });
            }
        }
    }
    boardWithKilledMinion
        .filter((e) => e.cardId === "TB_BaconShop_HERO_22_Buddy" ||
        e.cardId === "TB_BaconShop_HERO_22_Buddy_G")
        .forEach((arfus) => {
        const multiplier = arfus.cardId === "TB_BaconShop_HERO_22_Buddy_G" ? 2 : 1;
        const attackBonus = arfus.attack * multiplier;
        entitiesThatWereReborn
            .filter((e) => e.entityId !== arfus.entityId && e.rebornFromEntityId !== arfus.entityId)
            .forEach((e) => {
            (0, stats_1.modifyStats)(e, arfus, attackBonus, 0, boardWithKilledMinion, boardWithKilledMinionHero, gameState);
        });
    });
    const numberOfTriggersForDeathwhisper = Math.min(entitiesThatWereReborn.length, 1);
    for (let i = 0; i < numberOfTriggersForDeathwhisper; i++) {
        boardWithKilledMinion
            .filter((e) => e.cardId === "BG25_020" ||
            e.cardId === "BG25_020_G")
            .forEach((e) => {
            const multiplier = e.cardId === "BG25_020_G" ? 2 : 1;
            (0, utils_1.addStatsToBoard)(e, boardWithKilledMinion, boardWithKilledMinionHero, multiplier * 1, multiplier * 3, gameState);
        });
    }
};
exports.handleRebornForEntity = handleRebornForEntity;
//# sourceMappingURL=reborn.js.map