"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildFinalInput = void 0;
const auras_1 = require("./simulation/auras");
const enchantments_1 = require("./simulation/enchantments");
const utils_1 = require("./utils");
const buildFinalInput = (battleInput, cards, cardsData) => {
    var _a, _b;
    const entityIdContainer = { entityId: 999999999 };
    const { board: playerBoard, hand: playerHand, player: playerEntity, } = buildFinalInputForPlayer(battleInput.playerBoard, true, battleInput.gameState.anomalies, cards, cardsData, entityIdContainer);
    const { board: playerTeammateBoard, hand: playerTeammateHand, player: playerTeammateEntity, } = buildFinalInputForPlayer(battleInput.playerTeammateBoard, true, battleInput.gameState.anomalies, cards, cardsData, entityIdContainer);
    const { board: opponentBoard, hand: opponentHand, player: opponentEntity, } = buildFinalInputForPlayer(battleInput.opponentBoard, false, battleInput.gameState.anomalies, cards, cardsData, entityIdContainer);
    const { board: opponentTeammateBoard, hand: opponentTeammateHand, player: opponentTeammateEntity, } = buildFinalInputForPlayer(battleInput.opponentTeammateBoard, false, battleInput.gameState.anomalies, cards, cardsData, entityIdContainer);
    const inputReady = {
        playerBoard: {
            board: playerBoard,
            player: {
                ...playerEntity,
                hand: playerHand,
            },
        },
        playerTeammateBoard: !!playerTeammateEntity
            ? {
                board: playerTeammateBoard,
                player: {
                    ...playerTeammateEntity,
                    hand: playerTeammateHand,
                },
            }
            : null,
        opponentBoard: {
            board: opponentBoard,
            player: {
                ...opponentEntity,
                hand: opponentHand,
            },
        },
        opponentTeammateBoard: !!opponentTeammateEntity
            ? {
                board: opponentTeammateBoard,
                player: {
                    ...opponentTeammateEntity,
                    hand: opponentTeammateHand,
                },
            }
            : null,
        gameState: {
            ...battleInput.gameState,
            anomalies: (_b = (_a = battleInput.gameState.anomalies) === null || _a === void 0 ? void 0 : _a.filter((a) => !!a)) !== null && _b !== void 0 ? _b : [],
        },
    };
    return inputReady;
};
exports.buildFinalInput = buildFinalInput;
const buildFinalInputForPlayer = (playerInfo, isPlayer, anomalies, cards, cardsData, entityIdContainer) => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
    if (!playerInfo) {
        return { board: [], hand: [], player: null };
    }
    const { board, hand } = buildFinalInputBoard(playerInfo, isPlayer, cardsData, cards);
    const isGhost = ((_a = playerInfo.player) === null || _a === void 0 ? void 0 : _a.hpLeft) != null && playerInfo.player.hpLeft <= 0;
    playerInfo.player.secrets = (_d = (_c = ((_b = playerInfo.player.secrets) !== null && _b !== void 0 ? _b : playerInfo.secrets)) === null || _c === void 0 ? void 0 : _c.filter((e) => !!(e === null || e === void 0 ? void 0 : e.cardId))) !== null && _d !== void 0 ? _d : [];
    playerInfo.player.trinkets = ((_f = (_e = playerInfo.player.trinkets) === null || _e === void 0 ? void 0 : _e.filter((e) => !!(e === null || e === void 0 ? void 0 : e.cardId))) !== null && _f !== void 0 ? _f : []).sort((a, b) => a.entityId - b.entityId);
    playerInfo.player.friendly = isPlayer;
    playerInfo.player.globalInfo = (_g = playerInfo.player.globalInfo) !== null && _g !== void 0 ? _g : {};
    playerInfo.player.globalInfo.PirateAttackBonus = (_h = playerInfo.player.globalInfo.PirateAttackBonus) !== null && _h !== void 0 ? _h : 0;
    playerInfo.player.globalInfo.MechAttackBonus = (_j = playerInfo.player.globalInfo.MechAttackBonus) !== null && _j !== void 0 ? _j : 0;
    playerInfo.player.heroPowers = ((_k = playerInfo.player.heroPowers) === null || _k === void 0 ? void 0 : _k.length)
        ? playerInfo.player.heroPowers.map((hp, index) => sanitizeHeroPower(hp, index, playerInfo.player, cards))
        : [
            {
                cardId: playerInfo.player.heroPowerId,
                entityId: playerInfo.player.heroPowerEntityId,
                used: playerInfo.player.heroPowerUsed,
                info: playerInfo.player.heroPowerInfo,
                info2: playerInfo.player.heroPowerInfo2,
            },
        ];
    playerInfo.player.cardId = isGhost ? "TB_BaconShop_HERO_KelThuzad" : playerInfo.player.cardId;
    playerInfo.player.hpLeft = Math.max(1, playerInfo.player.hpLeft);
    (0, auras_1.setMissingAuras)(board, playerInfo.player, anomalies, cards);
    (0, auras_1.setImplicitDataHero)(playerInfo.player, cardsData, isPlayer, entityIdContainer);
    return { board, hand, player: playerInfo.player };
};
const sanitizeHeroPower = (hp, index, player, cards) => {
    return hp;
};
const buildFinalInputBoard = (playerInfo, isPlayer, cardsData, cards) => {
    var _a, _b;
    const board = playerInfo.board
        .map((entity) => (0, enchantments_1.fixEnchantments)(entity, cards))
        .map((entity) => ({
        ...entity,
        inInitialState: true,
        scriptDataNum1: cardsData.defaultScriptDataNum(entity.cardId) || entity.scriptDataNum1,
    }))
        .map((entity) => ({ ...(0, utils_1.addImpliedMechanics)(entity, cardsData), friendly: isPlayer }));
    const hand = (_b = (_a = playerInfo.player.hand) === null || _a === void 0 ? void 0 : _a.map((entity) => ({ ...entity, inInitialState: true })).map((entity) => (0, enchantments_1.fixEnchantments)(entity, cards)).map((entity) => ({ ...(0, utils_1.addImpliedMechanics)(entity, cardsData), friendly: isPlayer }))) !== null && _b !== void 0 ? _b : [];
    return { board, hand };
};
//# sourceMappingURL=input-sanitation.js.map