"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseAbstractClient = exports.isUpdateFailureReason = exports.UpdateFailureReason = void 0;
var tslib_1 = require("tslib");
var ethers_1 = require("ethers");
var utils_1 = require("../utils");
var svm_1 = require("../arch/svm");
var UpdateFailureReason;
(function (UpdateFailureReason) {
    UpdateFailureReason[UpdateFailureReason["NotReady"] = 0] = "NotReady";
    UpdateFailureReason[UpdateFailureReason["AlreadyUpdated"] = 1] = "AlreadyUpdated";
    UpdateFailureReason[UpdateFailureReason["BadRequest"] = 2] = "BadRequest";
    UpdateFailureReason[UpdateFailureReason["RPCError"] = 3] = "RPCError";
})(UpdateFailureReason || (exports.UpdateFailureReason = UpdateFailureReason = {}));
function isUpdateFailureReason(x) {
    return Number.isInteger(x);
}
exports.isUpdateFailureReason = isUpdateFailureReason;
var BaseAbstractClient = (function () {
    function BaseAbstractClient(eventSearchConfig, cachingMechanism) {
        if (eventSearchConfig === void 0) { eventSearchConfig = { from: 0, maxLookBack: 0 }; }
        this.eventSearchConfig = eventSearchConfig;
        this.cachingMechanism = cachingMechanism;
        this.firstHeightToSearch = 0;
        this.latestHeightSearched = 0;
        this._isUpdated = false;
    }
    Object.defineProperty(BaseAbstractClient.prototype, "isUpdated", {
        get: function () {
            return this._isUpdated;
        },
        set: function (value) {
            if (this._isUpdated === true && !value) {
                throw new Error("Cannot set isUpdated to false once it is true");
            }
            this._isUpdated = value;
        },
        enumerable: false,
        configurable: true
    });
    BaseAbstractClient.prototype.updateSearchConfig = function (provider) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var from, to, slot, maxLookBack;
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        from = this.firstHeightToSearch;
                        to = this.eventSearchConfig.to;
                        if (!(0, utils_1.isDefined)(to)) return [3, 1];
                        if (from > to) {
                            throw new Error("Invalid event search config from (".concat(from, ") > to (").concat(to, ")"));
                        }
                        return [3, 6];
                    case 1:
                        if (!(provider instanceof ethers_1.providers.Provider)) return [3, 3];
                        return [4, provider.getBlockNumber()];
                    case 2:
                        to = _a.sent();
                        return [3, 5];
                    case 3: return [4, (0, svm_1.getNearestSlotTime)(provider)];
                    case 4:
                        slot = (_a.sent()).slot;
                        to = Number(slot);
                        _a.label = 5;
                    case 5:
                        if (to < from) {
                            return [2, UpdateFailureReason.AlreadyUpdated];
                        }
                        _a.label = 6;
                    case 6:
                        maxLookBack = this.eventSearchConfig.maxLookBack;
                        return [2, { from: from, to: to, maxLookBack: maxLookBack }];
                }
            });
        });
    };
    BaseAbstractClient.prototype.assertUpdated = function () {
        if (!this.isUpdated) {
            throw new Error("Client not updated");
        }
    };
    BaseAbstractClient.prototype.hasCachingMechanism = function () {
        return (0, utils_1.isDefined)(this.cachingMechanism);
    };
    return BaseAbstractClient;
}());
exports.BaseAbstractClient = BaseAbstractClient;
//# sourceMappingURL=BaseAbstractClient.js.map