"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.decodeBytestrings = decodeBytestrings;
/* eslint-disable @typescript-eslint/no-explicit-any */
const lodash_1 = __importDefault(require("lodash"));
/**
 * Convert all objects that represent a bytestring to a node.js Buffer or a Uint8Array.
 * Objects that that represent bytestrings are objects of the form {"type": "Buffer", data: [<array of bytes>]}.
 * This is the kind of object that node.js returns when a Buffer is stringified as JSON.
 * Note that as an optimization, the conversion (potentially) happens in place.
 * @param value           An object that will be converted in place and recursively
 * @param useNodeJSBuffer If true decode to node.js Buffer, otherwise use Uint8Array
 * @returns The converted object with bytestrings replaced by Buffers or Uint8Arrays
 *
 * @remark
 * Ensure you use the return value of this function because if the input value is a bytestring,
 * it will NOT be converted in place.
 *
 */
function decodeBytestrings(value, useNodeJSBuffer) {
    if (lodash_1.default.isObject(value)) {
        if (value.type === 'Buffer' &&
            Array.isArray(value.data) &&
            value.data.every((x) => lodash_1.default.isNumber(x))) {
            if (useNodeJSBuffer) {
                return Buffer.from(value);
            }
            else {
                return new Uint8Array(value.data);
            }
        }
        else {
            for (const key in value) {
                value[key] = decodeBytestrings(value[key], useNodeJSBuffer);
            }
        }
    }
    else if (Array.isArray(value)) {
        return value.map((val) => decodeBytestrings(val, useNodeJSBuffer));
    }
    return value;
}
//# sourceMappingURL=bytestring.js.map