"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchMarketPerformers = exports.fetchCurrency = exports.fetchCurrencyChartData = exports.supportedCounterCurrencies = exports.fetchList = exports.getSupportedCoinsList = void 0;
const live_network_1 = __importDefault(require("@ledgerhq/live-network"));
const live_env_1 = require("@ledgerhq/live-env");
const types_1 = require("../utils/types");
const rangeDataTable_1 = require("../utils/rangeDataTable");
const url_1 = __importDefault(require("url"));
const utils_1 = require("../utils");
const baseURL = () => (0, live_env_1.getEnv)("LEDGER_COUNTERVALUES_API");
const ROOT_PATH = (0, live_env_1.getEnv)("MARKET_API_URL");
async function getSupportedCoinsList() {
    const url = `${ROOT_PATH}/coins/list`;
    const { data } = await (0, live_network_1.default)({ method: "GET", url });
    return data;
}
exports.getSupportedCoinsList = getSupportedCoinsList;
// fetches currencies data for selected currencies ids
async function fetchList({ counterCurrency, limit = 50, page = 1, order = types_1.Order.MarketCapDesc, search = "", liveCoinsList = [], starred = [], range = "24", }) {
    const url = url_1.default.format({
        pathname: `${baseURL()}/v3/markets`,
        query: {
            page: page,
            pageSize: limit,
            to: counterCurrency,
            sort: (0, utils_1.getSortParam)(order, range),
            ...(search.length >= 2 && { filter: search }),
            ...(starred.length > 0 && { ids: starred.sort().join(",") }),
            ...(liveCoinsList.length > 1 && { ids: liveCoinsList.sort().join(",") }),
            ...([types_1.Order.topLosers, types_1.Order.topGainers].includes(order) && { top: 100 }),
        },
    });
    const { data } = await (0, live_network_1.default)({
        method: "GET",
        url,
    });
    return data;
}
exports.fetchList = fetchList;
// Fetches list of supported counterCurrencies
async function supportedCounterCurrencies() {
    const url = `${ROOT_PATH}/simple/supported_vs_currencies`;
    const { data } = await (0, live_network_1.default)({
        method: "GET",
        url,
    });
    return data;
}
exports.supportedCounterCurrencies = supportedCounterCurrencies;
async function fetchCurrencyChartData({ id, counterCurrency, range = "24h", }) {
    const { days, interval } = rangeDataTable_1.rangeDataTable[range];
    const url = url_1.default.format({
        pathname: `${ROOT_PATH}/coins/${id}/market_chart`,
        query: {
            vs_currency: counterCurrency,
            days,
            interval,
        },
    });
    const { data } = await (0, live_network_1.default)({
        method: "GET",
        url,
    });
    return { [range]: data.prices };
}
exports.fetchCurrencyChartData = fetchCurrencyChartData;
async function fetchCurrency({ counterCurrency, id, }) {
    const url = url_1.default.format({
        pathname: `${baseURL()}/v3/markets`,
        query: {
            to: counterCurrency,
            ids: id,
            pageSize: 1,
            limit: 1,
        },
    });
    const { data } = await (0, live_network_1.default)({ method: "GET", url });
    return data[0];
}
exports.fetchCurrency = fetchCurrency;
async function fetchMarketPerformers({ counterCurrency, range, limit = 5, top = 50, sort, supported, }) {
    const sortParam = `${sort === "asc" ? "positive" : "negative"}-price-change-${(0, utils_1.getRange)(range)}`;
    const url = url_1.default.format({
        pathname: `${baseURL()}/v3/markets`,
        query: {
            to: counterCurrency,
            limit,
            top,
            sort: sortParam,
            supported,
        },
    });
    const { data } = await (0, live_network_1.default)({ method: "GET", url });
    return data;
}
exports.fetchMarketPerformers = fetchMarketPerformers;
//# sourceMappingURL=index.js.map