// tracing: off
import * as A from "../../Collections/Immutable/Chunk"
import * as Tp from "../../Collections/Immutable/Tuple"
import * as E from "../../Either"
import { pipe } from "../../Function"
import * as T from "../_internal/effect"
import * as M from "../_internal/managed"
import { collectLeft } from "./collectLeft"
import { collectRight } from "./collectRight"
import { distributedWith } from "./distributedWith"
import { flattenExitOption } from "./flattenExitOption"
import { fromQueueWithShutdown } from "./fromQueueWithShutdown"
import { mapM } from "./mapM"
/**
 * Split a stream by a predicate. The faster stream may advance by up to buffer elements further than the slower one.
 */

export function partitionEither_(self, p, buffer = 16) {
  return M.chain_(
    distributedWith(
      2,
      buffer,
      E.fold(
        () => T.succeed((_) => _ === 0),
        () => T.succeed((_) => _ === 1)
      )
    )(mapM(p)(self)),
    (queues) => {
      const [q1, q2] = queues

      if (q1 && q2) {
        return M.succeed(
          Tp.tuple(
            collectLeft(flattenExitOption(fromQueueWithShutdown(q1))),
            collectRight(flattenExitOption(fromQueueWithShutdown(q2)))
          )
        )
      } else {
        return M.dieMessage(
          `partitionEither: expected two streams but got ${A.size(queues)}`
        )
      }
    }
  )
}
/**
 * Split a stream by a predicate. The faster stream may advance by up to buffer elements further than the slower one.
 */

export function partitionEither(p, buffer = 16) {
  return (self) => partitionEither_(self, p, buffer)
}
//# sourceMappingURL=partitionEither.js.map
