"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseAbstractClient = exports.UpdateFailureReason = void 0;
exports.isUpdateFailureReason = isUpdateFailureReason;
var tslib_1 = require("tslib");
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);
}
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, 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, 3];
                    case 1: return [4, provider.getBlockNumber()];
                    case 2:
                        to = _a.sent();
                        if (to < from) {
                            return [2, UpdateFailureReason.AlreadyUpdated];
                        }
                        _a.label = 3;
                    case 3:
                        maxLookBack = this.eventSearchConfig.maxLookBack;
                        return [2, { from: from, to: to, maxLookBack: maxLookBack }];
                }
            });
        });
    };
    BaseAbstractClient.prototype.updateSvmSearchConfig = function (provider, logger) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var from, to, opts, 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, 3];
                    case 1:
                        opts = undefined;
                        return [4, (0, svm_1.getNearestSlotTime)(provider, opts, logger)];
                    case 2:
                        slot = (_a.sent()).slot;
                        to = Number(slot);
                        if (to < from) {
                            return [2, UpdateFailureReason.AlreadyUpdated];
                        }
                        _a.label = 3;
                    case 3:
                        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