import * as tsplus_module_1 from "@tsplus/stdlib/collections/HashMap/empty";
import * as tsplus_module_2 from "@tsplus/stdlib/collections/List/empty";
import * as tsplus_module_3 from "@tsplus/stdlib/data/Tuple/make";
import * as tsplus_module_4 from "@tsplus/stdlib/collections/HashMap/set";
import * as tsplus_module_5 from "@tsplus/stdlib/collections/List/prepend";
import * as tsplus_module_6 from "@tsplus/stdlib/collections/Chunk/reduceRight";
import * as tsplus_module_7 from "@tsplus/stdlib/collections/HashMap/unsafeGet";
import * as tsplus_module_8 from "@effect/core/io/Effect/operations/map";
import * as tsplus_module_9 from "@effect/core/io/Effect/operations/as";
import * as tsplus_module_10 from "@tsplus/stdlib/collections/Chunk/range";
import * as tsplus_module_11 from "@tsplus/stdlib/collections/Chunk/map";
import * as tsplus_module_12 from "@effect/core/io/Effect/operations/excl-forEach";
import * as tsplus_module_13 from "@effect/core/io/Effect/operations/flatMap";
import * as tsplus_module_14 from "@effect/core/io/Effect/operations/unit";
import * as tsplus_module_15 from "@effect/core/stream/Stream/operations/distributedWithDynamic";
import * as tsplus_module_16 from "@effect/core/io/Deferred/operations/make";
/**
 * More powerful version of `Stream.broadcast`. Allows to provide a function
 * that determines what queues should receive which elements. The decide
 * function will receive the indices of the queues in the resulting list.
 *
 * @tsplus fluent ets/Stream distributedWith
 */

export function distributedWith_(self, n, maximumLag, decide, __tsplusTrace) {
  return tsplus_module_13.flatMap_(tsplus_module_16.make(__tsplusTrace), deferred => tsplus_module_13.flatMap_(tsplus_module_15.distributedWithDynamic_(self, maximumLag, a => tsplus_module_13.flatMap_(deferred.await(__tsplusTrace), f => f(a), __tsplusTrace), () => tsplus_module_14.unit, __tsplusTrace), next => tsplus_module_13.flatMap_(tsplus_module_12.collectAll(() => tsplus_module_11.map_(tsplus_module_10.range(0, n - 1), id => tsplus_module_8.map_(next, ({
    tuple: [key, queue]
  }) => tsplus_module_3.make(tsplus_module_3.make(key, id), queue), __tsplusTrace)), __tsplusTrace), entries => {
    const {
      tuple: [mappings, queues]
    } = tsplus_module_6.reduceRight_(entries, tsplus_module_3.make(tsplus_module_1.empty(), tsplus_module_2.empty()), ({
      tuple: [mapping, queue]
    }, {
      tuple: [mappings, queues]
    }) => tsplus_module_3.make(tsplus_module_4.set_(mappings, mapping.get(0), mapping.get(1)), tsplus_module_5.prepend_(queues, queue)));
    return tsplus_module_9.as_(deferred.succeed(() => a => tsplus_module_8.map_(decide(a), f => key => f(tsplus_module_7.unsafeGet_(mappings, key)), __tsplusTrace), __tsplusTrace), () => queues, __tsplusTrace);
  }, __tsplusTrace), __tsplusTrace), __tsplusTrace);
}
/**
 * More powerful version of `Stream.broadcast`. Allows to provide a function
 * that determines what queues should receive which elements. The decide
 * function will receive the indices of the queues in the resulting list.
 *
 * @tsplus static ets/Stream/Aspects distributedWith
 */

export const distributedWith = (n, maximumLag, decide, __tsplusTrace) => self => distributedWith_(self, n, maximumLag, decide, __tsplusTrace);
//# sourceMappingURL=distributedWith.mjs.map