import { Script } from '@neo-one/client-common';
import { ArrayStackItem, BooleanStackItem, BufferStackItem, ByteStringStackItem, IntegerStackItem, InteropInterface, MapStackItem, NullStackItem, PointerStackItem, StructStackItem, } from '@neo-one/node-core';
import BN from 'bn.js';
export const parseStackItems = (stack) => stack.map(parseStackItem);
export const parseStackItem = (item) => {
    switch (item.Type) {
        case 'Any':
            return new NullStackItem();
        case 'Array':
            return new ArrayStackItem(parseStackItems(item.value));
        case 'Buffer':
            return new BufferStackItem(item.value);
        case 'InteropInterface':
            return new InteropInterface(item.value);
        case 'Map':
            return new MapStackItem(item.value.reduce((acc, { key, value }) => {
                const parsedKey = parsePrimitiveStackItem(key);
                const parsedValue = parseStackItem(value);
                acc.set(parsedKey, parsedValue);
                return acc;
            }, new Map()));
        case 'Pointer':
            return new PointerStackItem({
                script: new Script(item.value),
                position: item.Position,
            });
        case 'Struct':
            return new StructStackItem(parseStackItems(item.value));
        default:
            return parsePrimitiveStackItem(item);
    }
};
const parsePrimitiveStackItem = (item) => {
    switch (item.Type) {
        case 'Boolean':
            return new BooleanStackItem(item.value);
        case 'ByteString':
            return new ByteStringStackItem(item.value);
        case 'Integer':
            return new IntegerStackItem(new BN(item.value, 'le'));
        default:
            throw new Error(`invalid stack item when parsing, found type ${item}`);
    }
};

//# sourceMappingURL=stackItems.js.map
