const fileName_1 = "(@effect/core) _src/stream/Channel/operations/mergeAllWith.ts";
import * as tsplus_module_1 from "@effect/core/stream/Channel/MergeStrategy";
import * as tsplus_module_2 from "@effect/core/stream/Channel/operations/failCause";
import * as tsplus_module_3 from "@effect/core/stream/Channel/operations/succeedNow";
import * as tsplus_module_4 from "@effect/core/stream/Channel/operations/write";
import * as tsplus_module_5 from "@effect/core/stream/Channel/operations/zipRight";
import * as tsplus_module_6 from "@tsplus/stdlib/data/Either/fold";
import * as tsplus_module_7 from "@effect/core/io/Effect/operations/flatten";
import * as tsplus_module_8 from "@effect/core/io/Effect/operations/foldCause";
import * as tsplus_module_9 from "@effect/core/stream/Channel/operations/unwrap";
import * as tsplus_module_10 from "@tsplus/stdlib/prelude/Equivalence/boolean";
import * as tsplus_module_11 from "@effect/core/io/Fiber/operations/interruptAll";
import * as tsplus_module_12 from "@effect/core/io/Effect/operations/flatMap";
import * as tsplus_module_13 from "@effect/core/io/Effect/operations/failCause";
import * as tsplus_module_14 from "@effect/core/io/Effect/operations/as";
import * as tsplus_module_15 from "@effect/core/io/Effect/operations/zipRight";
import * as tsplus_module_16 from "@effect/core/io/Effect/operations/unit";
import * as tsplus_module_17 from "@effect/core/io/Semaphore/operations/withPermits";
import * as tsplus_module_18 from "@effect/core/io/Fiber/operations/interrupt";
import * as tsplus_module_19 from "@tsplus/stdlib/data/Either/definition";
import * as tsplus_module_20 from "@effect/core/io/Effect/operations/succeed";
import * as tsplus_module_21 from "@tsplus/stdlib/data/Maybe/fold";
import * as tsplus_module_22 from "@effect/core/io/Effect/operations/raceWith";
import * as tsplus_module_23 from "@effect/core/io/Semaphore/operations/withPermit";
import * as tsplus_module_24 from "@effect/core/io/Effect/operations/fork";
import * as tsplus_module_25 from "@effect/core/io/Effect/operations/race";
import * as tsplus_module_26 from "@effect/core/stream/Channel/operations/toPull";
import * as tsplus_module_27 from "@effect/core/io/Effect/operations/scoped";
import * as tsplus_module_28 from "@effect/core/io/Deferred/operations/make";
import * as tsplus_module_29 from "@effect/core/io/Effect/operations/do";
import * as tsplus_module_30 from "@effect/core/io/Effect/operations/tap";
import * as tsplus_module_31 from "@effect/core/io/Effect/operations/map";
import * as tsplus_module_32 from "@effect/core/io/Effect/operations/when";
import * as tsplus_module_33 from "@effect/core/io/Effect/operations/foldCauseEffect";
import * as tsplus_module_34 from "@effect/core/io/Effect/operations/repeatWhileEquals";
import * as tsplus_module_35 from "@effect/core/io/Effect/operations/forkScoped";
import * as tsplus_module_36 from "@tsplus/stdlib/data/Maybe/definition";
import * as tsplus_module_37 from "@effect/core/io/Effect/operations/succeedNow";
import * as tsplus_module_38 from "@effect/core/io/Effect/operations/repeatUntil";
import * as tsplus_module_39 from "@effect/core/io/Effect/operations/catchAllCause";
import * as tsplus_module_40 from "@effect/core/io/Semaphore/operations/make";
import * as tsplus_module_41 from "@effect/core/io/Ref/operations/make";
import * as tsplus_module_42 from "@effect/core/io/Queue/operations/unbounded";
import * as tsplus_module_43 from "@effect/core/io/Effect/operations/acquireRelease";
import * as tsplus_module_44 from "@effect/core/io/Queue/external/bounded";
import * as tsplus_module_45 from "@effect/core/io/Effect/operations/addFinalizer";
import * as tsplus_module_46 from "@effect/core/io/Effect/operations/withChildren";
import * as tsplus_module_47 from "@effect/core/stream/Channel/operations/unwrapScoped";
/**
 * @tsplus static ets/Channel/Ops mergeAllWith
 */

