"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
// tracing: off
const A = __importStar(require("../../Collections/Immutable/Chunk"))
const Tp = __importStar(require("../../Collections/Immutable/Tuple"))
const E = __importStar(require("../../Either"))
const Function_1 = require("../../Function")
const T = __importStar(require("../_internal/effect"))
const M = __importStar(require("../_internal/managed"))
const collectLeft_1 = require("./collectLeft")
const collectRight_1 = require("./collectRight")
const distributedWith_1 = require("./distributedWith")
const flattenExitOption_1 = require("./flattenExitOption")
const fromQueueWithShutdown_1 = require("./fromQueueWithShutdown")
const mapM_1 = require("./mapM")
/**
 * 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_(
    (0, distributedWith_1.distributedWith)(
      2,
      buffer,
      E.fold(
        () => T.succeed((_) => _ === 0),
        () => T.succeed((_) => _ === 1)
      )
    )((0, mapM_1.mapM)(p)(self)),
    (queues) => {
      const [q1, q2] = queues
      if (q1 && q2) {
        return M.succeed(
          Tp.tuple(
            (0, collectLeft_1.collectLeft)(
              (0, flattenExitOption_1.flattenExitOption)(
                (0, fromQueueWithShutdown_1.fromQueueWithShutdown)(q1)
              )
            ),
            (0, collectRight_1.collectRight)(
              (0, flattenExitOption_1.flattenExitOption)(
                (0, fromQueueWithShutdown_1.fromQueueWithShutdown)(q2)
              )
            )
          )
        )
      } else {
        return M.dieMessage(
          `partitionEither: expected two streams but got ${A.size(queues)}`
        )
      }
    }
  )
}
exports.partitionEither_ = partitionEither_
/**
 * Split a stream by a predicate. The faster stream may advance by up to buffer elements further than the slower one.
 */
function partitionEither(p, buffer = 16) {
  return (self) => partitionEither_(self, p, buffer)
}
exports.partitionEither = partitionEither
//# sourceMappingURL=partitionEither.js.map
