import * as tsplus_module_1 from "@tsplus/stdlib/data/AtomicNumber";
import * as tsplus_module_2 from "@effect/core/io/Effect/operations/unit";
import * as tsplus_module_3 from "@effect/core/io/Effect/operations/flatten";
import * as tsplus_module_4 from "@effect/core/io/Semaphore/operations/withPermit";
import * as tsplus_module_5 from "@tsplus/stdlib/data/Maybe/definition";
import * as tsplus_module_6 from "@effect/core/io/Cause/operations/map";
import * as tsplus_module_7 from "@effect/core/io/Exit/operations/failCause";
import * as tsplus_module_8 from "@effect/core/io/Exit/operations/fail";
import * as tsplus_module_9 from "@effect/core/stream/Stream/operations/runForEachScoped";
import * as tsplus_module_10 from "@effect/core/io/Effect/operations/foldCauseEffect";
import * as tsplus_module_11 from "@effect/core/io/Effect/operations/fork";
import * as tsplus_module_12 from "@effect/core/io/Cause/operations/isInterrupted";
import * as tsplus_module_13 from "@effect/core/io/Effect/operations/catchSomeCause";
import * as tsplus_module_14 from "@effect/core/io/Effect/operations/excl-forEach";
import * as tsplus_module_15 from "@tsplus/stdlib/collections/HashMap/values";
import * as tsplus_module_16 from "@effect/core/io/Effect/operations/map";
import * as tsplus_module_17 from "@tsplus/stdlib/data/Tuple/make";
import * as tsplus_module_18 from "@tsplus/stdlib/collections/HashMap/set";
import * as tsplus_module_19 from "@effect/core/io/Effect/operations/succeed";
import * as tsplus_module_20 from "@effect/core/io/Queue/external/bounded";
import * as tsplus_module_21 from "@effect/core/io/Effect/operations/do";
import * as tsplus_module_22 from "@effect/core/io/Effect/operations/tap";
import * as tsplus_module_23 from "@effect/core/io/Ref/operations/make";
import * as tsplus_module_24 from "@effect/core/io/Semaphore/operations/make";
import * as tsplus_module_25 from "@tsplus/stdlib/collections/HashMap/empty";
import * as tsplus_module_26 from "@effect/core/io/Effect/operations/flatMap";
import * as tsplus_module_27 from "@effect/core/io/Effect/operations/acquireRelease";
import * as tsplus_module_28 from "@tsplus/stdlib/collections/List/definition";
import * as tsplus_module_29 from "@tsplus/stdlib/collections/HashMap/removeMany";
import * as tsplus_module_30 from "@tsplus/stdlib/collections/List/empty";
import * as tsplus_module_31 from "@tsplus/stdlib/collections/List/prepend";
import * as tsplus_module_32 from "@effect/core/io/Effect/operations/succeedNow";
import * as tsplus_module_33 from "@effect/core/io/Effect/operations/failCause";
import * as tsplus_module_34 from "@effect/core/io/Exit/operations/succeed";
import * as tsplus_module_35 from "@effect/core/io/Effect/operations/reduce";
const distributedWithDynamicId = /*#__PURE__*/new tsplus_module_1.AtomicNumber(0);
/**
 * More powerful version of `Stream.distributedWith`. This returns a function
 * that will produce new queues and corresponding indices. You can also provide
 * a function that will be executed after the final events are enqueued in all
 * queues. Shutdown of the queues is handled by the driver. Downstream users can
 * also shutdown queues manually. In this case the driver will continue but no
 * longer backpressure on them.
 *
 * @tsplus fluent ets/Stream distributedWithDynamic
 */

