var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { getEnv } from "@ledgerhq/live-env";
import axios, { AxiosError } from "axios";
const LEDGER_COUNTERVALUES_API = getEnv("LEDGER_COUNTERVALUES_API");
export const fetchTokensFromCDN = (filename) => __awaiter(void 0, void 0, void 0, function* () {
    try {
        const { data, headers } = yield axios.get(`${getEnv("DYNAMIC_CAL_BASE_URL")}/${filename}`);
        return [data, headers.etag];
    }
    catch (err) {
        const error = err;
        throw new Error(`${error.message} ${getEnv("DYNAMIC_CAL_BASE_URL")}/${filename}`);
    }
});
export const fetchTokensFromCALService = (chainDetails, output, etag, next) => __awaiter(void 0, void 0, void 0, function* () {
    var _a, _b;
    try {
        const { data, headers } = yield axios.get(`${getEnv("CAL_SERVICE_URL")}/v1/tokens`, {
            params: Object.assign(Object.assign({}, ((next === null || next === void 0 ? void 0 : next.cursor) ? { cursor: next.cursor } : {})), { chain_id: chainDetails.chainId, standard: chainDetails.standard, blockchain_name: chainDetails.blockchain_name, output: output.join() }),
            headers: etag
                ? {
                    "If-None-Match": etag,
                }
                : undefined,
        });
        const cursor = headers["x-ledger-next"];
        if (cursor) {
            return fetchTokensFromCALService({ chainId: chainDetails.chainId, standard: chainDetails.standard }, output, etag, {
                tokens: (next === null || next === void 0 ? void 0 : next.tokens) ? [...next.tokens, ...data] : data,
                cursor,
            });
        }
        const hash = headers["etag"];
        return (next === null || next === void 0 ? void 0 : next.tokens) ? { tokens: [...next.tokens, ...data], hash } : { tokens: data, hash };
    }
    catch (err) {
        if (err instanceof AxiosError) {
            if (((_a = err.response) === null || _a === void 0 ? void 0 : _a.status) === 304) {
                throw err;
            }
            throw new Error(`${err === null || err === void 0 ? void 0 : err.message}: ${(_b = err === null || err === void 0 ? void 0 : err.config) === null || _b === void 0 ? void 0 : _b.url}`);
        }
        throw err;
    }
});
export const fetchTokensOrderedByMarketCap = () => __awaiter(void 0, void 0, void 0, function* () {
    const { data } = yield axios.get(`${LEDGER_COUNTERVALUES_API}/v3/supported/crypto`);
    return { tokens: data };
});
//# sourceMappingURL=index.js.map