"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApplicationEngine = void 0;
const tslib_1 = require("tslib");
const client_common_1 = require("@neo-one/client-common");
const node_core_1 = require("@neo-one/node-core");
const bn_js_1 = require("bn.js");
const lodash_1 = tslib_1.__importDefault(require("lodash"));
const converters_1 = require("./converters");
const utils_1 = require("./utils");
class ApplicationEngine {
    constructor(dispatcher) {
        this.dispatch = dispatcher.dispatch.bind(this);
    }
    get trigger() {
        return client_common_1.TriggerType[this.dispatch({
            method: 'gettrigger',
        })];
    }
    get gasConsumed() {
        try {
            return new bn_js_1.BN(this.dispatch({
                method: 'getgasconsumed',
            }));
        }
        catch (_a) {
            return new bn_js_1.BN(-1);
        }
    }
    get state() {
        return client_common_1.VMState[this.dispatch({
            method: 'getvmstate',
        })];
    }
    get resultStack() {
        return lodash_1.default.reverse(converters_1.parseStackItems(this.dispatch({
            method: 'getresultstack',
        })));
    }
    get notifications() {
        return converters_1.parseStackItems(this.dispatch({
            method: 'getnotifications',
        }));
    }
    get logs() {
        return this.dispatch({
            method: 'getlogs',
        }).map(converters_1.convertLog);
    }
    get faultException() {
        return this.dispatch({
            method: 'getfaultexception',
        });
    }
    create({ trigger, container, persistingBlock, gas, snapshot, settings }) {
        return this.dispatch({
            method: 'create',
            args: {
                trigger,
                container: container ? node_core_1.serializeScriptContainer(container) : undefined,
                gas: gas.toString(),
                persistingBlock: persistingBlock === undefined ? undefined : persistingBlock.serializeWire(),
                snapshot,
                settings: utils_1.validateProtocolSettings(settings),
            },
        });
    }
    execute() {
        return client_common_1.VMState[this.dispatch({
            method: 'execute',
        })];
    }
    loadScript({ script, rvcount, flags = client_common_1.CallFlags.All, scriptHash, initialPosition }) {
        return this.dispatch({
            method: 'loadscript',
            args: {
                script,
                rvcount,
                flags,
                scriptHash: scriptHash ? client_common_1.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',
        });
    }
}
exports.ApplicationEngine = ApplicationEngine;

//# sourceMappingURL=ApplicationEngine.js.map
