"use strict";
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateProtocolSettings = exports.blockchainSettingsToProtocolSettings = exports.createCSharpDispatchInvoke = exports.constants = void 0;
const tslib_1 = require("tslib");
const client_common_1 = require("@neo-one/client-common");
const edge_1 = require("@neo-one/edge");
const path_1 = tslib_1.__importDefault(require("path"));
const errors_1 = require("./errors");
const directory = path_1.default.basename(__dirname);
const APP_ROOT = directory === 'cjs' || directory === 'esm' ? path_1.default.resolve(__dirname, '..', '..') : path_1.default.resolve(__dirname, '..');
const CSHARP_APP_ROOT = (_a = process.env.EDGE_APP_ROOT) !== null && _a !== void 0 ? _a : path_1.default.resolve(APP_ROOT, 'lib', 'bin', 'Debug', 'netcoreapp3.0');
exports.constants = {
    APP_ROOT,
    CSHARP_APP_ROOT,
};
exports.createCSharpDispatchInvoke = (options) => {
    if (process.env.EDGE_APP_ROOT === undefined) {
        process.env.EDGE_APP_ROOT = CSHARP_APP_ROOT;
    }
    if (process.env.EDGE_USE_CORECLR === undefined) {
        process.env.EDGE_USE_CORECLR = '1';
    }
    const invokeFunction = edge_1.func(options);
    return (input) => invokeFunction(input, true);
};
exports.blockchainSettingsToProtocolSettings = (settingsIn) => ({
    network: settingsIn.network,
    addressVersion: settingsIn.addressVersion,
    standbyCommittee: settingsIn.standbyCommittee.map((ecpoint) => client_common_1.common.ecPointToString(ecpoint)),
    committeeMembersCount: settingsIn.standbyCommittee.length,
    validatorsCount: settingsIn.validatorsCount,
    millisecondsPerBlock: settingsIn.millisecondsPerBlock,
    memoryPoolMaxTransactions: settingsIn.memoryPoolMaxTransactions,
    maxTraceableBlocks: settingsIn.maxTraceableBlocks,
    initialGasDistribution: settingsIn.initialGasDistribution.toString(),
    maxTransactionsPerBlock: settingsIn.maxTransactionsPerBlock,
    nativeUpdateHistory: settingsIn.nativeUpdateHistory,
});
const isInt = (num) => num === parseInt(`${num}`, 10);
const numIsUint = (num) => num >= 0 && num < 2 ** 32 - 1 && isInt(num);
const numIsInt = (num) => num >= -(2 ** 32 / 2) && num <= 2 ** 32 / 2 - 1 && isInt(num);
const numIsByte = (num) => num >= 0 && num <= 2 ** 8 - 1 && isInt(num);
exports.validateProtocolSettings = (settings) => {
    var _a;
    const { network, addressVersion, committeeMembersCount, validatorsCount, millisecondsPerBlock, memoryPoolMaxTransactions, maxTraceableBlocks, maxTransactionsPerBlock, } = settings;
    if (network !== undefined && !numIsUint(network)) {
        throw new errors_1.InvalidUIntError(network);
    }
    if (addressVersion !== undefined && !numIsByte(addressVersion)) {
        throw new errors_1.InvalidByteError(addressVersion);
    }
    if (committeeMembersCount !== undefined && !numIsInt(committeeMembersCount)) {
        throw new errors_1.InvalidIntError(committeeMembersCount);
    }
    if (validatorsCount !== undefined && !numIsInt(validatorsCount)) {
        throw new errors_1.InvalidIntError(validatorsCount);
    }
    if (millisecondsPerBlock !== undefined && !numIsUint(millisecondsPerBlock)) {
        throw new errors_1.InvalidUIntError(millisecondsPerBlock);
    }
    if (memoryPoolMaxTransactions !== undefined && !numIsInt(memoryPoolMaxTransactions)) {
        throw new errors_1.InvalidIntError(memoryPoolMaxTransactions);
    }
    if (maxTraceableBlocks !== undefined && !numIsUint(maxTraceableBlocks)) {
        throw new errors_1.InvalidUIntError(maxTraceableBlocks);
    }
    if (maxTransactionsPerBlock !== undefined && !numIsUint(maxTransactionsPerBlock)) {
        throw new errors_1.InvalidUIntError(maxTransactionsPerBlock);
    }
    return Object.assign(Object.assign({}, settings), { initialGasDistribution: (_a = settings.initialGasDistribution) === null || _a === void 0 ? void 0 : _a.toString() });
};

//# sourceMappingURL=utils.js.map
