"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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchTokensFromCALService = exports.fetchTokensFromCDN = void 0;
const live_env_1 = require("@ledgerhq/live-env");
const axios_1 = __importStar(require("axios"));
const fetchTokensFromCDN = async (filename) => {
    try {
        const { data, headers } = await 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 = async (chainDetails, output, etag, next) => {
    const ref = (0, live_env_1.getEnv)("CAL_REF") || undefined;
    try {
        const { data, headers } = await axios_1.default.get(`${(0, live_env_1.getEnv)("CAL_SERVICE_URL")}/v1/tokens`, {
            params: {
                ...(next?.cursor ? { cursor: next.cursor } : {}),
                chain_id: chainDetails.chainId,
                standard: chainDetails.standard,
                blockchain_name: chainDetails.blockchain_name,
                output: output.join(),
                ref,
            },
            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?.tokens ? [...next.tokens, ...data] : data,
                cursor,
            });
        }
        const hash = headers["etag"];
        return next?.tokens ? { tokens: [...next.tokens, ...data], hash } : { tokens: data, hash };
    }
    catch (err) {
        if (err instanceof axios_1.AxiosError) {
            if (err.response?.status === 304) {
                throw err;
            }
            throw new Error(`${err?.message}: ${err?.config?.url}`);
        }
        throw err;
    }
};
exports.fetchTokensFromCALService = fetchTokensFromCALService;
//# sourceMappingURL=index.js.map