// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
// Needed for class-transformer json deserialisation
import 'reflect-metadata';
import { callUtilsMethod } from './bindings';
import { Block, BlockError, ClientError, PrefixHexError, SerdeJsonError, UnpackError, UTXOInput, WalletError, } from './types';
import { bigIntToHex, Utils } from './utils';
// Allow bigint to be serialized as hex string.
//
// Note:
// Serializing `bigint` to a different format, e.g. to decimal number string
// must be done manually.
BigInt.prototype.toJSON = function () {
    return bigIntToHex(this);
};
// Assign the util method on UTXOInput here to prevent loading bindings (callUtilsMethod)
// when importing UTXOInput just for typing.
Object.assign(UTXOInput, {
    /**
     * Creates a `UTXOInput` from an output id.
     */
    fromOutputId: function (outputId) {
        var input = callUtilsMethod({
            name: 'outputIdToUtxoInput',
            data: {
                outputId: outputId
            }
        });
        return new UTXOInput(input.transactionId, input.transactionOutputIndex);
    }
});
// Assign the util method on Block here to prevent loading bindings (callUtilsMethod)
// when importing Block just for typing.
Object.assign(Block, {
    /**
     * Returns the block id for the given block.
     */
    id: function (block, params) {
        return Utils.blockId(block, params);
    }
});
export * from './client';
export * from './secret_manager';
export * from './types';
export * from './utils';
export * from './wallet';
export * from './logger';
function errorHandle(error) {
    try {
        var err = JSON.parse(error.message);
        if (!err.type || !(err.type == 'panic' || err.type == 'error')) {
            return error;
        }
        // Guaranteed error
        err = err;
        if (err.type == 'panic') {
            // Panic example:
            // {"type":"panic","payload":"Client was destroyed"}
            return Error(err.payload);
        }
        else if (err.type == 'error') {
            err = err;
            if (typeof err.payload.error === 'string') {
                // Error example:
                // {"type":"error","payload":{"type":"block","error":"too many commitment inputs"}}
                switch (err.payload.type) {
                    case 'block':
                        return new BlockError(err.payload.error);
                    case 'prefixHex':
                        return new PrefixHexError(err.payload.error);
                    case 'serdeJson':
                        return new SerdeJsonError(err.payload.error);
                    case 'unpack':
                        return new UnpackError(err.payload.error);
                }
            }
            else {
                // Error example:
                // {"type":"error","payload":{"type":"client","error":{"error":"no healthy node available","type":"healthyNodePoolEmpty"}}}
                switch (err.payload.type) {
                    case 'client':
                        return new ClientError({
                            name: err.payload.error.type,
                            message: err.payload.error.error
                        });
                    case 'wallet':
                        return new WalletError({
                            name: err.payload.error.type,
                            message: err.payload.error.error
                        });
                }
            }
        }
        return Error('in ErrorHandle without a valid error object. Only call this in catch statements.');
    }
    catch (err) {
        // json error, SyntaxError, we must have send a non-json error
        return error;
    }
}
export { errorHandle };
// @ts-ignore
import { init } from '../wasm/iota_sdk_wasm';
export default init;
//# sourceMappingURL=index.js.map