"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchTokensOrderedByMarketCap = exports.fetchTokensFromCALService = exports.fetchTokensFromCDN = void 0;
const live_env_1 = require("@ledgerhq/live-env");
const axios_1 = __importStar(require("axios"));
const LEDGER_COUNTERVALUES_API = (0, live_env_1.getEnv)("LEDGER_COUNTERVALUES_API");
const fetchTokensFromCDN = (filename) => __awaiter(void 0, void 0, void 0, function* () {
    try {
        const { data, headers } = yield axios_1.default.get(`${(0, live_env_1.getEnv)("DYNAMIC_CAL_BASE_URL")}/${filename}`);
        return [data, headers.etag];
    }
    catch (err) {
        const error = err;
        throw new Error(`${error.message} ${(0, live_env_1.getEnv)("DYNAMIC_CAL_BASE_URL")}/${filename}`);
    }
});
exports.fetchTokensFromCDN = fetchTokensFromCDN;
const fetchTokensFromCALService = (chainDetails, output, etag, next) => __awaiter(void 0, void 0, void 0, function* () {
    var _a, _b;
    try {
        const { data, headers } = yield axios_1.default.get(`${(0, live_env_1.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 (0, exports.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 axios_1.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;
    }
});
exports.fetchTokensFromCALService = fetchTokensFromCALService;
const fetchTokensOrderedByMarketCap = () => __awaiter(void 0, void 0, void 0, function* () {
    const { data } = yield axios_1.default.get(`${LEDGER_COUNTERVALUES_API}/v3/supported/crypto`);
    return { tokens: data };
});
exports.fetchTokensOrderedByMarketCap = fetchTokensOrderedByMarketCap;
//# sourceMappingURL=index.js.map