"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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const logger_1 = require("../logger");
const utils = __importStar(require("../utils"));
const shardFunctions_1 = __importDefault(require("./shardFunctions"));
class PartitionObjects {
    constructor(stateManager, profiler, app, logger, storage, p2p, crypto, config) {
        this.crypto = crypto;
        this.app = app;
        this.logger = logger;
        this.config = config;
        this.profiler = profiler;
        this.p2p = p2p;
        this.storage = storage;
        this.stateManager = stateManager;
        this.mainLogger = logger.getLogger('main');
        this.fatalLogger = logger.getLogger('fatal');
        this.shardLogger = logger.getLogger('shardDump');
        this.statsLogger = logger.getLogger('statsDump');
        this.statemanager_fatal = stateManager.statemanager_fatal;
        this.nextCycleReportToSend = null;
        this.lastCycleReported = -1;
        this.partitionReportDirty = false;
        this.partitionObjectsByCycle = {};
        this.ourPartitionResultsByCycle = {};
        this.recentPartitionObjectsByCycleByHash = {};
        this.tempTXRecords = [];
        this.txByCycleByPartition = {};
        this.allPartitionResponsesByCycleByPartition = {};
        // the original way this was setup was to reset and apply repair results one partition at a time.
        // this could create issue if we have a TX spanning multiple partitions that are locally owned.
        this.resetAndApplyPerPartition = false;
    }
    /***
     *    ########     ###    ########  ######## #### ######## ####  #######  ##    ## ########  ######## ########   #######  ########  ########  ######
     *    ##     ##   ## ##   ##     ##    ##     ##     ##     ##  ##     ## ###   ## ##     ## ##       ##     ## ##     ## ##     ##    ##    ##    ##
     *    ##     ##  ##   ##  ##     ##    ##     ##     ##     ##  ##     ## ####  ## ##     ## ##       ##     ## ##     ## ##     ##    ##    ##
     *    ########  ##     ## ########     ##     ##     ##     ##  ##     ## ## ## ## ########  ######   ########  ##     ## ########     ##     ######
     *    ##        ######### ##   ##      ##     ##     ##     ##  ##     ## ##  #### ##   ##   ##       ##        ##     ## ##   ##      ##          ##
     *    ##        ##     ## ##    ##     ##     ##     ##     ##  ##     ## ##   ### ##    ##  ##       ##        ##     ## ##    ##     ##    ##    ##
     *    ##        ##     ## ##     ##    ##    ####    ##    ####  #######  ##    ## ##     ## ######## ##         #######  ##     ##    ##     ######
     */
    /**
     * getPartitionReport used by reporting (monitor server) to query if there is a partition report ready
     * @param {boolean} consensusOnly
     * @param {boolean} smallHashes
     * @returns {any}
     */
    getPartitionReport(consensusOnly, smallHashes) {
        let response = {}; // {res:[], cycleNumber:-1}
        if (this.nextCycleReportToSend != null) {
            const shardValues = this.stateManager.shardValuesByCycle.get(this.nextCycleReportToSend.cycleNumber);
            const consensusStartPartition = shardValues.nodeShardData.consensusStartPartition;
            const consensusEndPartition = shardValues.nodeShardData.consensusEndPartition;
            response = { res: [], cycleNumber: this.nextCycleReportToSend.cycleNumber };
            if (this.lastCycleReported < this.nextCycleReportToSend.cycleNumber || this.partitionReportDirty === true) {
                // consensusOnly hashes
                if (smallHashes === true) {
                    for (const r of this.nextCycleReportToSend.res) {
                        r.h = utils.makeShortHash(r.h);
                    }
                }
                for (const r of this.nextCycleReportToSend.res) {
                    if (consensusOnly) {
                        //check if partition is in our range!
                        if (shardFunctions_1.default.partitionInWrappingRange(r.i, consensusStartPartition, consensusEndPartition)) {
                            response.res.push(r);
                        }
                    }
                    else {
                        response.res.push(r);
                    }
                }
                // Partition_hash: partitionHash, Partition_id:
                //response = this.nextCycleReportToSend
                this.lastCycleReported = this.nextCycleReportToSend.cycleNumber; // update reported cycle
                this.nextCycleReportToSend = null; // clear it because we sent it
                this.partitionReportDirty = false; // not dirty anymore
                /* prettier-ignore */ if (logger_1.logFlags.debug)
                    this.mainLogger.debug('getPartitionReport: ' + `insync: ${this.stateManager.stateIsGood} ` + utils.stringifyReduce(response));
            }
        }
        return response;
    }
    /***
     *    ######## ##    ## ########        ########   #######  #### ##    ## ########  ######
     *    ##       ###   ## ##     ##       ##     ## ##     ##  ##  ###   ##    ##    ##    ##
     *    ##       ####  ## ##     ##       ##     ## ##     ##  ##  ####  ##    ##    ##
     *    ######   ## ## ## ##     ##       ########  ##     ##  ##  ## ## ##    ##     ######
     *    ##       ##  #### ##     ##       ##        ##     ##  ##  ##  ####    ##          ##
     *    ##       ##   ### ##     ##       ##        ##     ##  ##  ##   ###    ##    ##    ##
     *    ######## ##    ## ########        ##         #######  #### ##    ##    ##     ######
     */
    // eslint-disable-next-line
    setupHandlers() { }
}
exports.default = PartitionObjects;
//# sourceMappingURL=PartitionObjects.js.map