"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.forEachParN_ = exports.forEachParN = exports.forEachPar_ = exports.forEachPar = exports.forEachUnit = exports.forEachUnit_ = exports.forEachExec = exports.forEachExec_ = exports.forEach_ = exports.forEach = void 0;
// ets_tracing: off
const Chunk = __importStar(require("../Collections/Immutable/Chunk"));
const Tp = __importStar(require("../Collections/Immutable/Tuple"));
const ExecutionStrategy_1 = require("../Effect/ExecutionStrategy");
const core_1 = require("./core");
const T = __importStar(require("./deps"));
const managed_1 = require("./managed");
/**
 * Applies the function `f` to each element of the `Iterable<A>` and
 * returns the results in a new `B[]`.
 *
 * For a parallel version of this method, see `forEachPar`.
 * If you do not need the results, see `forEachUnit` for a more efficient implementation.
 *
 * @ets_data_first forEach_
 */
function forEach(f, __trace) {
    return (as) => forEach_(as, f, __trace);
}
exports.forEach = forEach;
/**
 * Applies the function `f` to each element of the `Iterable<A>` and
 * returns the results in a new `B[]`.
 *
 * For a parallel version of this method, see `forEachPar_`.
 * If you do not need the results, see `forEachUnit_` for a more efficient implementation.
 */
function forEach_(as, f, __trace) {
    return (0, managed_1.managedApply)(T.map_(T.forEach_(as, (a) => f(a).effect, __trace), (res) => {
        const fins = Chunk.map_(res, (k) => k.get(0));
        const as = Chunk.map_(res, (k) => k.get(1));
        return Tp.tuple((e) => T.forEach_(Chunk.reverse(fins), (fin) => fin(e), __trace), as);
    }));
}
exports.forEach_ = forEach_;
/**
 * Applies the function `f` to each element of the `Iterable<A>` in parallel,
 * and returns the results in a new `readonly B[]`.
 *
 * For a sequential version of this method, see `forEach`.
 */
function forEachExec_(as, es, f, __trace) {
    switch (es._tag) {
        case "Sequential": {
            return forEach_(as, f, __trace);
        }
        case "Parallel": {
            return forEachPar_(as, f, __trace);
        }
        case "ParallelN": {
            return forEachParN_(as, es.n, f, __trace);
        }
    }
}
exports.forEachExec_ = forEachExec_;
/**
 * Applies the function `f` to each element of the `Iterable<A>` in parallel,
 * and returns the results in a new `readonly B[]`.
 *
 * For a sequential version of this method, see `forEach`.
 *
 * @ets_data_first forEachExec_
 */
function forEachExec(es, f, __trace) {
    return (as) => forEachExec_(as, es, f, __trace);
}
exports.forEachExec = forEachExec;
/**
 * Applies the function `f` to each element of the `Iterable[A]` and runs
 * produced effects sequentially.
 *
 * Equivalent to `forEach(as)(f).unit`, but without the cost of building
 * the list of results.
 */
function forEachUnit_(as, f, __trace) {
    return (0, managed_1.managedApply)(T.map_(T.forEach_(as, (a) => f(a).effect, __trace), (result) => {
        const { tuple: [fins] } = Chunk.unzip(result);
        return Tp.tuple((e) => T.forEach_(Chunk.reverse(fins), (f) => f(e), __trace), undefined);
    }));
}
exports.forEachUnit_ = forEachUnit_;
/**
 * Applies the function `f` to each element of the `Iterable[A]` and runs
 * produced effects sequentially.
 *
 * Equivalent to `forEach(as)(f).unit`, but without the cost of building
 * the list of results.
 *
 * @ets_data_first forEachUnit_
 */
function forEachUnit(f, __trace) {
    return (as) => forEachUnit_(as, f, __trace);
}
exports.forEachUnit = forEachUnit;
/**
 * Applies the function `f` to each element of the `Iterable<A>` in parallel,
 * and returns the results in a new `B[]`.
 *
 * For a sequential version of this method, see `forEach`.
 *
 * @ets_data_first forEachPar_
 */
function forEachPar(f, __trace) {
    return (as) => forEachPar_(as, f, __trace);
}
exports.forEachPar = forEachPar;
/**
 * Applies the function `f` to each element of the `Iterable<A>` in parallel,
 * and returns the results in a new `B[]`.
 *
 * For a sequential version of this method, see `forEach_`.
 */
function forEachPar_(as, f, __trace) {
    return (0, core_1.mapM_)((0, core_1.makeManagedReleaseMap)(T.parallel, __trace), (parallelReleaseMap) => {
        const makeInnerMap = T.provideSome_(T.map_((0, core_1.makeManagedReleaseMap)(ExecutionStrategy_1.sequential).effect, ({ tuple: [_, x] }) => x), (x) => Tp.tuple(x, parallelReleaseMap));
        return T.forEachPar_(as, (a) => T.map_(T.chain_(makeInnerMap, (innerMap) => T.provideSome_(f(a).effect, (u) => Tp.tuple(u, innerMap))), ({ tuple: [_, b] }) => b));
    });
}
exports.forEachPar_ = forEachPar_;
/**
 * Applies the function `f` to each element of the `Iterable<A>` in parallel,
 * and returns the results in a new `B[]`.
 *
 * Unlike `forEachPar`, this method will use at most up to `n` fibers.
 *
 * @ets_data_first forEachParN_
 */
function forEachParN(n, f, __trace) {
    return (as) => forEachParN_(as, n, f, __trace);
}
exports.forEachParN = forEachParN;
/**
 * Applies the function `f` to each element of the `Iterable<A>` in parallel,
 * and returns the results in a new `B[]`.
 *
 * Unlike `forEachPar_`, this method will use at most up to `n` fibers.
 */
function forEachParN_(as, n, f, __trace) {
    return (0, core_1.mapM_)((0, core_1.makeManagedReleaseMap)(T.parallelN(n), __trace), (parallelReleaseMap) => {
        const makeInnerMap = T.provideSome_(T.map_((0, core_1.makeManagedReleaseMap)(ExecutionStrategy_1.sequential).effect, ({ tuple: [_, x] }) => x), (x) => Tp.tuple(x, parallelReleaseMap));
        return T.forEachParN_(as, n, (a) => T.map_(T.chain_(makeInnerMap, (innerMap) => T.provideSome_(f(a).effect, (u) => Tp.tuple(u, innerMap))), ({ tuple: [_, b] }) => b));
    });
}
exports.forEachParN_ = forEachParN_;
//# sourceMappingURL=forEach.js.map