const fileName_1 = "(@effect/core) _src/stream/Channel/operations/mergeWith.ts";
import * as tsplus_module_1 from "@effect/core/io/Fiber/operations/interrupt";
import * as tsplus_module_2 from "@effect/core/io/Effect/operations/zipRight";
import * as tsplus_module_3 from "@effect/core/stream/Channel/operations/fromEffect";
import * as tsplus_module_4 from "@effect/core/io/Effect/operations/succeed";
import * as tsplus_module_5 from "@effect/core/io/Exit/operations/failCause";
import * as tsplus_module_6 from "@effect/core/io/Exit/operations/succeed";
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 "@tsplus/stdlib/data/Either/fold";
import * as tsplus_module_10 from "@effect/core/io/Exit/operations/fold";
import * as tsplus_module_11 from "@effect/core/io/Fiber/operations/await";
import * as tsplus_module_12 from "@effect/core/io/Effect/operations/map";
import * as tsplus_module_13 from "@effect/core/io/Effect/operations/forkDaemon";
import * as tsplus_module_14 from "@effect/core/stream/Channel/operations/flatMap";
import * as tsplus_module_15 from "@effect/core/io/Fiber/operations/join";
import * as tsplus_module_16 from "@effect/core/stream/Channel/MergeState";
import * as tsplus_module_17 from "@effect/core/io/Effect/operations/raceWith";
import * as tsplus_module_18 from "@effect/core/stream/Channel/operations/unwrap";
import * as tsplus_module_19 from "@effect/core/io/Effect/operations/exit";
import * as tsplus_module_20 from "@effect/core/io/Effect/operations/zipWith";
import * as tsplus_module_21 from "@effect/core/stream/Channel/operations/embedInput";
import * as tsplus_module_22 from "@effect/core/stream/Channel/operations/pipeTo";
import * as tsplus_module_23 from "@effect/core/stream/Channel/operations/toPull";
import * as tsplus_module_24 from "@effect/core/stream/Channel/operations/fromInput";
import * as tsplus_module_25 from "@effect/core/stream/Channel/SingleProducerAsyncInput";
import * as tsplus_module_26 from "@effect/core/io/Effect/operations/do";
import * as tsplus_module_27 from "@effect/core/stream/Channel/operations/unwrapScoped";
import { concreteMergeDecision } from "@effect/core/stream/Channel/MergeDecision";
/**
 * Returns a new channel, which is the merge of this channel and the specified
 * channel, where the behavior of the returned channel on left or right early
 * termination is decided by the specified `leftDone` and `rightDone` merge
 * decisions.
 *
 * @tsplus fluent ets/Channel mergeWith
 */

