"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.partitionEither = exports.partitionEither_ = void 0;
// ets_tracing: off
const L = __importStar(require("../../../../Collections/Immutable/List"));
const Tp = __importStar(require("../../../../Collections/Immutable/Tuple"));
const T = __importStar(require("../../../../Effect"));
const E = __importStar(require("../../../../Either"));
const Function_1 = require("../../../../Function");
const M = __importStar(require("../../../../Managed"));
const CollectLeft = __importStar(require("./collectLeft"));
const CollectRight = __importStar(require("./collectRight"));
const DistributedWith = __importStar(require("./distributedWith"));
const FlattenExitOption = __importStar(require("./flattenExitOption"));
const FromQueueWithShutdown = __importStar(require("./fromQueueWithShutdown"));
const MapEffect = __importStar(require("./mapEffect"));
/**
 * Split a stream by a predicate. The faster stream may advance by up to buffer elements further than the slower one.
 */
function partitionEither_(self, p, buffer = 16) {
    return (M.chain_(DistributedWith.distributedWith_(MapEffect.mapEffect_(self, p), 2, buffer, E.fold((_) => T.succeed((_) => _ === 0), (_) => T.succeed((_) => _ === 1))), (dequeues) => {
        if (L.size(dequeues) === 2) {
            return M.succeed(Tp.tuple(CollectLeft.collectLeft(FlattenExitOption.flattenExitOption(FromQueueWithShutdown.fromQueueWithShutdown_(L.unsafeFirst(dequeues)))), CollectRight.collectRight(FlattenExitOption.flattenExitOption(FromQueueWithShutdown.fromQueueWithShutdown_(L.unsafeLast(dequeues))))));
        }
        return M.dieMessage(`partitionEither: expected two streams but got ${L.size(dequeues)}`);
    }));
}
exports.partitionEither_ = partitionEither_;
/**
 * Split a stream by a predicate. The faster stream may advance by up to buffer elements further than the slower one.
 *
 * @ets_data_first partitionEither_
 */
function partitionEither(p, buffer = 16) {
    return (self) => partitionEither_(self, p, buffer);
}
exports.partitionEither = partitionEither;
//# sourceMappingURL=partitionEither.js.map