"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CoinStats = void 0;
const ApiHandler_1 = require("../helper/ApiHandler");
class CoinStats {
    constructor() {
        this.CoinStatsAPIKEY = process.env.COINSTATS_API_KEY || "iLCC2RViQWPIwW1C8u8RtIRA2RYBI44n2Eg+/27+Ixc=";
        this.CoinStatsApiHandler = new ApiHandler_1.ApiHandler("https://openapiv1.coinstats.app", {}, 10000);
    }
    async getBalance(address, networks) {
        var _a;
        // expect the API to return an array of { blockchain, balances: RawBalance[] }
        const response = await this.CoinStatsApiHandler.get(`/wallet/balances?address=${address}&networks=${networks}`, {
            "X-API-KEY": this.CoinStatsAPIKEY,
            accept: "application/json",
        });
        if (!response)
            return [];
        // if the handler wraps it in a { results: [...] } object, grab that, else assume it's the array directly
        const buckets = (_a = response.results) !== null && _a !== void 0 ? _a : response;
        // flatten all the balances and remap blockchain -> chain
        return buckets.flatMap((bucket) => bucket.balances.map((bal) => ({
            chain: bucket.blockchain,
            coinId: bal.coinId,
            amount: bal.amount,
            name: bal.name,
            symbol: bal.symbol,
            price: bal.price,
            priceBtc: bal.priceBtc,
            imgUrl: bal.imgUrl,
            pCh24h: bal.pCh24h,
            rank: bal.rank,
            volume: bal.volume,
            decimals: bal.decimals,
            contractAddress: bal.contractAddress,
        })));
    }
}
exports.CoinStats = CoinStats;
