import * as tsplus_module_1 from "@tsplus/stdlib/collections/Chunk/takeWhile";
import * as tsplus_module_2 from "@tsplus/stdlib/collections/Chunk/drop";
import * as tsplus_module_3 from "@tsplus/stdlib/collections/Chunk/take";
import * as tsplus_module_4 from "@tsplus/stdlib/collections/Chunk/isEmpty";
import * as tsplus_module_5 from "@effect/core/stream/Channel/operations/write";
import * as tsplus_module_6 from "@effect/core/stream/Channel/operations/zipRight";
import * as tsplus_module_7 from "@tsplus/stdlib/collections/Chunk/concat";
import * as tsplus_module_8 from "@effect/core/stream/Channel/operations/fail";
import * as tsplus_module_9 from "@effect/core/stream/Channel/operations/succeed";
import * as tsplus_module_10 from "@effect/core/stream/Channel/operations/readWith";
import * as tsplus_module_11 from "@effect/core/stream/Channel/operations/pipeTo";
import { concreteStream, StreamInternal } from "@effect/core/stream/Stream/operations/_internal/StreamInternal";
/**
 * Takes all elements of the stream until the specified predicate evaluates to
 * `true`.
 *
 * @tsplus fluent ets/Stream takeUntil
 */

export function takeUntil_(self, f, __tsplusTrace) {
  const loop = tsplus_module_10.readWith(chunk => {
    const taken = tsplus_module_1.takeWhile_(chunk, a => !f(a));
    const last = tsplus_module_3.take_(tsplus_module_2.drop_(chunk, taken.length), 1);
    return tsplus_module_4.isEmpty(last) ? tsplus_module_6.zipRight_(tsplus_module_5.write(() => taken), () => loop) : tsplus_module_5.write(() => tsplus_module_7.concat_(taken, last));
  }, err => tsplus_module_8.fail(() => err), done => tsplus_module_9.succeed(() => done));
  void 0;
  return new StreamInternal(tsplus_module_11.pipeTo_(self.channel, () => loop));
}
/**
 * Takes all elements of the stream until the specified predicate evaluates to
 * `true`.
 *
 * @tsplus static ets/Stream/Aspects takeUntil
 */

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