"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchCurrencyTo = void 0;
const live_network_1 = __importDefault(require("@ledgerhq/live-network"));
const fetchCurrencyTo_mocks_1 = require("./__mocks__/fetchCurrencyTo.mocks");
const isIntegrationTestEnv_1 = require("../../utils/isIntegrationTestEnv");
const timeout_1 = require("../../const/timeout");
const axios_1 = __importDefault(require("axios"));
const errors_1 = require("@ledgerhq/errors");
const flattenV5CurrenciesToAndFrom_1 = require("../../utils/flattenV5CurrenciesToAndFrom");
const __1 = require("../..");
async function fetchCurrencyTo({ baseUrl = (0, __1.getSwapAPIBaseURL)(), currencyFromId, providers, additionalCoinsFlag = false, }) {
    if ((0, isIntegrationTestEnv_1.isIntegrationTestEnv)())
        return Promise.resolve((0, flattenV5CurrenciesToAndFrom_1.flattenV5CurrenciesToAndFrom)(fetchCurrencyTo_mocks_1.fetchCurrencyToMock));
    const url = new URL(`${baseUrl}/currencies/to`);
    url.searchParams.append("providers-whitelist", providers.join(","));
    url.searchParams.append("additional-coins-flag", additionalCoinsFlag.toString());
    url.searchParams.append("currency-from", currencyFromId);
    const headers = (0, __1.getSwapUserIP)();
    try {
        const { data } = await (0, live_network_1.default)({
            method: "GET",
            url: url.toString(),
            timeout: timeout_1.DEFAULT_SWAP_TIMEOUT_MS,
            ...(headers !== undefined ? { headers } : {}),
        });
        return (0, flattenV5CurrenciesToAndFrom_1.flattenV5CurrenciesToAndFrom)(data);
    }
    catch (e) {
        if (axios_1.default.isAxiosError(e)) {
            if (e.code === "ECONNABORTED") {
                // TODO: LIVE-8901 (handle request timeout)
            }
        }
        if (e instanceof errors_1.LedgerAPI4xx) {
            // TODO: LIVE-8901 (handle 4xx)
        }
        throw e;
    }
}
exports.fetchCurrencyTo = fetchCurrencyTo;
//# sourceMappingURL=fetchCurrencyTo.js.map