import * as tsplus_module_1 from "@effect/core/stream/Channel/operations/pipeTo";
import * as tsplus_module_2 from "@effect/core/io/Effect/operations/forkScoped";
import * as tsplus_module_3 from "@effect/core/io/Effect/operations/map";
import * as tsplus_module_4 from "@effect/core/stream/Channel/operations/unwrapScoped";
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 "@effect/core/stream/Channel/operations/fail";
import * as tsplus_module_8 from "@effect/core/stream/Channel/operations/unit";
import * as tsplus_module_9 from "@effect/core/stream/Channel/operations/readWith";
import * as tsplus_module_10 from "@effect/core/stream/Channel/operations/failCause";
import * as tsplus_module_11 from "@effect/core/io/Exit/operations/fold";
import * as tsplus_module_12 from "@tsplus/stdlib/data/Maybe/fold";
import * as tsplus_module_13 from "@effect/core/io/Fiber/operations/poll";
import * as tsplus_module_14 from "@effect/core/stream/Channel/operations/unwrap";
import { concreteStream, StreamInternal } from "@effect/core/stream/Stream/operations/_internal/StreamInternal";
/**
 * Halts the evaluation of this stream when the provided IO completes. The
 * given IO will be forked as part of the returned stream, and its success
 * will be discarded.
 *
 * An element in the process of being pulled will not be interrupted when the
 * IO completes. See `interruptWhen` for this behavior.
 *
 * If the IO completes with a failure, the stream will emit that failure.
 *
 * @tsplus fluent ets/Stream haltWhen
 */

export function haltWhen_(self, io, __tsplusTrace) {
  void 0;
  return new StreamInternal(tsplus_module_4.unwrapScoped(tsplus_module_3.map_(tsplus_module_2.forkScoped(io(), __tsplusTrace), fiber => tsplus_module_1.pipeTo_(self.channel, () => writer(fiber, __tsplusTrace)), __tsplusTrace)));
}
/**
 * Halts the evaluation of this stream when the provided IO completes. The
 * given IO will be forked as part of the returned stream, and its success
 * will be discarded.
 *
 * An element in the process of being pulled will not be interrupted when the
 * IO completes. See `interruptWhen` for this behavior.
 *
 * If the IO completes with a failure, the stream will emit that failure.
 *
 * @tsplus static ets/Stream/Aspects haltWhen
 */

export const haltWhen = (io, __tsplusTrace) => self => haltWhen_(self, io, __tsplusTrace);

function writer(fiber, __tsplusTrace) {
  return tsplus_module_14.unwrap(() => tsplus_module_3.map_(tsplus_module_13.poll(fiber, __tsplusTrace), option => tsplus_module_12.fold_(option, () => tsplus_module_9.readWith(input => tsplus_module_6.zipRight_(tsplus_module_5.write(() => input), () => writer(fiber, __tsplusTrace)), err => tsplus_module_7.fail(() => err), () => tsplus_module_8.unit), exit => tsplus_module_11.fold_(exit, cause => tsplus_module_10.failCause(() => cause), () => tsplus_module_8.unit)), __tsplusTrace));
}
//# sourceMappingURL=haltWhen.mjs.map