import { common } from '@neo-one/client-common';
import { func } from '@neo-one/edge';
import path from 'path';
import { InvalidByteError, InvalidIntError, InvalidUIntError } from './errors';
const directory = path.basename(__dirname);
const APP_ROOT = directory === 'cjs' || directory === 'esm' ? path.resolve(__dirname, '..', '..') : path.resolve(__dirname, '..');
const CSHARP_APP_ROOT = process.env.EDGE_APP_ROOT ?? path.resolve(APP_ROOT, 'lib', 'bin', 'Debug', 'netcoreapp3.0');
export const constants = {
    APP_ROOT,
    CSHARP_APP_ROOT,
};
export const 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 = func(options);
    return (input) => invokeFunction(input, true);
};
export const blockchainSettingsToProtocolSettings = (settingsIn) => ({
    network: settingsIn.network,
    addressVersion: settingsIn.addressVersion,
    standbyCommittee: settingsIn.standbyCommittee.map((ecpoint) => 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);
export const validateProtocolSettings = (settings) => {
    const { network, addressVersion, committeeMembersCount, validatorsCount, millisecondsPerBlock, memoryPoolMaxTransactions, maxTraceableBlocks, maxTransactionsPerBlock, } = settings;
    if (network !== undefined && !numIsUint(network)) {
        throw new InvalidUIntError(network);
    }
    if (addressVersion !== undefined && !numIsByte(addressVersion)) {
        throw new InvalidByteError(addressVersion);
    }
    if (committeeMembersCount !== undefined && !numIsInt(committeeMembersCount)) {
        throw new InvalidIntError(committeeMembersCount);
    }
    if (validatorsCount !== undefined && !numIsInt(validatorsCount)) {
        throw new InvalidIntError(validatorsCount);
    }
    if (millisecondsPerBlock !== undefined && !numIsUint(millisecondsPerBlock)) {
        throw new InvalidUIntError(millisecondsPerBlock);
    }
    if (memoryPoolMaxTransactions !== undefined && !numIsInt(memoryPoolMaxTransactions)) {
        throw new InvalidIntError(memoryPoolMaxTransactions);
    }
    if (maxTraceableBlocks !== undefined && !numIsUint(maxTraceableBlocks)) {
        throw new InvalidUIntError(maxTraceableBlocks);
    }
    if (maxTransactionsPerBlock !== undefined && !numIsUint(maxTransactionsPerBlock)) {
        throw new InvalidUIntError(maxTransactionsPerBlock);
    }
    return { ...settings, initialGasDistribution: settings.initialGasDistribution?.toString() };
};

//# sourceMappingURL=utils.js.map
