import * as tsplus_module_1 from "@effect/core/stream/Channel/operations/write";
import * as tsplus_module_2 from "@effect/core/stream/Channel/operations/zipRight";
import * as tsplus_module_3 from "@effect/core/stream/Channel/operations/fail";
import * as tsplus_module_4 from "@effect/core/stream/Channel/operations/fromEffect";
import * as tsplus_module_5 from "@effect/core/stream/Channel/operations/as";
import * as tsplus_module_6 from "@effect/core/stream/Channel/operations/readWith";
import * as tsplus_module_7 from "@tsplus/stdlib/collections/Chunk/flatten";
import * as tsplus_module_8 from "@tsplus/stdlib/data/Maybe/definition";
import * as tsplus_module_9 from "@effect/core/stream/Channel/operations/flatMap";
import * as tsplus_module_10 from "@effect/core/stream/Channel/operations/doneCollect";
import * as tsplus_module_11 from "@effect/core/stream/Channel/operations/foldChannel";
import * as tsplus_module_12 from "@effect/core/stream/Channel/operations/bufferChunk";
import * as tsplus_module_13 from "@effect/core/stream/Channel/operations/pipeTo";
import * as tsplus_module_14 from "@effect/core/io/Ref/operations/make";
import * as tsplus_module_15 from "@tsplus/stdlib/collections/Chunk/empty";
import * as tsplus_module_16 from "@effect/core/io/Effect/operations/zip";
import { concreteSink, SinkInternal } from "@effect/core/stream/Sink/operations/_internal/SinkInternal";
/**
 * Creates a sink that produces values until one verifies the predicate `f`.
 *
 * @tsplus fluent ets/Sink untilOutputEffect
 */

export function untilOutputEffect_(self, f, __tsplusTrace) {
  void 0;
  return new SinkInternal(tsplus_module_9.flatMap_(tsplus_module_4.fromEffect(() => tsplus_module_16.zip_(tsplus_module_14.makeRef(() => tsplus_module_15.empty(), __tsplusTrace), () => tsplus_module_14.makeRef(() => false, __tsplusTrace), __tsplusTrace)), ({
    tuple: [leftoversRef, upstreamDoneRef]
  }) => {
    const upstreamMarker = tsplus_module_6.readWith(chunk => tsplus_module_2.zipRight_(tsplus_module_1.write(() => chunk), () => upstreamMarker), err => tsplus_module_3.fail(() => err), done => tsplus_module_5.as_(tsplus_module_4.fromEffect(() => upstreamDoneRef.set(true, __tsplusTrace)), () => done));
    const loop = tsplus_module_11.foldChannel_(tsplus_module_10.doneCollect(self.channel), err => tsplus_module_3.fail(() => err), ({
      tuple: [leftovers, doneValue]
    }) => tsplus_module_9.flatMap_(tsplus_module_4.fromEffect(() => f(doneValue)), satisfied => tsplus_module_2.zipRight_(tsplus_module_4.fromEffect(() => leftoversRef.set(tsplus_module_7.flatten(leftovers), __tsplusTrace)), () => tsplus_module_9.flatMap_(tsplus_module_4.fromEffect(() => upstreamDoneRef.get(__tsplusTrace)), upstreamDone => satisfied ? tsplus_module_5.as_(tsplus_module_1.write(() => tsplus_module_7.flatten(leftovers)), () => tsplus_module_8.some(doneValue)) : upstreamDone ? tsplus_module_5.as_(tsplus_module_1.write(() => tsplus_module_7.flatten(leftovers)), () => tsplus_module_8.none) : loop))));
    return tsplus_module_13.pipeTo_(tsplus_module_13.pipeTo_(upstreamMarker, () => tsplus_module_12.bufferChunk(leftoversRef)), () => loop);
  }));
}
/**
 * Creates a sink that produces values until one verifies the predicate `f`.
 *
 * @tsplus static ets/Sink/Aspects untilOutputEffect
 */

export const untilOutputEffect = (f, __tsplusTrace) => self => untilOutputEffect_(self, f, __tsplusTrace);
//# sourceMappingURL=untilOutputEffect.mjs.map