import { __awaiter, __extends, __generator } from "tslib";
import { BaseHTTPAdapter } from "./baseAdapter";
var PriceFeed = /** @class */ (function (_super) {
    __extends(PriceFeed, _super);
    function PriceFeed(_a) {
        var _b = _a === void 0 ? {} : _a, name = _b.name, apiKey = _b.apiKey, _c = _b.timeout, timeout = _c === void 0 ? 5000 : _c, _d = _b.retries, retries = _d === void 0 ? 3 : _d;
        var _this = _super.call(this, (name !== null && name !== void 0 ? name : apiKey) ? "CoinGecko Pro" : "CoinGecko Free", apiKey ? "pro-api.coingecko.com" : "api.coingecko.com", {
            timeout: timeout,
            retries: retries,
        }) || this;
        _this.apiKey = undefined;
        _this.apiKey = apiKey;
        return _this;
    }
    PriceFeed.prototype.getPriceByAddress = function (address_1) {
        return __awaiter(this, arguments, void 0, function (address, currency) {
            var price;
            if (currency === void 0) { currency = "usd"; }
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.getPricesByAddress([address], currency)];
                    case 1:
                        price = _a.sent();
                        return [2 /*return*/, price[0]];
                }
            });
        });
    };
    PriceFeed.prototype.getPricesByAddress = function (addresses_1) {
        return __awaiter(this, arguments, void 0, function (addresses, currency) {
            var queryArgs, prices;
            if (currency === void 0) { currency = "usd"; }
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        queryArgs = {
                            contract_addresses: addresses.map(function (address) { return address.toLowerCase(); }).join(","),
                            vs_currencies: currency,
                            include_last_updated_at: true,
                        };
                        if (this.apiKey)
                            queryArgs["x_cg_pro_api_key"] = this.apiKey;
                        return [4 /*yield*/, this.query("api/v3/simple/token_price/ethereum", queryArgs)];
                    case 1:
                        prices = _a.sent();
                        if (!this.validateResponse(prices, currency))
                            throw new Error("Unexpected ".concat(this.name, " response: ").concat(JSON.stringify(prices)));
                        return [2 /*return*/, addresses
                                .filter(function (address) { return prices[address.toLowerCase()] !== undefined; })
                                .map(function (address) {
                                var price = prices[address.toLowerCase()];
                                return { address: address, price: price[currency], timestamp: price.last_updated_at };
                            })];
                }
            });
        });
    };
    PriceFeed.prototype.validateResponse = function (response, currency) {
        if (typeof response !== "object")
            return false;
        return Object.entries(response).every(function (_a) {
            var address = _a[0], tokenPrice = _a[1];
            // prettier-ignore
            return (/0x[0-9a-fA-F]{40}/.exec(address) !== undefined
                && typeof tokenPrice === "object"
                && !isNaN(tokenPrice[currency])
                && !isNaN(tokenPrice.last_updated_at));
        });
    };
    return PriceFeed;
}(BaseHTTPAdapter));
export { PriceFeed };
//# sourceMappingURL=coingecko.js.map