import network from "@ledgerhq/live-network";
import { DEFAULT_SWAP_TIMEOUT_MS } from "../../const/timeout";
import { flattenV5CurrenciesToAndFrom } from "../../utils/flattenV5CurrenciesToAndFrom";
import { fetchCurrencyFromMock } from "./__mocks__/fetchCurrencyFrom.mocks";
import { getSwapAPIBaseURL, getSwapUserIP } from "../..";
import { getEnv } from "@ledgerhq/live-env";
export async function fetchCurrencyFrom({ baseUrl = getSwapAPIBaseURL(), currencyTo, providers, additionalCoinsFlag = false, }) {
    if (getEnv("MOCK") || getEnv("PLAYWRIGHT_RUN"))
        return flattenV5CurrenciesToAndFrom(fetchCurrencyFromMock);
    const headers = getSwapUserIP();
    const url = new URL(`${baseUrl}/currencies/from`);
    url.searchParams.append("providers-whitelist", providers.join(","));
    url.searchParams.append("additional-coins-flag", additionalCoinsFlag.toString());
    if (currencyTo) {
        url.searchParams.append("currency-to", currencyTo);
    }
    try {
        const { data } = await network({
            method: "GET",
            url: url.toString(),
            timeout: DEFAULT_SWAP_TIMEOUT_MS,
            ...(headers !== undefined ? { headers } : {}),
        });
        return flattenV5CurrenciesToAndFrom(data);
    }
    catch {
        throw Error("Something went wrong in fetchCurrencyFrom call");
    }
}
//# sourceMappingURL=fetchCurrencyFrom.js.map