"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ByteArrayEncoding = void 0;
exports.convertPartialTransactionOptionsToFull = convertPartialTransactionOptionsToFull;
exports.stripHexPrefix = stripHexPrefix;
exports.bigIntToBytes = bigIntToBytes;
exports.encodeJson = encodeJson;
const encoding_1 = require("@cosmjs/encoding");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const long_1 = __importDefault(require("long"));
const constants_1 = require("./constants");
/**
 * @description Either return undefined or insert default sequence value into
 * `partialTransactionOptions` if it does not exist.
 *
 * @returns undefined or full TransactionOptions.
 */
function convertPartialTransactionOptionsToFull(partialTransactionOptions) {
    if (partialTransactionOptions === undefined) {
        return undefined;
    }
    return {
        sequence: constants_1.DEFAULT_SEQUENCE,
        ...partialTransactionOptions,
    };
}
/**
 * @description Strip '0x' prefix from input string. If there is no '0x' prefix, return the original
 * input.
 *
 * @returns input without '0x' prefix or original input if no prefix.
 */
function stripHexPrefix(input) {
    if (input.indexOf('0x') === 0) {
        return input.slice(2);
    }
    return input;
}
function toBigInt(u) {
    if (u.length <= 1) {
        return BigInt(0);
    }
    // eslint-disable-next-line no-bitwise
    const negated = (u[0] & 1) === 1;
    const hex = Buffer.from(u.slice(1)).toString('hex');
    const abs = BigInt(`0x${hex}`);
    return negated ? -abs : abs;
}
function bigIntToBytes(value) {
    const absoluteValue = value < 0 ? value * BigInt(-1) : value;
    const nonPaddedHexValue = absoluteValue.toString(16);
    const paddedHexValue = nonPaddedHexValue.length % 2 === 0 ? nonPaddedHexValue : `0${nonPaddedHexValue}`;
    const numberBytes = Buffer.from(paddedHexValue, 'hex');
    const signedBytes = Uint8Array.of(value < 0 ? 3 : 2, ...numberBytes);
    return signedBytes;
}
var ByteArrayEncoding;
(function (ByteArrayEncoding) {
    ByteArrayEncoding["HEX"] = "hex";
    ByteArrayEncoding["BIGINT"] = "bigint";
})(ByteArrayEncoding || (exports.ByteArrayEncoding = ByteArrayEncoding = {}));
function encodeJson(object, byteArrayEncoding = ByteArrayEncoding.HEX) {
    // eslint-disable-next-line prefer-arrow-callback
    return JSON.stringify(object, function replacer(_key, value) {
        // Even though we set the an UInt8Array as the value,
        // it comes in here as an object with UInt8Array as the buffer property.
        if (value instanceof bignumber_js_1.default) {
            return value.toString();
        }
        if (typeof value === 'bigint') {
            return value.toString();
        }
        if (value instanceof long_1.default) {
            return value.toString();
        }
        if ((value === null || value === void 0 ? void 0 : value.buffer) instanceof Uint8Array) {
            if (byteArrayEncoding === ByteArrayEncoding.HEX) {
                return (0, encoding_1.toHex)(value.buffer);
            }
            else {
                return toBigInt(value.buffer).toString();
            }
        }
        else if (value instanceof Uint8Array) {
            if (byteArrayEncoding === ByteArrayEncoding.HEX) {
                return (0, encoding_1.toHex)(value);
            }
            else {
                return toBigInt(value).toString();
            }
        }
        return value;
    });
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaGVscGVycy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uL3NyYy9saWIvaGVscGVycy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7QUFhQSx3RkFXQztBQVFELHdDQU1DO0FBYUQsc0NBUUM7QUFPRCxnQ0FnQ0M7QUFsR0QsK0NBQXlDO0FBQ3pDLGdFQUFxQztBQUNyQyxnREFBd0I7QUFHeEIsMkNBQStDO0FBRS9DOzs7OztHQUtHO0FBQ0gsU0FBZ0Isc0NBQXNDLENBQ3BELHlCQUFxRDtJQUVyRCxJQUFJLHlCQUF5QixLQUFLLFNBQVMsRUFBRSxDQUFDO1FBQzVDLE9BQU8sU0FBUyxDQUFDO0lBQ25CLENBQUM7SUFFRCxPQUFPO1FBQ0wsUUFBUSxFQUFFLDRCQUFnQjtRQUMxQixHQUFHLHlCQUF5QjtLQUM3QixDQUFDO0FBQ0osQ0FBQ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