"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.partitionEither = void 0;
exports.partitionEither_ = partitionEither_;

var tsplus_module_1 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/List/definition"));

var tsplus_module_2 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/List/unsafeHead"));

var tsplus_module_3 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/fromQueueWithShutdown"));

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/flattenExitMaybe"));

var tsplus_module_5 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/collectLeft"));

var tsplus_module_6 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/List/unsafeLast"));

var tsplus_module_7 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/collectRight"));

var tsplus_module_8 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Tuple/make"));

var tsplus_module_9 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/succeedNow"));

var tsplus_module_10 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/dieMessage"));

var tsplus_module_11 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Either/fold"));

var tsplus_module_12 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/mapEffect"));

var tsplus_module_13 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/distributedWith"));

var tsplus_module_14 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/flatMap"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

/**
 * Split a stream by a predicate. The faster stream may advance by up to
 * buffer elements further than the slower one.
 *
 * @tsplus fluent ets/Stream partitionEither
 */
function partitionEither_(self, p, buffer = 16, __tsplusTrace) {
  return tsplus_module_14.flatMap_(tsplus_module_13.distributedWith_(tsplus_module_12.mapEffect_(self, p, __tsplusTrace), 2, buffer, either => tsplus_module_11.fold_(either, () => tsplus_module_9.succeedNow(_ => _ === 0, __tsplusTrace), () => tsplus_module_9.succeedNow(_ => _ === 1, __tsplusTrace)), __tsplusTrace), dequeues => {
    if (tsplus_module_1.length(dequeues) === 2) {
      return tsplus_module_9.succeedNow(tsplus_module_8.make(tsplus_module_5.collectLeft(tsplus_module_4.flattenExitMaybe(tsplus_module_3.fromQueueWithShutdown(() => tsplus_module_2.unsafeHead(dequeues)), __tsplusTrace), __tsplusTrace), tsplus_module_7.collectRight(tsplus_module_4.flattenExitMaybe(tsplus_module_3.fromQueueWithShutdown(() => tsplus_module_6.unsafeLast(dequeues)), __tsplusTrace), __tsplusTrace)), __tsplusTrace);
    }

    return tsplus_module_10.dieMessage(() => `Stream.partitionEither: expected two streams but received: ${tsplus_module_1.length(dequeues)}`, __tsplusTrace);
  }, __tsplusTrace);
}
/**
 * Split a stream by a predicate. The faster stream may advance by up to
 * buffer elements further than the slower one.
 *
 * @tsplus static ets/Stream/Aspects partitionEither
 */


const partitionEither = (p, buffer = 16, __tsplusTrace) => self => partitionEither_(self, p, buffer, __tsplusTrace);

exports.partitionEither = partitionEither;
//# sourceMappingURL=partitionEither.js.map