import { CallFlags, common, TriggerType, VMState } from '@neo-one/client-common';
import { serializeScriptContainer, } from '@neo-one/node-core';
import BN from 'bn.js';
import _ from 'lodash';
import { convertLog, parseStackItems } from './converters';
import { validateProtocolSettings } from './utils';
export class ApplicationEngine {
    constructor(dispatcher) {
        this.dispatch = dispatcher.dispatch.bind(this);
    }
    get trigger() {
        return TriggerType[this.dispatch({
            method: 'gettrigger',
        })];
    }
    get gasConsumed() {
        try {
            return new BN(this.dispatch({
                method: 'getgasconsumed',
            }));
        }
        catch {
            return new BN(-1);
        }
    }
    get state() {
        return VMState[this.dispatch({
            method: 'getvmstate',
        })];
    }
    get resultStack() {
        return _.reverse(parseStackItems(this.dispatch({
            method: 'getresultstack',
        })));
    }
    get notifications() {
        return parseStackItems(this.dispatch({
            method: 'getnotifications',
        }));
    }
    get logs() {
        return this.dispatch({
            method: 'getlogs',
        }).map(convertLog);
    }
    get faultException() {
        return this.dispatch({
            method: 'getfaultexception',
        });
    }
    create({ trigger, container, persistingBlock, gas, snapshot, settings }) {
        return this.dispatch({
            method: 'create',
            args: {
                trigger,
                container: container ? serializeScriptContainer(container) : undefined,
                gas: gas.toString(),
                persistingBlock: persistingBlock === undefined ? undefined : persistingBlock.serializeWire(),
                snapshot,
                settings: validateProtocolSettings(settings),
            },
        });
    }
    execute() {
        return VMState[this.dispatch({
            method: 'execute',
        })];
    }
    loadScript({ script, rvcount, flags = CallFlags.All, scriptHash, initialPosition }) {
        return this.dispatch({
            method: 'loadscript',
            args: {
                script,
                rvcount,
                flags,
                scriptHash: scriptHash ? common.uInt160ToHex(scriptHash) : undefined,
                initialPosition,
            },
        });
    }
    push(item) {
        return this.dispatch({
            method: 'push',
            args: {
                item,
            },
        });
    }
    loadContract({ hash, method, pcount, flags }) {
        return this.dispatch({
            method: 'loadcontract',
            args: {
                hash,
                method,
                pcount,
                flags,
            },
        });
    }
    checkScript() {
        return this.dispatch({
            method: 'checkscript',
        });
    }
}

//# sourceMappingURL=ApplicationEngine.js.map
