"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;
// ets_tracing: off
const CK = __importStar(require("../../../../Collections/Immutable/Chunk"));
const HM = __importStar(require("../../../../Collections/Immutable/HashMap"));
const L = __importStar(require("../../../../Collections/Immutable/List"));
const Tp = __importStar(require("../../../../Collections/Immutable/Tuple"));
const T = __importStar(require("../../../../Effect"));
const Function_1 = require("../../../../Function");
const M = __importStar(require("../../../../Managed"));
const P = __importStar(require("../../../../Promise"));
const DistributedWithDynamic = __importStar(require("./distributedWithDynamic"));
/**
 * 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.
 */
function distributedWith_(self, n, maximumLag, decide) {
    return M.chain_(T.toManaged(P.make()), (prom) => {
        return M.chain_(DistributedWithDynamic.distributedWithDynamic_(self, maximumLag, (a) => T.chain_(P.await(prom), (_) => _(a)), (_) => T.unit), (next) => (T.toManaged(T.chain_(T.collectAll(CK.map_(CK.range(0, n), (id) => T.map_(next, ({ tuple: [key, queue] }) => Tp.tuple(Tp.tuple(key, id), queue)))), (entries) => {
            const { tuple: [mappings, queues] } = CK.reduceRight_(entries, Tp.tuple(HM.make(), L.empty()), ({ tuple: [mapping, queue] }, { tuple: [mappings, queues] }) => Tp.tuple(HM.set_(mappings, Tp.get_(mapping, 0), Tp.get_(mapping, 1)), L.prepend_(queues, queue)));
            return T.as_(P.succeed_(prom, (a) => T.map_(decide(a), (f) => (key) => f(HM.unsafeGet_(mappings, key)))), queues);
        }))));
    });
}
exports.distributedWith_ = distributedWith_;
/**
 * 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.
 *
 * @ets_data_first distributedWith_
 */
function distributedWith(n, maximumLag, decide) {
    return (self) => distributedWith_(self, n, maximumLag, decide);
}
exports.distributedWith = distributedWith;
//# sourceMappingURL=distributedWith.js.map