import * as tsplus_module_1 from "@tsplus/stdlib/collections/Chunk/builder";
import * as tsplus_module_2 from "@tsplus/stdlib/data/Tuple/make";
import * as tsplus_module_3 from "@effect/core/stream/Channel/operations/succeed";
import * as tsplus_module_4 from "@effect/core/stream/Channel/operations/pipeTo";
import * as tsplus_module_5 from "@effect/core/stream/Channel/operations/flatMap";
import * as tsplus_module_6 from "@effect/core/stream/Channel/operations/suspend";
import * as tsplus_module_7 from "@effect/core/stream/Channel/operations/zipRight";
import * as tsplus_module_8 from "@effect/core/stream/Channel/operations/fail";
import * as tsplus_module_9 from "@effect/core/stream/Channel/operations/succeedNow";
import * as tsplus_module_10 from "@effect/core/stream/Channel/operations/readWith";
/**
 * 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 collects 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.
 *
 * @tsplus getter ets/Channel doneCollect
 */

export function doneCollect(self) {
  return tsplus_module_6.suspend(() => {
    const builder = tsplus_module_1.builder();
    return tsplus_module_5.flatMap_(tsplus_module_4.pipeTo_(self, () => reader(builder)), z => tsplus_module_3.succeed(() => tsplus_module_2.make(builder.build(), z)));
  });
}

function reader(builder) {
  return tsplus_module_10.readWith(outElem => tsplus_module_7.zipRight_(tsplus_module_3.succeed(() => {
    builder.append(outElem);
  }), () => reader(builder)), outErr => tsplus_module_8.fail(() => outErr), outDone => tsplus_module_9.succeedNow(outDone));
}
//# sourceMappingURL=doneCollect.mjs.map