"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseHTTPAdapter = void 0;
const tslib_1 = require("tslib");
const utils_1 = require("../../utils");
const assert_1 = tslib_1.__importDefault(require("assert"));
class BaseHTTPAdapter {
    name;
    host;
    _retries = 0;
    _timeout = 0;
    get retries() {
        return this._retries;
    }
    set retries(retries) {
        (0, assert_1.default)(retries >= 0);
        this._retries = retries;
    }
    get timeout() {
        return this._timeout;
    }
    set timeout(timeout) {
        (0, assert_1.default)(timeout >= 0);
        this._timeout = timeout;
    }
    constructor(name, host, { timeout = 1000, retries = 1 }) {
        this.name = name;
        this.host = host;
        this.retries = retries;
        this.timeout = timeout;
    }
    async query(path, urlArgs) {
        const url = `https://${this.host}/${path ?? ""}`;
        const errs = [];
        let tries = 0;
        do {
            try {
                return await (0, utils_1.fetchWithTimeout)(url, urlArgs ?? {}, { "User-Agent": process.env.ACROSS_USER_AGENT ?? "across-protocol" }, this.timeout);
            }
            catch (err) {
                const errMsg = err instanceof Error ? err.message : "unknown error";
                errs.push(errMsg);
                if (++tries <= this.retries)
                    await this.sleep(Math.pow(1.5, tries) * 1000);
            }
        } while (tries <= this.retries);
        throw new Error(`${this.name} price lookup failure (${errs.join(", ")})`);
    }
    sleep(ms) {
        return new Promise((r) => setTimeout(r, ms));
    }
}
exports.BaseHTTPAdapter = BaseHTTPAdapter;
//# sourceMappingURL=baseAdapter.js.map