"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseAbstractClient = exports.UpdateFailureReason = void 0;
exports.isUpdateFailureReason = isUpdateFailureReason;
const utils_1 = require("../utils");
const 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);
}
class BaseAbstractClient {
    eventSearchConfig;
    cachingMechanism;
    _isUpdated;
    firstHeightToSearch = 0;
    latestHeightSearched = 0;
    constructor(eventSearchConfig = { from: 0, maxLookBack: 0 }, cachingMechanism) {
        this.eventSearchConfig = eventSearchConfig;
        this.cachingMechanism = cachingMechanism;
        this._isUpdated = false;
    }
    get isUpdated() {
        return this._isUpdated;
    }
    set isUpdated(value) {
        if (this._isUpdated === true && !value) {
            throw new Error("Cannot set isUpdated to false once it is true");
        }
        this._isUpdated = value;
    }
    async updateSearchConfig(provider) {
        const from = this.firstHeightToSearch;
        let { to } = this.eventSearchConfig;
        if ((0, utils_1.isDefined)(to)) {
            if (from > to) {
                throw new Error(`Invalid event search config from (${from}) > to (${to})`);
            }
        }
        else {
            to = await provider.getBlockNumber();
            if (to < from) {
                return UpdateFailureReason.AlreadyUpdated;
            }
        }
        const { maxLookBack } = this.eventSearchConfig;
        return { from, to, maxLookBack };
    }
    async updateSvmSearchConfig(provider, logger) {
        const from = this.firstHeightToSearch;
        let { to } = this.eventSearchConfig;
        if ((0, utils_1.isDefined)(to)) {
            if (from > to) {
                throw new Error(`Invalid event search config from (${from}) > to (${to})`);
            }
        }
        else {
            const opts = undefined;
            const { slot } = await (0, svm_1.getNearestSlotTime)(provider, opts, logger);
            to = Number(slot);
            if (to < from) {
                return UpdateFailureReason.AlreadyUpdated;
            }
        }
        const { maxLookBack } = this.eventSearchConfig;
        return { from, to, maxLookBack };
    }
    assertUpdated() {
        if (!this.isUpdated) {
            throw new Error("Client not updated");
        }
    }
    hasCachingMechanism() {
        return (0, utils_1.isDefined)(this.cachingMechanism);
    }
}
exports.BaseAbstractClient = BaseAbstractClient;
//# sourceMappingURL=BaseAbstractClient.js.map