export function mergeWith_(self, that, leftDone, rightDone) {
  const effect = tsplus_module_12.map_(tsplus_module_26.bind_(tsplus_module_26.bind_(tsplus_module_26.bindValue_(tsplus_module_26.bind_(tsplus_module_26.Do(), "input", () => tsplus_module_25.make(), fileName_1 + ":52:10"), "queueReader", ({
    input
  }) => tsplus_module_24.fromInput(input), fileName_1 + ":58:15"), "pullL", ({
    queueReader
  }) => tsplus_module_23.toPull(tsplus_module_22.pipeTo_(queueReader, () => self)), fileName_1 + ":59:10"), "pullR", ({
    queueReader
  }) => tsplus_module_23.toPull(tsplus_module_22.pipeTo_(queueReader, () => that())), fileName_1 + ":60:10"), ({
    input,
    pullL,
    pullR
  }) => {
    const handleSide = (exit, fiber, pull) => (done, both, single) => {
      const onDecision = decision => {
        void 0;

        if (decision._tag === "Done") {
          return tsplus_module_4.succeed(() => tsplus_module_3.fromEffect(() => tsplus_module_2.zipRight_(tsplus_module_1.interrupt(fiber, fileName_1 + ":133:71"), () => decision.io, fileName_1 + ":133:75")), fileName_1 + ":133:36");
        }

        return tsplus_module_12.map_(tsplus_module_11._await(fiber, fileName_1 + ":135:31"), exit => tsplus_module_10.fold_(exit, cause => tsplus_module_3.fromEffect(() => decision.f(tsplus_module_5.failCause(cause))), either => tsplus_module_9.fold_(either, done => tsplus_module_3.fromEffect(() => decision.f(tsplus_module_6.succeed(done))), elem => tsplus_module_8.zipRight_(tsplus_module_7.write(() => elem), () => go(single(decision.f))))), fileName_1 + ":135:37");
      };

      return tsplus_module_10.fold_(exit, cause => onDecision(done(tsplus_module_5.failCause(cause))), either => tsplus_module_9.fold_(either, z => onDecision(done(tsplus_module_6.succeed(z))), elem => tsplus_module_4.succeed(() => tsplus_module_8.zipRight_(tsplus_module_7.write(() => elem), () => tsplus_module_14.flatMap_(tsplus_module_3.fromEffect(() => tsplus_module_13.forkDaemon(pull, fileName_1 + ":155:57")), leftFiber => go(both(leftFiber, fiber)))), fileName_1 + ":153:33")));
    };

    const go = state => {
      switch (state._tag) {
        case "BothRunning":
          {
            const lj = tsplus_module_15.join(state.left, fileName_1 + ":178:32");
            const rj = tsplus_module_15.join(state.right, fileName_1 + ":183:33");
            return tsplus_module_18.unwrap(() => tsplus_module_17.raceWith_(lj, () => rj, (leftEx, _) => handleSide(leftEx, state.right, pullL)(leftDone, (l, r) => tsplus_module_16.bothRunning(l, r), _ => tsplus_module_16.leftDone(_)), (rightEx, _) => handleSide(rightEx, state.left, pullR)(rightDone, (l, r) => tsplus_module_16.bothRunning(r, l), _ => tsplus_module_16.rightDone(_)), fileName_1 + ":186:26"));
          }

        case "LeftDone":
          {
            return tsplus_module_18.unwrap(() => tsplus_module_12.map_(tsplus_module_19.exit(pullR, fileName_1 + ":205:25"), exit => tsplus_module_10.fold_(exit, cause => tsplus_module_3.fromEffect(() => state.f(tsplus_module_5.failCause(cause))), either => tsplus_module_9.fold_(either, done => tsplus_module_3.fromEffect(() => state.f(tsplus_module_6.succeed(done))), elem => tsplus_module_8.zipRight_(tsplus_module_7.write(() => elem), () => go(tsplus_module_16.leftDone(state.f))))), fileName_1 + ":205:31"));
          }

        case "RightDone":
          {
            return tsplus_module_18.unwrap(() => tsplus_module_12.map_(tsplus_module_19.exit(pullL, fileName_1 + ":219:25"), exit => tsplus_module_10.fold_(exit, cause => tsplus_module_3.fromEffect(() => state.f(tsplus_module_5.failCause(cause))), either => tsplus_module_9.fold_(either, done => tsplus_module_3.fromEffect(() => state.f(tsplus_module_6.succeed(done))), elem => tsplus_module_8.zipRight_(tsplus_module_7.write(() => elem), () => go(tsplus_module_16.rightDone(state.f))))), fileName_1 + ":219:31"));
          }
      }
    };

    return tsplus_module_21.embedInput_(tsplus_module_14.flatMap_(tsplus_module_3.fromEffect(() => tsplus_module_20.zipWith_(tsplus_module_13.forkDaemon(pullL, fileName_1 + ":236:22"), () => tsplus_module_13.forkDaemon(pullR, fileName_1 + ":238:29"), (left, right) => tsplus_module_16.bothRunning(left, right), fileName_1 + ":237:19")), go), input);
  }, fileName_1 + ":61:9");
  return tsplus_module_27.unwrapScoped(effect);
}
//# sourceMappingURL=mergeWith.mjs.map