const fileName_1 = "(@effect/core) _src/stream/Stream/operations/takeUntilEffect.ts";
import * as tsplus_module_1 from "@tsplus/stdlib/collections/Chunk/empty";
import * as tsplus_module_2 from "@effect/core/stream/Channel/operations/pipeTo";
import * as tsplus_module_3 from "@effect/core/stream/Channel/operations/failCause";
import * as tsplus_module_4 from "@effect/core/stream/Channel/operations/succeed";
import * as tsplus_module_5 from "@effect/core/stream/Channel/operations/readWithCause";
import * as tsplus_module_6 from "@tsplus/stdlib/collections/Chunk/single";
import * as tsplus_module_7 from "@effect/core/stream/Channel/operations/write";
import * as tsplus_module_8 from "@effect/core/stream/Channel/operations/zipRight";
import * as tsplus_module_9 from "@effect/core/io/Effect/operations/map";
import * as tsplus_module_10 from "@effect/core/stream/Channel/operations/unwrap";
import { concreteStream, StreamInternal } from "@effect/core/stream/Stream/operations/_internal/StreamInternal";
/**
 * Takes all elements of the stream until the specified effectual predicate
 * evaluates to `true`.
 *
 * @tsplus fluent ets/Stream takeUntilEffect
 */

export function takeUntilEffect_(self, f, __tsplusTrace) {
  void 0;
  return new StreamInternal(tsplus_module_2.pipeTo_(self.channel, () => loop(tsplus_module_1.empty()[Symbol.iterator](), f)));
}
/**
 * Takes all elements of the stream until the specified effectual predicate
 * evaluates to `true`.
 *
 * @tsplus static ets/Stream/Aspects takeUntilEffect
 */

export const takeUntilEffect = (f, __tsplusTrace) => self => takeUntilEffect_(self, f, __tsplusTrace);

function loop(chunkIterator, f) {
  const next = chunkIterator.next();

  if (next.done) {
    return tsplus_module_5.readWithCause(elem => loop(elem[Symbol.iterator](), f), err => tsplus_module_3.failCause(() => err), done => tsplus_module_4.succeed(() => done));
  } else {
    return tsplus_module_10.unwrap(() => tsplus_module_9.map_(f(next.value), b => b ? tsplus_module_7.write(() => tsplus_module_6.single(next.value)) : tsplus_module_8.zipRight_(tsplus_module_7.write(() => tsplus_module_6.single(next.value)), () => loop(chunkIterator, f)), fileName_1 + ":41:24"));
  }
}
//# sourceMappingURL=takeUntilEffect.mjs.map