"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 });
exports.sendRequests = exports.queueRequest = exports.parseRecord = exports.validateRecordTypes = exports.updateRecord = exports.dropInvalidTxs = exports.getTxs = exports.reset = exports.init = void 0;
const Snapshot = __importStar(require("../snapshot"));
const Context = __importStar(require("./Context"));
const Self = __importStar(require("./Self"));
/** STATE */
let p2pLogger;
/** ROUTES */
/*
const gossipRoute: P2P.P2PTypes.GossipHandler = (payload) => {}

const routes = {
  internal: {},
  gossip: {
    gossip: gossipRoute,
  },
}
*/
let cycleNumberForNetworkDataHash = 0;
let cycleNumberForNetworkReceiptHash = 0;
let cycleNumberForNetworkSummaryHash = 0;
/** FUNCTIONS */
/** CycleCreator Functions */
/* These functions must be defined by all modules that implement a
     network action like going active, lost node detection, etc.
     These functions are called by CycleCreator
*/
function init() {
    // Init logger
    p2pLogger = Context.logger.getLogger('p2p');
    // Init state
    reset();
    // Register routes
    /*
    for (const [name, handler] of Object.entries(routes.internal)) {
      Comms.registerInternal(name, handler)
    }
    for (const [name, handler] of Object.entries(routes.gossip)) {
      Comms.registerGossipHandler(name, handler)
    }
    */
}
exports.init = init;
function reset() { }
exports.reset = reset;
function getTxs() {
    return;
}
exports.getTxs = getTxs;
function dropInvalidTxs(txs) {
    return;
}
exports.dropInvalidTxs = dropInvalidTxs;
/*
Given the txs and prev cycle record mutate the referenced record
*/
function updateRecord(txs, record, prev) {
    // If you're the first node
    if (Self.isFirst) {
        // Get saftey mode field values from snapshot
        Object.assign(record, Snapshot.safetyModeVals);
    }
    // If you're not the first node
    else {
        // Just copy the safety mode, safteyNum, and networkStateHash vals for now
        if (prev) {
            record.safetyMode = prev.safetyMode;
            record.safetyNum = prev.safetyNum;
            record.networkStateHash = prev.networkStateHash;
        }
    }
    /**
     * [NOTE] The check for turning off safety mode once safefy number of nodes
     * have become active should probably go here.
     */
    if (record.safetyMode === true && prev) {
        if (prev.active >= prev.safetyNum) {
            record.safetyMode = false;
        }
    }
    const stateHashes = Snapshot.getStateHashes(cycleNumberForNetworkDataHash);
    if (stateHashes && stateHashes.length > 0) {
        record.networkDataHash = stateHashes.map((stateHash) => {
            return {
                cycle: stateHash.counter,
                hash: stateHash.networkHash,
            };
        });
        if (record.networkDataHash.length > 0) {
            cycleNumberForNetworkDataHash = record.networkDataHash[record.networkDataHash.length - 1].cycle + 1;
        }
    }
    else {
        record.networkDataHash = [];
    }
    const receiptHashes = Snapshot.getReceiptHashes(cycleNumberForNetworkReceiptHash);
    if (receiptHashes && receiptHashes.length > 0) {
        record.networkReceiptHash = receiptHashes.map((receiptHash) => {
            return {
                cycle: receiptHash.counter,
                hash: receiptHash.networkReceiptHash,
            };
        });
        if (record.networkReceiptHash.length > 0) {
            cycleNumberForNetworkReceiptHash = record.networkReceiptHash[record.networkReceiptHash.length - 1].cycle + 1;
        }
    }
    else {
        record.networkReceiptHash = [];
    }
    const summaryHashes = Snapshot.getSummaryHashes(cycleNumberForNetworkSummaryHash);
    if (summaryHashes && summaryHashes.length > 0) {
        record.networkSummaryHash = summaryHashes.map((stateHash) => {
            return {
                cycle: stateHash.counter,
                hash: stateHash.networkSummaryHash,
            };
        });
        if (record.networkSummaryHash.length > 0) {
            cycleNumberForNetworkSummaryHash = record.networkSummaryHash[record.networkSummaryHash.length - 1].cycle + 1;
        }
    }
    else {
        record.networkSummaryHash = [];
    }
}
exports.updateRecord = updateRecord;
function validateRecordTypes(rec) {
    // [TODO] Implement actual validation
    return '';
}
exports.validateRecordTypes = validateRecordTypes;
function parseRecord(record) {
    return {
        added: [],
        removed: [],
        updated: [],
    };
}
exports.parseRecord = parseRecord;
function queueRequest(request) { }
exports.queueRequest = queueRequest;
function sendRequests() { }
exports.sendRequests = sendRequests;
//# sourceMappingURL=SafetyMode.js.map