import { common, TriggerType, VMState } from '@neo-one/client-common';
import { CallFlags, serializeScriptContainer, } from '@neo-one/node-core';
import BN from 'bn.js';
import _ from 'lodash';
import { convertLog, parseStackItems } from './converters';
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);
    }
    create({ trigger, container, gas, snapshot }) {
        return this.dispatch({
            method: 'create',
            args: {
                trigger,
                container: container ? serializeScriptContainer(container) : undefined,
                gas: gas.toString(),
                snapshot,
            },
        });
    }
    execute() {
        return VMState[this.dispatch({
            method: 'execute',
        })];
    }
    loadScript({ script, flags = CallFlags.All, scriptHash, initialPosition }) {
        return this.dispatch({
            method: 'loadscript',
            args: {
                script,
                flags,
                scriptHash: scriptHash ? common.uInt160ToHex(scriptHash) : undefined,
                initialPosition,
            },
        });
    }
    loadContract({ hash, method, flags, packParameters = false }) {
        return this.dispatch({
            method: 'loadcontract',
            args: {
                hash,
                method,
                flags,
                packParameters,
            },
        });
    }
    checkScript() {
        return this.dispatch({
            method: 'checkscript',
        });
    }
}

//# sourceMappingURL=ApplicationEngine.js.map
