"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.listSupportedFiats = exports.OFAC_CURRENCIES = void 0;
const cryptoassets_1 = require("@ledgerhq/cryptoassets");
const live_env_1 = require("@ledgerhq/live-env");
const logs_1 = require("@ledgerhq/logs");
let userSupportedFiats = null;
// The API returns Coingeko countervalues tickers, but getFiatCurrencyByTicker might not support each of those.
const locallySupportedFiats = [
    "AED",
    "AUD",
    "BHD",
    "BRL",
    "CAD",
    "CHF",
    "CLP",
    "CNY",
    "CZK",
    "DKK",
    "EUR",
    "GBP",
    "HKD",
    "HUF",
    "IDR",
    "ILS",
    "INR",
    "JPY",
    "KRW",
    "MXN",
    "MYR",
    "NGN",
    "NOK",
    "NZD",
    "PHP",
    "PKR",
    "PLN",
    "RUB",
    "SEK",
    "SGD",
    "THB",
    "TRY",
    "UAH",
    "USD",
    "VND",
    "ZAR",
];
exports.OFAC_CURRENCIES = [
    "AFN",
    "BYN",
    "CUP",
    "CUC",
    "IRR",
    "IQD",
    "KPW",
    "RUB",
    "SDG",
    "SYP",
    "MMK",
];
async function initializeUserSupportedFiats() {
    const remoteSupportedTokens = await fetchSupportedFiatsTokens();
    let supportedTokens = [];
    if (remoteSupportedTokens.length !== 0) {
        remoteSupportedTokens.forEach(token => {
            if ((0, cryptoassets_1.hasFiatCurrencyTicker)(token)) {
                supportedTokens.push(token);
            }
        });
    }
    else {
        supportedTokens = locallySupportedFiats;
    }
    userSupportedFiats = supportedTokens
        .filter(currency => !exports.OFAC_CURRENCIES.includes(currency))
        .map(cryptoassets_1.getFiatCurrencyByTicker);
}
async function fetchSupportedFiatsTokens() {
    try {
        const response = await fetch(`${(0, live_env_1.getEnv)("LEDGER_COUNTERVALUES_API")}/v3/supported/fiat`, {
            method: "GET",
            headers: {
                accept: "application/json",
            },
        });
        if (!response.ok) {
            throw new Error(`Request failed with status ${response.status}`);
        }
        const data = await response.json();
        return data;
    }
    catch (error) {
        (0, logs_1.log)("debug", `Failed to fetch supported fiat tokens. Error Message: ${error}`);
        return [];
    }
}
async function listSupportedFiats() {
    if (userSupportedFiats === null) {
        // Handle case where userSupportedFiats is not yet populated (e.g., by calling initializeUserSupportedFiats)
        try {
            await initializeUserSupportedFiats();
        }
        catch (error) {
            // Handle initialization error
            (0, logs_1.log)("debug", `Failed to initialize userSupportedFiats. Error Message: ${error}`);
            return userSupportedFiats || [];
        }
        return userSupportedFiats || [];
    }
    return userSupportedFiats;
}
exports.listSupportedFiats = listSupportedFiats;
//# sourceMappingURL=support.js.map