"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.mapOutEffectPar = exports.mapOutEffectPar_ = void 0;
// ets_tracing: off
const T = __importStar(require("../../../../Effect"));
const E = __importStar(require("../../../../Either"));
const F = __importStar(require("../../../../Fiber"));
const Function_1 = require("../../../../Function");
const M = __importStar(require("../../../../Managed"));
const P = __importStar(require("../../../../Promise"));
const Q = __importStar(require("../../../../Queue"));
const SM = __importStar(require("../../../../Semaphore"));
const C = __importStar(require("../core"));
const Managed = __importStar(require("./managed"));
const ToPull = __importStar(require("./toPull"));
const Unwrap = __importStar(require("./unwrap"));
const ZipRight = __importStar(require("./zipRight"));
function mapOutEffectPar_(self, n, f) {
    return Managed.managed_(M.withChildren((getChildren) => (M.map_(M.tap_(M.bind_(M.bind_(M.bind_(M.bind_(M.tap_(M.do, () => M.finalizer(T.chain_(getChildren, F.interruptAll))), "queue", () => T.toManagedRelease_(Q.makeBounded(n), Q.shutdown)), "errorSignal", () => P.makeManaged()), "permits", () => T.toManaged(SM.makeSemaphore(n))), "pull", () => ToPull.toPull(self)), ({ errorSignal, permits, pull, queue }) => (T.forkManaged(T.interruptible(T.forever(T.foldCauseM_(pull, (cause) => Q.offer_(queue, T.halt(cause)), E.fold((outDone) => T.asUnit(T.zipRight_(T.interruptible(SM.withPermits_(T.unit, permits, n)), Q.offer_(queue, T.succeed(E.left(outDone))))), (outElem) => (T.asUnit(T.tap_(T.tap_(T.tap_(T.bind_(T.bind_(T.do, "p", () => P.make()), "latch", () => P.make()), ({ p }) => Q.offer_(queue, T.map_(P.await(p), E.right))), ({ latch, p }) => T.fork(SM.withPermit_((T.zipRight_(P.succeed_(latch, void 0), (T.to_(T.tapCause_(T.raceFirst_(P.await(errorSignal), f(outElem)), (_) => P.halt_(errorSignal, _)), p)))), permits))), ({ latch }) => P.await(latch))))))))))), ({ queue }) => queue))), (queue) => {
        const consumer = Unwrap.unwrap((T.foldCause_(T.flatten(Q.take(queue)), (_) => C.failCause(_), E.fold((outDone) => C.end(outDone), (outElem) => ZipRight.zipRight_(C.write(outElem), consumer)))));
        return consumer;
    });
}
exports.mapOutEffectPar_ = mapOutEffectPar_;
/**
 * @ets_data_first mapOutEffectPar_
 */
function mapOutEffectPar(n, f) {
    return (self) => mapOutEffectPar_(self, n, f);
}
exports.mapOutEffectPar = mapOutEffectPar;
//# sourceMappingURL=mapOutEffectPar.js.map