import * as tsplus_module_1 from "@effect/core/io/Effect/operations/done";
import * as tsplus_module_2 from "@effect/core/io/Effect/operations/unsome";
import * as tsplus_module_3 from "@effect/core/io/Effect/operations/flatMap";
import * as tsplus_module_4 from "@effect/core/stream/Stream/operations/unfoldEffect";
import * as tsplus_module_5 from "@effect/core/stream/Stream/operations/_internal/Handoff";
import * as tsplus_module_6 from "@effect/core/io/Effect/operations/zipRight";
import * as tsplus_module_7 from "@effect/core/stream/Channel/operations/writeChunk";
import * as tsplus_module_8 from "@effect/core/stream/Channel/operations/concatMap";
import * as tsplus_module_9 from "@effect/core/stream/Channel/operations/pipeTo";
import * as tsplus_module_10 from "@effect/core/stream/Channel/operations/runScoped";
import * as tsplus_module_11 from "@effect/core/io/Effect/operations/fork";
import * as tsplus_module_12 from "@effect/core/io/Effect/operations/do";
import * as tsplus_module_13 from "@effect/core/io/Effect/operations/tap";
import * as tsplus_module_14 from "@effect/core/io/Effect/operations/map";
import * as tsplus_module_15 from "@effect/core/stream/Channel/operations/unwrapScoped";
import * as tsplus_module_16 from "@effect/core/stream/Channel/operations/fromEffect";
import * as tsplus_module_17 from "@effect/core/io/Exit/operations/succeed";
import * as tsplus_module_18 from "@effect/core/stream/Channel/operations/zipRight";
import * as tsplus_module_19 from "@tsplus/stdlib/data/Maybe/definition";
import * as tsplus_module_20 from "@effect/core/io/Cause/operations/map";
import * as tsplus_module_21 from "@effect/core/io/Exit/operations/failCause";
import * as tsplus_module_22 from "@effect/core/io/Exit/operations/fail";
import * as tsplus_module_23 from "@effect/core/stream/Channel/operations/readWithCause";
import { concreteStream, StreamInternal } from "@effect/core/stream/Stream/operations/_internal/StreamInternal";
/**
 * Combines the elements from this stream and the specified stream by
 * repeatedly applying the function `f` to extract an element using both sides
 * and conceptually "offer" it to the destination stream. `f` can maintain
 * some internal state to control the combining process, with the initial
 * state being specified by `s`.
 *
 * Where possible, prefer `Stream.combineChunks` for a more efficient
 * implementation.
 *
 * @tsplus fluent ets/Stream combine
 */

export function combine_(self, that, s, f, __tsplusTrace) {
  return new StreamInternal(tsplus_module_15.unwrapScoped(tsplus_module_14.map_(tsplus_module_12.bindValue_(tsplus_module_12.bindValue_(tsplus_module_13.tap_(tsplus_module_13.tap_(tsplus_module_12.bind_(tsplus_module_12.bind_(tsplus_module_12.bind_(tsplus_module_12.bind_(tsplus_module_12.Do(), "left", () => tsplus_module_5.make(__tsplusTrace), __tsplusTrace), "right", () => tsplus_module_5.make(__tsplusTrace), __tsplusTrace), "latchL", () => tsplus_module_5.make(__tsplusTrace), __tsplusTrace), "latchR", () => tsplus_module_5.make(__tsplusTrace), __tsplusTrace), ({
    latchL,
    left
  }) => {
    void 0;
    return tsplus_module_11.fork(tsplus_module_10.runScoped(tsplus_module_9.pipeTo_(tsplus_module_8.concatMap_(self.channel, chunk => tsplus_module_7.writeChunk(chunk)), () => producer(left, latchL, __tsplusTrace))), __tsplusTrace);
  }, __tsplusTrace), ({
    latchR,
    right
  }) => {
    const that0 = that();
    void 0;
    return tsplus_module_11.fork(tsplus_module_10.runScoped(tsplus_module_9.pipeTo_(tsplus_module_8.concatMap_(that0.channel, chunk => tsplus_module_7.writeChunk(chunk)), () => producer(right, latchR, __tsplusTrace))), __tsplusTrace);
  }, __tsplusTrace), "pullLeft", ({
    latchL,
    left
  }) => tsplus_module_6.zipRight_(tsplus_module_5.offer(latchL, undefined, __tsplusTrace), () => tsplus_module_3.flatMap_(tsplus_module_5.take(left, __tsplusTrace), exit => tsplus_module_1.done(() => exit, __tsplusTrace), __tsplusTrace), __tsplusTrace), __tsplusTrace), "pullRight", ({
    latchR,
    right
  }) => tsplus_module_6.zipRight_(tsplus_module_5.offer(latchR, undefined, __tsplusTrace), () => tsplus_module_3.flatMap_(tsplus_module_5.take(right, __tsplusTrace), exit => tsplus_module_1.done(() => exit, __tsplusTrace), __tsplusTrace), __tsplusTrace), __tsplusTrace), ({
    pullLeft,
    pullRight
  }) => {
    const stream = tsplus_module_4.unfoldEffect(s, s => tsplus_module_3.flatMap_(f(s, pullLeft, pullRight), exit => tsplus_module_2.unsome(tsplus_module_1.done(() => exit, __tsplusTrace), __tsplusTrace), __tsplusTrace));
    void 0;
    return stream.channel;
  }, __tsplusTrace)));
}
/**
 * Combines the elements from this stream and the specified stream by
 * repeatedly applying the function `f` to extract an element using both sides
 * and conceptually "offer" it to the destination stream. `f` can maintain
 * some internal state to control the combining process, with the initial
 * state being specified by `s`.
 *
 * Where possible, prefer `Stream.combineChunks` for a more efficient
 * implementation.
 *
 * @tsplus static ets/Stream/Aspects combine
 */

export const combine = (that, s, f, __tsplusTrace) => self => combine_(self, that, s, f, __tsplusTrace);

function producer(handoff, latch, __tsplusTrace) {
  return tsplus_module_18.zipRight_(tsplus_module_16.fromEffect(() => tsplus_module_5.take(latch, __tsplusTrace)), () => tsplus_module_23.readWithCause(value => tsplus_module_18.zipRight_(tsplus_module_16.fromEffect(() => tsplus_module_5.offer(handoff, tsplus_module_17.succeed(value), __tsplusTrace)), () => producer(handoff, latch, __tsplusTrace)), cause => tsplus_module_16.fromEffect(() => tsplus_module_5.offer(handoff, tsplus_module_21.failCause(tsplus_module_20.map_(cause, tsplus_module_19.some)), __tsplusTrace)), () => tsplus_module_18.zipRight_(tsplus_module_16.fromEffect(() => tsplus_module_5.offer(handoff, tsplus_module_22.fail(tsplus_module_19.none), __tsplusTrace)), () => producer(handoff, latch, __tsplusTrace))));
}
//# sourceMappingURL=combine.mjs.map