export function mergeAllWith_(channels, n, f, bufferSize = 16, mergeStrategy = tsplus_module_1.BackPressure) {
  return tsplus_module_47.unwrapScoped(tsplus_module_31.map_(tsplus_module_46.withChildren(getChildren => tsplus_module_31.map_(tsplus_module_30.tap_(tsplus_module_29.bindValue_(tsplus_module_29.bind_(tsplus_module_29.bind_(tsplus_module_29.bind_(tsplus_module_29.bind_(tsplus_module_29.bind_(tsplus_module_29.bind_(tsplus_module_30.tap_(tsplus_module_29.Do(), () => tsplus_module_45.addFinalizer(() => tsplus_module_12.flatMap_(getChildren, chunk => tsplus_module_11.interruptAll(chunk, fileName_1 + ":45:89"), fileName_1 + ":45:59"), fileName_1 + ":45:39"), fileName_1 + ":45:13"), "queue", () => tsplus_module_43.acquireRelease(() => tsplus_module_44.bounded(bufferSize, fileName_1 + ":48:26"), queue => queue.shutdown, fileName_1 + ":47:32"), fileName_1 + ":46:14"), "cancelers", () => tsplus_module_43.acquireRelease(() => tsplus_module_42.unbounded(fileName_1 + ":55:28"), queue => queue.shutdown, fileName_1 + ":54:32"), fileName_1 + ":53:14"), "lastDone", () => tsplus_module_41.makeRef(() => tsplus_module_36.none, fileName_1 + ":58:41"), fileName_1 + ":58:14"), "errorSignal", () => tsplus_module_28.make(fileName_1 + ":59:49"), fileName_1 + ":59:14"), "permits", () => tsplus_module_40.make(n, fileName_1 + ":60:46"), fileName_1 + ":60:14"), "pull", () => tsplus_module_26.toPull(channels), fileName_1 + ":61:14"), "evaluatePull", ({
    errorSignal,
    lastDone,
    queue
  }) => pull => tsplus_module_39.catchAllCause_(tsplus_module_12.flatMap_(tsplus_module_38.repeatUntil_(tsplus_module_12.flatMap_(pull, either => tsplus_module_6.fold_(either, done => tsplus_module_20.succeed(() => tsplus_module_36.some(done), fileName_1 + ":69:45"), out => tsplus_module_14.as_(queue.offer(tsplus_module_37.succeedNow(tsplus_module_19.right(out), fileName_1 + ":70:59"), fileName_1 + ":70:41"), () => tsplus_module_36.none, fileName_1 + ":70:82")), fileName_1 + ":67:25"), option => tsplus_module_36.isSome(option), fileName_1 + ":73:29"), option => tsplus_module_21.fold_(option, () => tsplus_module_16.unit, outDone => lastDone.update(_ => tsplus_module_21.fold_(_, () => tsplus_module_36.some(outDone), lastDone => tsplus_module_36.some(f(lastDone, outDone))), fileName_1 + ":76:36")), fileName_1 + ":74:25"), cause => tsplus_module_15.zipRight_(queue.offer(tsplus_module_13.failCause(() => cause, fileName_1 + ":80:49"), fileName_1 + ":80:32"), () => tsplus_module_16.unit_(errorSignal.succeed(() => undefined, fileName_1 + ":81:42"), fileName_1 + ":81:58"), fileName_1 + ":80:59"), fileName_1 + ":78:31"), fileName_1 + ":62:19"), ({
    cancelers,
    errorSignal,
    evaluatePull,
    lastDone,
    permits,
    pull,
    queue
  }) => tsplus_module_35.forkScoped(tsplus_module_34.repeatWhileEquals_(tsplus_module_33.foldCauseEffect_(pull, cause => tsplus_module_15.zipRight_(tsplus_module_12.flatMap_(getChildren, fibers => tsplus_module_11.interruptAll(fibers, fileName_1 + ":90:39"), fileName_1 + ":89:38"), () => tsplus_module_14.as_(queue.offer(tsplus_module_13.failCause(() => cause, fileName_1 + ":92:49"), fileName_1 + ":92:32"), () => false, fileName_1 + ":92:60"), fileName_1 + ":91:22"), either => tsplus_module_6.fold_(either, outDone => tsplus_module_22.raceWith_(errorSignal.await(fileName_1 + ":96:40"), () => tsplus_module_17.withPermits_(permits, n, fileName_1 + ":97:44")(tsplus_module_16.unit), (_, permitAcquisition) => tsplus_module_15.zipRight_(tsplus_module_12.flatMap_(getChildren, fibers => tsplus_module_11.interruptAll(fibers, fileName_1 + ":99:77"), fileName_1 + ":99:46"), () => tsplus_module_14.as_(tsplus_module_18.interrupt(permitAcquisition, fileName_1 + ":100:56"), () => false, fileName_1 + ":100:61"), fileName_1 + ":99:88"), (_, failureAwait) => tsplus_module_15.zipRight_(tsplus_module_18.interrupt(failureAwait, fileName_1 + ":102:49"), () => tsplus_module_12.flatMap_(lastDone.get(fileName_1 + ":104:35"), option => tsplus_module_21.fold_(option, () => queue.offer(tsplus_module_20.succeed(() => tsplus_module_19.left(outDone), fileName_1 + ":107:61"), fileName_1 + ":107:46"), lastDone => queue.offer(tsplus_module_20.succeed(() => tsplus_module_19.left(f(lastDone, outDone)), fileName_1 + ":110:53"), fileName_1 + ":109:48")), fileName_1 + ":105:39"), fileName_1 + ":102:53"), fileName_1 + ":96:51"), channel => {
    switch (mergeStrategy._tag) {
      case "BackPressure":
        {
          return tsplus_module_31.map_(tsplus_module_29.bind_(tsplus_module_30.tap_(tsplus_module_30.tap_(tsplus_module_29.bindValue_(tsplus_module_29.bind_(tsplus_module_29.Do(), "latch", () => tsplus_module_28.make(fileName_1 + ":119:63"), fileName_1 + ":119:34"), "raceIOs", () => tsplus_module_27.scoped(() => tsplus_module_12.flatMap_(tsplus_module_26.toPull(channel), pull => tsplus_module_25.race_(evaluatePull(pull), () => errorSignal.await(fileName_1 + ":124:95"), fileName_1 + ":124:77"), fileName_1 + ":124:43"), fileName_1 + ":121:44"), fileName_1 + ":120:39"), ({
            latch,
            raceIOs
          }) => tsplus_module_24.fork(tsplus_module_23.withPermit(permits, fileName_1 + ":127:38")(tsplus_module_15.zipRight_(latch.succeed(() => undefined, fileName_1 + ":128:58"), () => raceIOs, fileName_1 + ":128:71")), fileName_1 + ":129:38"), fileName_1 + ":126:33"), ({
            latch
          }) => latch.await(fileName_1 + ":131:60"), fileName_1 + ":131:33"), "errored", () => errorSignal.isDone(fileName_1 + ":132:70"), fileName_1 + ":132:34"), ({
            errored
          }) => !errored, fileName_1 + ":133:33");
        }

      case "BufferSliding":
        {
          return tsplus_module_31.map_(tsplus_module_29.bind_(tsplus_module_30.tap_(tsplus_module_30.tap_(tsplus_module_29.bindValue_(tsplus_module_30.tap_(tsplus_module_30.tap_(tsplus_module_29.bind_(tsplus_module_29.bind_(tsplus_module_29.bind_(tsplus_module_29.Do(), "canceler", () => tsplus_module_28.make(fileName_1 + ":137:66"), fileName_1 + ":137:34"), "latch", () => tsplus_module_28.make(fileName_1 + ":138:63"), fileName_1 + ":138:34"), "size", () => cancelers.size, fileName_1 + ":139:34"), ({
            size
          }) => tsplus_module_32.when(() => size >= n, tsplus_module_12.flatMap_(cancelers.take, deferred => deferred.succeed(() => undefined, fileName_1 + ":143:86"), fileName_1 + ":143:55"), fileName_1 + ":141:42"), fileName_1 + ":140:33"), ({
            canceler
          }) => cancelers.offer(canceler, fileName_1 + ":146:67"), fileName_1 + ":146:33"), "raceIOs", ({
            canceler
          }) => tsplus_module_27.scoped(() => tsplus_module_12.flatMap_(tsplus_module_26.toPull(channel), pull => tsplus_module_25.race_(tsplus_module_25.race_(evaluatePull(pull), () => errorSignal.await(fileName_1 + ":153:62"), fileName_1 + ":153:44"), () => canceler.await(fileName_1 + ":154:59"), fileName_1 + ":154:44"), fileName_1 + ":151:43"), fileName_1 + ":148:44"), fileName_1 + ":147:39"), ({
            latch,
            raceIOs
          }) => tsplus_module_24.fork(tsplus_module_23.withPermit(permits, fileName_1 + ":158:38")(tsplus_module_15.zipRight_(latch.succeed(() => undefined, fileName_1 + ":159:58"), () => raceIOs, fileName_1 + ":159:71")), fileName_1 + ":160:38"), fileName_1 + ":157:33"), ({
            latch
          }) => latch.await(fileName_1 + ":162:60"), fileName_1 + ":162:33"), "errored", () => errorSignal.isDone(fileName_1 + ":163:70"), fileName_1 + ":163:34"), ({
            errored
          }) => !errored, fileName_1 + ":164:33");
        }
    }
  }), fileName_1 + ":87:31"), tsplus_module_10.boolean)(() => true, fileName_1 + ":170:54"), fileName_1 + ":171:26"), fileName_1 + ":84:13"), ({
    queue
  }) => queue, fileName_1 + ":173:13"), fileName_1 + ":43:24"), queue => {
    const consumer = tsplus_module_9.unwrap(() => tsplus_module_8.foldCause_(tsplus_module_7.flattenNow(queue.take, fileName_1 + ":184:27"), cause => tsplus_module_2.failCause(() => cause), either => tsplus_module_6.fold_(either, outDone => tsplus_module_3.succeedNow(outDone), outElem => tsplus_module_5.zipRight_(tsplus_module_4.write(() => outElem), () => consumer)), fileName_1 + ":184:39"));
    return consumer;
  }, fileName_1 + ":174:10"));
}
/**
 * @tsplus static ets/Channel/Aspects mergeAllWith
 */

export const mergeAllWith = (n, f, bufferSize = 16, mergeStrategy = MergeStrategy.BackPressure) => channels => mergeAllWith_(channels, n, f, bufferSize, mergeStrategy);
//# sourceMappingURL=mergeAllWith.mjs.map