"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.mapMPar = void 0;
// ets_tracing: off
const A = __importStar(require("../../Collections/Immutable/Chunk"));
const Function_1 = require("../../Function");
const O = __importStar(require("../../Option"));
const P = __importStar(require("../../Promise"));
const Q = __importStar(require("../../Queue"));
const SM = __importStar(require("../../Semaphore"));
const T = __importStar(require("../_internal/effect"));
const M = __importStar(require("../_internal/managed"));
const Pull = __importStar(require("../Pull"));
const definitions_1 = require("./definitions");
const forEach_1 = require("./forEach");
/**
 * Maps over elements of the stream with the specified effectful function,
 * executing up to `n` invocations of `f` concurrently. Transformed elements
 * will be emitted in the original order.
 */
function mapMPar(n) {
    return (f) => (self) => new definitions_1.Stream((M.map_(M.tap_(M.bind_(M.bind_(M.bind_(M.do, "out", () => M.fromEffect(Q.makeBounded(n))), "errorSignal", () => M.fromEffect(P.make())), "permits", () => M.fromEffect(SM.makeSemaphore(n))), ({ errorSignal, out, permits }) => (M.fork(M.foldCauseM_((0, forEach_1.forEachManaged)((a) => (T.asUnit(T.tap_(T.tap_(T.tap_(T.bind_(T.bind_(T.do, "p", () => P.make()), "latch", () => P.make()), ({ p }) => Q.offer_(out, (T.mapError_(P.await(p), O.some)))), ({ latch, p }) => (T.fork(SM.withPermit_(T.chain_(
    // Make sure we start evaluation before moving on to the next element
    P.succeed(undefined)(latch), () => (
    // Transfer the result to the consuming stream
    T.to_(
    // Notify other tasks of a failure
    T.tapCause_(
    // Interrupt evaluation if another task fails
    T.raceFirst_(P.await(errorSignal), f(a)), (e) => (P.halt(e)(errorSignal))), p))), permits)))), ({ latch }) => P.await(latch)))))(self), (c) => M.fromEffect(Q.offer_(out, Pull.halt(c))), () => (M.fromEffect(T.chain_(SM.withPermits_(T.unit, permits, n), () => Q.offer_(out, Pull.end)))))))), ({ out }) => (T.map_(T.flatten(Q.take(out)), (o) => A.single(o))))));
}
exports.mapMPar = mapMPar;
//# sourceMappingURL=mapMPar.js.map