"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HalofiGameBalancesApiSource = void 0;
const axios_1 = __importDefault(require("axios"));
const _types_1 = require("../../../types");
const halofi_game_constants_1 = require("./halofi.game.constants");
class HalofiGameBalancesApiSource {
    getPlayerGames(network, address) {
        return __awaiter(this, void 0, void 0, function* () {
            const url = `${halofi_game_constants_1.BASE_API_URL}/players/active-games?networkId=${_types_1.NETWORK_IDS[network]}&playerAddress=${address}`;
            return (0, halofi_game_constants_1.sendRequestWithThrottle)(axios_1.default.get(url));
        });
    }
    getBalances(address, network) {
        return __awaiter(this, void 0, void 0, function* () {
            const args = [network, address];
            const response = yield (0, halofi_game_constants_1.retry)(this.getPlayerGames, args);
            const player = response.data;
            const balances = {};
            for (let i = 0; i < player.length; i += 1) {
                const { gameId, incentiveAmount, interestAmount, withdrawn, isWinner, paidAmount, playerId, rewardAmount, totalEarningsConverted, gameAPY, rewards, } = player[i];
                let playerIncentiveAmount = incentiveAmount;
                let playerRewardAmount = rewardAmount;
                if (rewards) {
                    const playerRewards = (0, halofi_game_constants_1.transformRewardArrayToObject)(rewards);
                    playerRewardAmount = String(0);
                    playerIncentiveAmount = String(0);
                    if (playerRewards[halofi_game_constants_1.RewardType.Incentive]) {
                        playerIncentiveAmount = playerRewards[halofi_game_constants_1.RewardType.Incentive].balance;
                    }
                    if (playerRewards[halofi_game_constants_1.RewardType.Reward]) {
                        playerRewardAmount = playerRewards[halofi_game_constants_1.RewardType.Reward].balance;
                    }
                }
                balances[gameId] = {
                    incentiveAmount: parseFloat(parseFloat(playerIncentiveAmount).toFixed(3)),
                    interestAmount: parseFloat(parseFloat(interestAmount).toFixed(3)),
                    withdrawn,
                    isWinner,
                    paidAmount: parseFloat(paidAmount),
                    rewardAmount: parseFloat(parseFloat(playerRewardAmount).toFixed(3)),
                    poolAPY: parseFloat(gameAPY),
                    pooltotalEarningsConverted: parseFloat(parseFloat(totalEarningsConverted).toFixed(3)),
                    playerId,
                };
            }
            return balances;
        });
    }
}
exports.HalofiGameBalancesApiSource = HalofiGameBalancesApiSource;
//# sourceMappingURL=halofi.game.balances.api-source.js.map