import * as tsplus_module_1 from "@tsplus/stdlib/collections/List/definition";
import * as tsplus_module_2 from "@tsplus/stdlib/collections/List/unsafeHead";
import * as tsplus_module_3 from "@effect/core/stream/Stream/operations/fromQueueWithShutdown";
import * as tsplus_module_4 from "@effect/core/stream/Stream/operations/flattenExitMaybe";
import * as tsplus_module_5 from "@effect/core/stream/Stream/operations/collectLeft";
import * as tsplus_module_6 from "@tsplus/stdlib/collections/List/unsafeLast";
import * as tsplus_module_7 from "@effect/core/stream/Stream/operations/collectRight";
import * as tsplus_module_8 from "@tsplus/stdlib/data/Tuple/make";
import * as tsplus_module_9 from "@effect/core/io/Effect/operations/succeedNow";
import * as tsplus_module_10 from "@effect/core/io/Effect/operations/dieMessage";
import * as tsplus_module_11 from "@tsplus/stdlib/data/Either/fold";
import * as tsplus_module_12 from "@effect/core/stream/Stream/operations/mapEffect";
import * as tsplus_module_13 from "@effect/core/stream/Stream/operations/distributedWith";
import * as tsplus_module_14 from "@effect/core/io/Effect/operations/flatMap";
/**
 * 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
 */

export 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
 */

export const partitionEither = (p, buffer = 16, __tsplusTrace) => self => partitionEither_(self, p, buffer, __tsplusTrace);
//# sourceMappingURL=partitionEither.mjs.map