"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.distributedWith = void 0;
exports.distributedWith_ = distributedWith_;

var tsplus_module_1 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/HashMap/empty"));

var tsplus_module_2 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/List/empty"));

var tsplus_module_3 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Tuple/make"));

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/HashMap/set"));

var tsplus_module_5 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/List/prepend"));

var tsplus_module_6 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Chunk/reduceRight"));

var tsplus_module_7 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/HashMap/unsafeGet"));

var tsplus_module_8 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/map"));

var tsplus_module_9 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/as"));

var tsplus_module_10 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Chunk/range"));

var tsplus_module_11 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Chunk/map"));

var tsplus_module_12 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/excl-forEach"));

var tsplus_module_13 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/flatMap"));

var tsplus_module_14 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/unit"));

var tsplus_module_15 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/distributedWithDynamic"));

var tsplus_module_16 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Deferred/operations/make"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

/**
 * 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
 */
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
 */


const distributedWith = (n, maximumLag, decide, __tsplusTrace) => self => distributedWith_(self, n, maximumLag, decide, __tsplusTrace);

exports.distributedWith = distributedWith;
//# sourceMappingURL=distributedWith.js.map