"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.encodeInputType = exports.isTuple = exports.isTupleArray = exports.getDataSize = exports.isArrayType = exports.isValidAbiType = exports.DataTypeError = void 0;
const error_1 = require("../utils/error");
const wasm_1 = require("./wasm");
class DataTypeError extends error_1.RuntimeError {
}
exports.DataTypeError = DataTypeError;
function isValidAbiType(typeStr) {
    if (!typeStr) {
        return false;
    }
    return wasm_1.isValidDataType(typeStr);
}
exports.isValidAbiType = isValidAbiType;
function isArrayType(type) {
    return wasm_1.isArrayType(type);
}
exports.isArrayType = isArrayType;
function getDataSize(typeStr) {
    return wasm_1.getDataSize(typeStr);
}
exports.getDataSize = getDataSize;
exports.isTupleArray = (inputDef) => inputDef.type === 'tuple[]';
exports.isTuple = (inputDef) => inputDef.type === 'tuple' || exports.isTupleArray(inputDef);
function encodeInputType(inputDef) {
    var _a;
    if (exports.isTuple(inputDef)) {
        if (!((_a = inputDef.components) === null || _a === void 0 ? void 0 : _a.length)) {
            throw new DataTypeError('Unable to encode tuple datatype without components');
        }
        const serializedComponentTypes = inputDef.components.map(c => encodeInputType(c)).join(',');
        const serializedType = `(${serializedComponentTypes})`;
        return exports.isTupleArray(inputDef) ? `${serializedType}[]` : serializedType;
    }
    return inputDef.type;
}
exports.encodeInputType = encodeInputType;
