"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.bindAllParN_ = exports.bindAllParN = exports.bindAllPar_ = exports.bindAllPar = exports.bindAll_ = exports.bindAll = void 0;
// ets_tracing: off
const R = __importStar(require("../Collections/Immutable/Dictionary"));
const core_1 = require("./core");
const excl_forEach_1 = require("./excl-forEach");
const map_1 = require("./map");
/**
 * Bind a record of effects in a do
 *
 * @ets_data_first bindAll_
 */
function bindAll(r, __trace) {
    // @ts-expect-error
    return (s) => bindAll_(s, r, __trace);
}
exports.bindAll = bindAll;
/**
 * Bind a record of effects in a do
 */
function bindAll_(s, r, __trace) {
    return (0, core_1.chain_)(s, (k) => (0, map_1.map_)((0, excl_forEach_1.forEach_)(R.collect_(r(k), (k, v) => [k, v]), ([_, e]) => (0, map_1.map_)(e, (a) => [_, a])), (values) => {
        const res = {};
        for (const [k, v] of values) {
            res[k] = v;
        }
        return Object.assign(res, k);
    }), __trace);
}
exports.bindAll_ = bindAll_;
/**
 * Bind a record of effects, in parallel, in a do
 *
 * @ets_data_first bindAllPar_
 */
function bindAllPar(r, __trace) {
    // @ts-expect-error
    return (s) => bindAllPar_(s, r, __trace);
}
exports.bindAllPar = bindAllPar;
/**
 * Bind a record of effects, in parallel, in a do
 */
function bindAllPar_(s, r, __trace) {
    return (0, core_1.chain_)(s, (k) => (0, map_1.map_)((0, excl_forEach_1.forEachPar_)(R.collect_(r(k), (k, v) => [k, v]), ([_, e]) => (0, map_1.map_)(e, (a) => [_, a])), (values) => {
        const res = {};
        for (const [k, v] of values) {
            res[k] = v;
        }
        return Object.assign(res, k);
    }), __trace);
}
exports.bindAllPar_ = bindAllPar_;
/**
 * Bind a record of effects, in parallel (up to N fibers), in a do
 *
 * @ets_data_first bindAllParN_
 */
function bindAllParN(n, r, __trace) {
    // @ts-expect-error
    return (s) => bindAllParN_(s, n, r, __trace);
}
exports.bindAllParN = bindAllParN;
/**
 * Bind a record of effects, in parallel (up to N fibers), in a do
 */
function bindAllParN_(s, n, r, __trace) {
    return (0, core_1.chain_)(s, (k) => (0, map_1.map_)((0, excl_forEach_1.forEachParN_)(R.collect_(r(k), (k, v) => [k, v]), n, ([_, e]) => (0, map_1.map_)(e, (a) => [_, a])), (values) => {
        const res = {};
        for (const [k, v] of values) {
            res[k] = v;
        }
        return Object.assign(res, k);
    }), __trace);
}
exports.bindAllParN_ = bindAllParN_;
//# sourceMappingURL=bindAll.js.map