"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseStackItem = exports.parseStackItems = void 0;
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");
exports.parseStackItems = (stack) => stack.map(exports.parseStackItem);
exports.parseStackItem = (item) => {
    switch (item.Type) {
        case 'Any':
            return new node_core_1.NullStackItem();
        case 'Array':
            return new node_core_1.ArrayStackItem(exports.parseStackItems(item.value));
        case 'Buffer':
            return new node_core_1.BufferStackItem(item.value);
        case 'InteropInterface':
            return new node_core_1.InteropInterface(item.value);
        case 'Map':
            return new node_core_1.MapStackItem(item.value.reduce((acc, { key, value }) => {
                const parsedKey = parsePrimitiveStackItem(key);
                const parsedValue = exports.parseStackItem(value);
                acc.set(parsedKey, parsedValue);
                return acc;
            }, new Map()));
        case 'Pointer':
            return new node_core_1.PointerStackItem({
                script: new client_common_1.Script(item.value),
                position: item.Position,
            });
        case 'Struct':
            return new node_core_1.StructStackItem(exports.parseStackItems(item.value));
        default:
            return parsePrimitiveStackItem(item);
    }
};
const parsePrimitiveStackItem = (item) => {
    switch (item.Type) {
        case 'Boolean':
            return new node_core_1.BooleanStackItem(item.value);
        case 'ByteString':
            return new node_core_1.ByteStringStackItem(item.value);
        case 'Integer':
            return new node_core_1.IntegerStackItem(new bn_js_1.BN(item.value, 'le'));
        default:
            throw new Error(`invalid stack item when parsing, found type ${item}`);
    }
};

//# sourceMappingURL=stackItems.js.map
