import { __awaiter, __generator } from "tslib";
import axios from "axios";
var _a = process.env.ACROSS_USER_AGENT, ACROSS_USER_AGENT = _a === void 0 ? "across-protocol" : _a;
var AbstractAdapter = /** @class */ (function () {
    function AbstractAdapter(name, path, opts) {
        var _a, _b, _c;
        this.name = name;
        this.path = path;
        this.timeout = (_a = opts === null || opts === void 0 ? void 0 : opts.timeout) !== null && _a !== void 0 ? _a : 2000;
        this.retries = (_b = opts === null || opts === void 0 ? void 0 : opts.retries) !== null && _b !== void 0 ? _b : 1;
        this.throw = (_c = opts === null || opts === void 0 ? void 0 : opts.throwOnError) !== null && _c !== void 0 ? _c : true;
        this.logger = opts === null || opts === void 0 ? void 0 : opts.logger;
    }
    AbstractAdapter.prototype.sleep = function (ms) {
        return new Promise(function (r) { return setTimeout(r, ms); });
    };
    AbstractAdapter.prototype.fetch = function (name, url, timeout, retries) {
        if (timeout === void 0) { timeout = 2000; }
        if (retries === void 0) { retries = 1; }
        return __awaiter(this, void 0, void 0, function () {
            var args, errs, tries, err_1, errMsg;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        args = {
                            headers: { "User-Agent": ACROSS_USER_AGENT },
                            timeout: timeout,
                        };
                        errs = [];
                        tries = 0;
                        _a.label = 1;
                    case 1:
                        _a.trys.push([1, 3, , 6]);
                        return [4 /*yield*/, axios(url, args)];
                    case 2: return [2 /*return*/, (_a.sent()).data];
                    case 3:
                        err_1 = _a.sent();
                        errMsg = axios.isAxiosError(err_1) || err_1 instanceof Error ? err_1.message : "unknown error";
                        errs.push(errMsg);
                        if (!(++tries <= retries)) return [3 /*break*/, 5];
                        return [4 /*yield*/, this.sleep(Math.pow(1.5, tries) * 1000)];
                    case 4:
                        _a.sent(); // simple backoff
                        _a.label = 5;
                    case 5: return [3 /*break*/, 6];
                    case 6:
                        if (tries <= retries) return [3 /*break*/, 1];
                        _a.label = 7;
                    case 7: throw new Error("".concat(name, " retrieval failure (").concat(errs.join(", "), ")"));
                }
            });
        });
    };
    AbstractAdapter.prototype.error = function (error) {
        var _a;
        if (this.throw) {
            throw error;
        }
        var reason;
        if (error instanceof Error) {
            reason = error.message;
        }
        else {
            reason = typeof error === "string" ? error : "unknown error";
        }
        var _b = this, name = _b.name, path = _b.path, timeout = _b.timeout, retries = _b.retries;
        (_a = this.logger) === null || _a === void 0 ? void 0 : _a.warn({
            at: "".concat(name, "::update"),
            message: "Failed to read addresses from ".concat(name, "."),
            reason: reason,
            path: path,
            retries: retries,
            timeout: timeout,
        });
        return Promise.resolve([]);
    };
    return AbstractAdapter;
}());
export { AbstractAdapter };
//# sourceMappingURL=abstract.js.map