"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getWasmMemorySize = exports.toChecksumAddress = exports.sha3 = exports.getDataSize = exports.isArrayType = exports.isValidDataType = exports.parseEventSignature = exports.parseFunctionSignature = exports.abiDecodeParameters = void 0;
const pkg_1 = require("../../wasm/ethabi/pkg");
const ethlogger_ethabi_bg_1 = require("../../wasm/ethabi/pkg/ethlogger_ethabi_bg");
const error_1 = require("../utils/error");
class EthAbiError extends error_1.RuntimeError {
}
function unwrapJsResult(result) {
    if (result.t === 'Ok') {
        return result.c;
    }
    else if (result.t === 'Err') {
        throw new EthAbiError(result.c);
    }
    else {
        throw new EthAbiError('Unexpected return value from wasm module');
    }
}
function abiDecodeParameters(input, types) {
    const data = new Uint8Array(Buffer.from(input, 'hex'));
    return unwrapJsResult(pkg_1.abi_decode_parameters(data, types));
}
exports.abiDecodeParameters = abiDecodeParameters;
function parseFunctionSignature(signature) {
    const result = unwrapJsResult(pkg_1.parse_function_signature(signature));
    return { type: 'function', ...result };
}
exports.parseFunctionSignature = parseFunctionSignature;
function parseEventSignature(signature) {
    const result = unwrapJsResult(pkg_1.parse_event_signature(signature));
    return { type: 'event', ...result };
}
exports.parseEventSignature = parseEventSignature;
function isValidDataType(dataType) {
    return pkg_1.is_valid_param_type(dataType);
}
exports.isValidDataType = isValidDataType;
function isArrayType(dataType) {
    return unwrapJsResult(pkg_1.is_array_type(dataType));
}
exports.isArrayType = isArrayType;
function getDataSize(dataType) {
    return unwrapJsResult(pkg_1.get_data_size(dataType));
}
exports.getDataSize = getDataSize;
function sha3(str) {
    var _a;
    return (_a = pkg_1.sha3(str)) !== null && _a !== void 0 ? _a : null;
}
exports.sha3 = sha3;
function toChecksumAddress(address) {
    return unwrapJsResult(pkg_1.to_checksum_address(address));
}
exports.toChecksumAddress = toChecksumAddress;
function getWasmMemorySize() {
    return ethlogger_ethabi_bg_1.memory.buffer.byteLength;
}
exports.getWasmMemorySize = getWasmMemorySize;
