"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");
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);
    }
    create({ trigger, container, gas, snapshot }) {
        return this.dispatch({
            method: 'create',
            args: {
                trigger,
                container: container ? node_core_1.serializeScriptContainer(container) : undefined,
                gas: gas.toString(),
                snapshot,
            },
        });
    }
    execute() {
        return client_common_1.VMState[this.dispatch({
            method: 'execute',
        })];
    }
    loadScript({ script, flags = node_core_1.CallFlags.All, scriptHash, initialPosition }) {
        return this.dispatch({
            method: 'loadscript',
            args: {
                script,
                flags,
                scriptHash: scriptHash ? client_common_1.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',
        });
    }
}
exports.ApplicationEngine = ApplicationEngine;

//# sourceMappingURL=ApplicationEngine.js.map
