"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.reduceAllParN = exports.reduceAllParN_ = exports.reduceAllPar = exports.reduceAllPar_ = exports.reduceAll = exports.reduceAll_ = void 0;
// ets_tracing: off
const A = __importStar(require("../Collections/Immutable/Array"));
const NA = __importStar(require("../Collections/Immutable/NonEmptyArray"));
const O = __importStar(require("../Option"));
const core_1 = require("./core");
const map_1 = require("./map");
const mergeAll_1 = require("./mergeAll");
const zipWith_1 = require("./zipWith");
/**
 * Reduces an `Iterable[IO]` to a single `IO`, working sequentially.
 */
function reduceAll_(as, f, __trace) {
    return (0, core_1.suspend)(() => A.reduce_(NA.tail(as), NA.head(as), (acc, a) => (0, zipWith_1.zipWith_)(acc, a, f)), __trace);
}
exports.reduceAll_ = reduceAll_;
/**
 * Reduces an `Iterable[IO]` to a single `IO`, working sequentially.
 *
 * @ets_data_first reduceAll_
 */
function reduceAll(f, __trace) {
    return (as) => reduceAll_(as, f, __trace);
}
exports.reduceAll = reduceAll;
/**
 * Reduces an `Iterable[IO]` to a single `IO`, working in parallel.
 */
function reduceAllPar_(as, f, __trace) {
    return (0, map_1.map_)((0, mergeAll_1.mergeAllPar_)(as, O.none, (acc, elem) => O.some(O.fold_(acc, () => elem, (a) => f(a, elem))), __trace), O.getOrElse(() => {
        throw new Error("Bug");
    }));
}
exports.reduceAllPar_ = reduceAllPar_;
/**
 * Reduces an `Iterable[IO]` to a single `IO`, working in parallel.
 *
 * @ets_data_first reduceAllPar_
 */
function reduceAllPar(f, __trace) {
    return (as) => reduceAllPar_(as, f, __trace);
}
exports.reduceAllPar = reduceAllPar;
/**
 * Reduces an `Iterable[IO]` to a single `IO`, working in up to `n` fibers in parallel.
 */
function reduceAllParN_(as, n, f, __trace) {
    return (0, map_1.map_)((0, mergeAll_1.mergeAllParN_)(as, n, O.none, (acc, elem) => O.some(O.fold_(acc, () => elem, (a) => f(a, elem))), __trace), O.getOrElse(() => {
        throw new Error("Bug");
    }));
}
exports.reduceAllParN_ = reduceAllParN_;
/**
 * Reduces an `Iterable[IO]` to a single `IO`, working in up to `n` fibers in parallel.
 *
 * @ets_data_first reduceAllParN_
 */
function reduceAllParN(n, f, __trace) {
    return (as) => reduceAllParN_(as, n, f, __trace);
}
exports.reduceAllParN = reduceAllParN;
//# sourceMappingURL=reduceAll.js.map