"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.doneCollect = void 0;
// ets_tracing: off
const CK = __importStar(require("../../../../Collections/Immutable/Chunk"));
const Tp = __importStar(require("../../../../Collections/Immutable/Tuple"));
const C = __importStar(require("../core"));
const ReadWith = __importStar(require("./readWith"));
const ZipRight = __importStar(require("./zipRight"));
function doneCollectReader(builder) {
    return ReadWith.readWith((out) => ZipRight.zipRight_(C.succeedWith(() => {
        builder.append(out);
    }), doneCollectReader(builder)), (err) => C.fail(err), (done) => C.end(done));
}
/**
 * Returns a new channel, which is the same as this one, except that all the outputs are
 * collected and bundled into a tuple together with the terminal value of this channel.
 *
 * As the channel returned from this channel collect's all of this channel's output into an in-
 * memory chunk, it is not safe to call this method on channels that output a large or unbounded
 * number of values.
 */
function doneCollect(self) {
    return C.suspend(() => {
        const builder = CK.builder();
        return C.chain_(C.pipeTo_(self, doneCollectReader(builder)), (z) => C.succeedWith(() => Tp.tuple(builder.build(), z)));
    });
}
exports.doneCollect = doneCollect;
//# sourceMappingURL=doneCollect.js.map