export function distributedWithDynamic_(self, maximumLag, decide, done = () => tsplus_module_2.unit, __tsplusTrace) {
  return tsplus_module_16.map_(tsplus_module_21.bind_(tsplus_module_21.bind_(tsplus_module_21.Do(), "queuesRef", () => tsplus_module_27.acquireRelease(() => tsplus_module_23.makeRef(() => tsplus_module_25.empty(), __tsplusTrace), map => tsplus_module_26.flatMap_(map.get(__tsplusTrace), queues => tsplus_module_14.forEach(() => queues, ({
    tuple: [, queue]
  }) => queue.shutdown, __tsplusTrace), __tsplusTrace), __tsplusTrace), __tsplusTrace), "add", ({
    queuesRef
  }) => tsplus_module_16.map_(tsplus_module_22.tap_(tsplus_module_21.bindValue_(tsplus_module_21.bind_(tsplus_module_21.bind_(tsplus_module_21.Do(), "queuesLock", () => tsplus_module_24.make(1, __tsplusTrace), __tsplusTrace), "newQueue", () => tsplus_module_23.makeRef(() => tsplus_module_16.map_(tsplus_module_22.tap_(tsplus_module_21.bind_(tsplus_module_21.bind_(tsplus_module_21.Do(), "queue", () => tsplus_module_20.bounded(maximumLag, __tsplusTrace), __tsplusTrace), "id", () => tsplus_module_19.succeed(() => distributedWithDynamicId.incrementAndGet(), __tsplusTrace), __tsplusTrace), ({
    id,
    queue
  }) => queuesRef.update(map => tsplus_module_18.set_(map, id, queue), __tsplusTrace), __tsplusTrace), ({
    id,
    queue
  }) => tsplus_module_17.make(id, queue), __tsplusTrace), __tsplusTrace), __tsplusTrace), "finalize", ({
    newQueue,
    queuesLock
  }) => endTake => // Make sure that no queues are currently being added
  tsplus_module_4.withPermit(queuesLock, __tsplusTrace)(tsplus_module_2.unit_(tsplus_module_22.tap_(tsplus_module_22.tap_(tsplus_module_21.bind_(tsplus_module_22.tap_(tsplus_module_21.Do(), () => // All newly created queues should end immediately
  newQueue.set(tsplus_module_16.map_(tsplus_module_22.tap_(tsplus_module_21.bind_(tsplus_module_22.tap_(tsplus_module_21.bind_(tsplus_module_21.Do(), "queue", () => tsplus_module_20.bounded(1, __tsplusTrace), __tsplusTrace), ({
    queue
  }) => queue.offer(endTake, __tsplusTrace), __tsplusTrace), "id", () => tsplus_module_19.succeed(() => distributedWithDynamicId.incrementAndGet(), __tsplusTrace), __tsplusTrace), ({
    id,
    queue
  }) => queuesRef.update(map => tsplus_module_18.set_(map, id, queue), __tsplusTrace), __tsplusTrace), ({
    id,
    queue
  }) => tsplus_module_17.make(id, queue), __tsplusTrace), __tsplusTrace), __tsplusTrace), "queues", () => tsplus_module_16.map_(queuesRef.get(__tsplusTrace), map => tsplus_module_15.values(map), __tsplusTrace), __tsplusTrace), ({
    queues
  }) => tsplus_module_14.forEach(() => queues, queue => tsplus_module_13.catchSomeCause_(queue.offer(endTake, __tsplusTrace), cause => tsplus_module_12.isInterrupted(cause) ? tsplus_module_5.some(tsplus_module_2.unit) : tsplus_module_5.none, __tsplusTrace), __tsplusTrace), __tsplusTrace), () => done(endTake), __tsplusTrace), __tsplusTrace)), __tsplusTrace), ({
    finalize
  }) => tsplus_module_11.fork(tsplus_module_10.foldCauseEffect_(tsplus_module_9.runForEachScoped_(self, a => offer(queuesRef, decide, a, __tsplusTrace), __tsplusTrace), cause => finalize(tsplus_module_7.failCause(tsplus_module_6.map_(cause, tsplus_module_5.some))), () => finalize(tsplus_module_8.fail(tsplus_module_5.none)), __tsplusTrace), __tsplusTrace), __tsplusTrace), ({
    newQueue,
    queuesLock
  }) => tsplus_module_4.withPermit(queuesLock, __tsplusTrace)(tsplus_module_3.flattenNow(newQueue.get(__tsplusTrace), __tsplusTrace)), __tsplusTrace), __tsplusTrace), ({
    add
  }) => add, __tsplusTrace);
}
/**
 * More powerful version of `Stream.distributedWith`. This returns a function
 * that will produce new queues and corresponding indices. You can also provide
 * a function that will be executed after the final events are enqueued in all
 * queues. Shutdown of the queues is handled by the driver. Downstream users can
 * also shutdown queues manually. In this case the driver will continue but no
 * longer backpressure on them.
 */

export const distributedWithDynamic = (maximumLag, decide, done = () => Effect.unit, __tsplusTrace) => self => distributedWithDynamic_(self, maximumLag, decide, done, __tsplusTrace);

function offer(ref, decide, a, __tsplusTrace) {
  return tsplus_module_22.tap_(tsplus_module_21.bind_(tsplus_module_21.bind_(tsplus_module_21.Do(), "shouldProcess", () => decide(a), __tsplusTrace), "queues", () => ref.get(__tsplusTrace), __tsplusTrace), ({
    queues,
    shouldProcess
  }) => tsplus_module_26.flatMap_(tsplus_module_35.reduce(() => queues, () => tsplus_module_30.empty(), (acc, {
    tuple: [id, queue]
  }) => shouldProcess(id) ? tsplus_module_10.foldCauseEffect_(queue.offer(tsplus_module_34.succeed(a), __tsplusTrace), cause => // Ignore all downstream queues that were shut down and remove
  // them later
  tsplus_module_12.isInterrupted(cause) ? tsplus_module_32.succeedNow(tsplus_module_31.prepend_(acc, id), __tsplusTrace) : tsplus_module_33.failCause(() => cause, __tsplusTrace), () => tsplus_module_32.succeedNow(acc, __tsplusTrace), __tsplusTrace) : tsplus_module_32.succeedNow(acc, __tsplusTrace), __tsplusTrace), ids => tsplus_module_28.isNil(ids) ? tsplus_module_2.unit : ref.update(map => tsplus_module_29.removeMany_(map, ids), __tsplusTrace), __tsplusTrace), __tsplusTrace);
}
//# sourceMappingURL=distributedWithDynamic.mjs.map