"use strict"
var __createBinding =
  (this && this.__createBinding) ||
  (Object.create
    ? function (o, m, k, k2) {
        if (k2 === undefined) k2 = k
        Object.defineProperty(o, k2, {
          enumerable: true,
          get: function () {
            return m[k]
          }
        })
      }
    : function (o, m, k, k2) {
        if (k2 === undefined) k2 = k
        o[k2] = m[k]
      })
var __setModuleDefault =
  (this && this.__setModuleDefault) ||
  (Object.create
    ? function (o, v) {
        Object.defineProperty(o, "default", { enumerable: true, value: v })
      }
    : function (o, v) {
        o["default"] = v
      })
var __importStar =
  (this && this.__importStar) ||
  function (mod) {
    if (mod && mod.__esModule) return mod
    var result = {}
    if (mod != null)
      for (var k in mod)
        if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k))
          __createBinding(result, mod, k)
    __setModuleDefault(result, mod)
    return result
  }
Object.defineProperty(exports, "__esModule", { value: true })
exports.distributedWith_ = exports.distributedWith = void 0
// tracing: off
const A = __importStar(require("../../Collections/Immutable/Chunk"))
const Map = __importStar(require("../../Collections/Immutable/Map"))
const Function_1 = require("../../Function")
const P = __importStar(require("../../Promise"))
const T = __importStar(require("../_internal/effect"))
const M = __importStar(require("../_internal/managed"))
const distributedWithDynamic_1 = require("./distributedWithDynamic")
/**
 * More powerful version of `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.
 */
function distributedWith(n, maximumLag, decide) {
  return (self) => distributedWith_(self, n, maximumLag, decide)
}
exports.distributedWith = distributedWith
/**
 * More powerful version of `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.
 */
function distributedWith_(self, n, maximumLag, decide) {
  return M.chain_(M.fromEffect(P.make()), (prom) =>
    M.chain_(
      (0, distributedWithDynamic_1.distributedWithDynamic_)(
        self,
        maximumLag,
        (o) => T.chain_(P.await(prom), (_) => _(o)),
        (_) => T.unit
      ),
      (next) =>
        M.fromEffect(
          T.chain_(
            A.mapM_(
              A.map_(A.range(0, n - 1), (id) =>
                T.map_(next, ({ tuple: [key, queue] }) => [[key, id], queue])
              ),
              Function_1.identity
            ),
            (entries) => {
              const [mappings, queues] = A.reduceRight_(
                entries,
                [Map.empty, A.empty()],
                ([mapping, queue], [mappings, queues]) => [
                  Map.insert(mapping[0], mapping[1])(mappings),
                  A.concat_(A.single(queue), queues)
                ]
              )
              return T.as_(
                P.succeed_(prom, (o) =>
                  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
                  T.map_(decide(o), (f) => (key) => f(mappings.get(key)))
                ),
                queues
              )
            }
          )
        )
    )
  )
}
exports.distributedWith_ = distributedWith_
//# sourceMappingURL=distributedWith.js.map
