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) => ({
    magic: settingsIn.messageMagic,
    addressVersion: settingsIn.addressVersion,
    standbyCommittee: settingsIn.standbyCommittee.map((ecpoint) => common.ecPointToString(ecpoint)),
    committeeMembersCount: settingsIn.standbyCommittee.length,
    validatorsCount: settingsIn.validatorsCount,
    millisecondsPerBlock: settingsIn.millisecondsPerBlock,
    memoryPoolMaxTransactions: settingsIn.memoryPoolMaxTransactions,
});
const numIsUint = (num) => num >= 0 && num < 2 ** 32 - 1;
const numIsInt = (num) => num >= -(2 ** 32 / 2) && num <= 2 ** 32 / 2 - 1;
const numIsByte = (num) => num >= 0 && num <= 2 ** 8 - 1;
export const validateProtocolSettings = (settings) => {
    const { magic, addressVersion, committeeMembersCount, validatorsCount, millisecondsPerBlock, memoryPoolMaxTransactions, } = settings;
    if (magic !== undefined && !numIsUint(magic)) {
        throw new InvalidUIntError(magic);
    }
    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);
    }
    return settings;
};

//# sourceMappingURL=utils.js.map
