"use strict";

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

var tsplus_module_1 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/AtomicNumber"));

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

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

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Semaphore/operations/withPermit"));

var tsplus_module_5 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Maybe/definition"));

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

var tsplus_module_7 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Exit/operations/failCause"));

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

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

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

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

var tsplus_module_12 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Cause/operations/isInterrupted"));

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

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

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

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

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

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

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

var tsplus_module_20 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Queue/external/bounded"));

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

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

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

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

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

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

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

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

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

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

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

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

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

var tsplus_module_34 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Exit/operations/succeed"));

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

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; }

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

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


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

exports.distributedWithDynamic = distributedWithDynamic;

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.js.map