"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.serializeEnvelope = exports.extractEnvelopeChild = void 0;
/**
 * Extracts the child envelope from a parent envelope.
 * @param deser The (de)serializer to use for deserialization (when required)
 * @param parent The envelope from which to extract the child.
 * @returns The deserialized child container as IEnvelope instance.
 */
function extractEnvelopeChild(deser, parent) {
    if (parent.child) {
        if (Buffer.isBuffer(parent.child)) {
            return deser.deserialize(parent.child);
        }
        else {
            return parent.child;
        }
    }
}
exports.extractEnvelopeChild = extractEnvelopeChild;
/**
 * Serializes a given envelope and its child envelopes (recursively).
 * @param deser The (de)serializer to use for serialization (when required)
 * @param envelope The envelope that needs to be serialized
 * @returns A buffer that contains the serialized envelope
 */
function serializeEnvelope(deser, envelope) {
    if (Buffer.isBuffer(envelope)) {
        return envelope;
    }
    const childBuf = envelope.child ? serializeEnvelope(deser, envelope.child) : undefined;
    envelope.child = childBuf;
    return deser.serialize(envelope);
}
exports.serializeEnvelope = serializeEnvelope;
