"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const nestedCounters_1 = require("../utils/nestedCounters");
const logger_1 = require("../logger");
const utils = __importStar(require("../utils"));
/**
 * Help provide a list of archivers that can be used to ask for data
 * Intialize the list and use the dataSourceArchiver
 * If you need another archiver to talk to call tryNextDataSourceArchiver
 */
class ArchiverDataSourceHelper {
    constructor(stateManager) {
        this.stateManager = stateManager;
    }
    initWithList(listOfArchivers) {
        utils.shuffleArray(listOfArchivers);
        this.dataSourceArchiverIndex = 0;
        this.dataSourceArchiver = listOfArchivers[this.dataSourceArchiverIndex];
        this.dataSourceArchiverList = [...listOfArchivers];
    }
    /**
     * tryNextDataSourceArchiver
     * @param debugString
     */
    tryNextDataSourceArchiver(debugString) {
        this.dataSourceArchiverIndex++;
        /* prettier-ignore */ if (logger_1.logFlags.error)
            this.stateManager.mainLogger.error(`tryNextDataSourceArchiver ${debugString} try next archiver: ${this.dataSourceArchiverIndex}`);
        if (this.dataSourceArchiverIndex >= this.dataSourceArchiverList.length) {
            /* prettier-ignore */ if (logger_1.logFlags.error)
                this.stateManager.mainLogger.error(`tryNextDataSourceArchiver ${debugString} ran out of archivers ask for data`);
            this.dataSourceArchiverIndex = 0;
            /* prettier-ignore */ nestedCounters_1.nestedCountersInstance.countEvent('archiver_sync', `tryNextDataSourceArchiver Out of tries: ${this.dataSourceArchiverIndex} of ${this.dataSourceArchiverList.length} `, 1);
            return false;
        }
        // pick new data source archiver
        this.dataSourceArchiver = this.dataSourceArchiverList[this.dataSourceArchiverIndex];
        if (this.dataSourceArchiver == null) {
            /* prettier-ignore */ nestedCounters_1.nestedCountersInstance.countEvent('archiver_sync', `tryNextDataSourceArchiver next try: ${this.dataSourceArchiverIndex} of ${this.dataSourceArchiverList.length} ARCHIVER==null`, 1);
            return false;
        }
        /* prettier-ignore */ if (logger_1.logFlags.error)
            this.stateManager.mainLogger.error(`tryNextDataSourceArchiver ${debugString} found: ${this.dataSourceArchiver.ip} ${this.dataSourceArchiver.port} `);
        /* prettier-ignore */ nestedCounters_1.nestedCountersInstance.countEvent('archiver_sync', `tryNextDataSourceArchiver next try: ${this.dataSourceArchiverIndex} of ${this.dataSourceArchiverList.length}`, 1);
        return true;
    }
    // Not sure if this is needed
    tryRestartList(debugString) {
        this.dataSourceArchiverIndex = 0;
        const numberArchivers = this.dataSourceArchiverList.length;
        //allow a list restart if we have a small number of NumberArchivers
        if (numberArchivers > 3) {
            return false;
        }
        /* prettier-ignore */ nestedCounters_1.nestedCountersInstance.countEvent('sync', `ArchiverDataSourceHelper restartList ${debugString} numberArchivers:${numberArchivers}`, 1);
        if (numberArchivers > 0) {
            this.dataSourceArchiver = this.dataSourceArchiverList[this.dataSourceArchiverIndex];
            return true;
        }
        return false;
    }
    getNumberArchivers() {
        return this.dataSourceArchiverList.length;
    }
}
exports.default = ArchiverDataSourceHelper;
//# sourceMappingURL=ArchiverDataSourceHelper.js.map