"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.end =
  exports.fromExit =
  exports.dieMessage =
  exports.die =
  exports.halt =
  exports.fromPull =
  exports.fromEffect =
  exports.fail =
  exports.chunk =
  exports.single =
  exports.tap =
  exports.tap_ =
  exports.map =
  exports.map_ =
  exports.isSuccess =
  exports.isFailure =
  exports.isDone =
  exports.foldM =
  exports.foldM_ =
  exports.fold =
  exports.fold_ =
  exports.done =
  exports.Take =
    void 0
const tracing_1 = __importStar(require("../../../Tracing"))
const tracing_2 = __importStar(require("../../../Tracing"))
const fileName_1 = "(@effect-ts/system): _src/Experimental/Stream/_internal/Take.ts"
const C = __importStar(require("../../../Cause"))
const A = __importStar(require("../../../Collections/Immutable/Chunk"))
const T = __importStar(require("../../../Effect"))
const commons_1 = require("../../../Effect/commons")
const Ex = __importStar(require("../../../Exit"))
const O = __importStar(require("../../../Option"))
/**
 * A `Take<E, A>` represents a single `take` from a queue modeling a stream of
 * values. A `Take` may be a failure cause `Cause<E>`, an chunk value `A`
 * or an end-of-stream marker.
 */
class Take {
  constructor(exit) {
    this.exit = exit
  }
}
exports.Take = Take
/**
 * Transforms `Take[E, A]` to `Effect[R, E, B]`.
 */
function done(self) {
  return T.done(self.exit, fileName_1 + ":25:16")
}
exports.done = done
/**
 * Folds over the failure cause, success value and end-of-stream marker to
 * yield a value.
 */
function fold_(self, end, error, value) {
  return Ex.fold_(
    self.exit,
    (_) => O.fold_(C.flipCauseOption(_), () => end, error),
    value
  )
}
exports.fold_ = fold_
/**
 * Folds over the failure cause, success value and end-of-stream marker to
 * yield a value.
 *
 * @dataFirst fold_
 */
function fold(end, error, value) {
  return (self) => fold_(self, end, error, value)
}
exports.fold = fold
/**
 * Effectful version of `Take#fold`.
 *
 * Folds over the failure cause, success value and end-of-stream marker to
 * yield an effect.
 */
function foldM_(self, end, error, value) {
  return Ex.foldM_(
    self.exit,
    (_) => O.fold_(C.flipCauseOption(_), () => end, error),
    value
  )
}
exports.foldM_ = foldM_
/**
 * Effectful version of `Take#fold`.
 *
 * Folds over the failure cause, success value and end-of-stream marker to
 * yield an effect.
 *
 * @dataFirst foldM_
 */
function foldM(end, error, value) {
  return (self) => foldM_(self, end, error, value)
}
exports.foldM = foldM
/**
 * Checks if this `take` is done (`Take.end`).
 */
function isDone(self) {
  return Ex.fold_(
    self.exit,
    (_) => O.isNone(C.flipCauseOption(_)),
    (_) => false
  )
}
exports.isDone = isDone
/**
 * Checks if this `take` is a failure.
 */
function isFailure(self) {
  return Ex.fold_(
    self.exit,
    (_) => O.isSome(C.flipCauseOption(_)),
    (_) => false
  )
}
exports.isFailure = isFailure
/**
 * Checks if this `take` is a success.
 */
function isSuccess(self) {
  return Ex.fold_(
    self.exit,
    (_) => false,
    (_) => true
  )
}
exports.isSuccess = isSuccess
/**
 * Transforms `Take<E, A>` to `Take<E, B>` by applying function `f`.
 */
function map_(self, f) {
  return new Take(Ex.map_(self.exit, A.map(f)))
}
exports.map_ = map_
/**
 * Transforms `Take<E, A>` to `Take<E, B>` by applying function `f`.
 *
 * @dataFirst map_
 */
function map(f) {
  return (self) => map_(self, f)
}
exports.map = map
/**
 * Returns an effect that effectfully "peeks" at the success of this take.
 */
function tap_(self, f) {
  return T.asUnit(Ex.forEach_(self.exit, f), fileName_1 + ":151:18")
}
exports.tap_ = tap_
/**
 * Returns an effect that effectfully "peeks" at the success of this take.
 *
 * @dataFirst tap_
 */
function tap(f) {
  return (self) => tap_(self, f)
}
exports.tap = tap
/**
 * Creates a `Take<never, A>` with a singleton chunk.
 */
function single(a) {
  return new Take(Ex.succeed(A.single(a)))
}
exports.single = single
/**
 * Creates a `Take[Nothing, A]` with the specified chunk.
 */
function chunk(as) {
  return new Take(Ex.succeed(as))
}
exports.chunk = chunk
/**
 * Creates a failing `Take<E, unknown>` with the specified failure.
 */
function fail(e) {
  return new Take(Ex.fail(O.some(e)))
}
exports.fail = fail
/**
 * Creates an effect from `Effect<R, E,A>` that does not fail, but succeeds with the `Take<E, A>`.
 * Error from stream when pulling is converted to `Take.halt`. Creates a singleton chunk.
 */
function fromEffect(effect) {
  return T.foldCause_(effect, (cause) => halt(cause), single, fileName_1 + ":189:22")
}
exports.fromEffect = fromEffect
/**
 * Creates effect from `Pull<R, E, A>` that does not fail, but succeeds with the `Take<E, A>`.
 * Error from stream when pulling is converted to `Take.halt`, end of stream to `Take.end`.
 */
function fromPull(pull) {
  return T.foldCause_(
    pull,
    (_) => O.fold_(C.flipCauseOption(_), () => exports.end, halt),
    chunk,
    fileName_1 + ":197:22"
  )
}
exports.fromPull = fromPull
/**
 * Creates a failing `Take<E, never>` with the specified cause.
 */
function halt(c) {
  return new Take(Ex.halt(C.map_(c, O.some)))
}
exports.halt = halt
/**
 * Creates a failing `Take<never, never>` with the specified throwable.
 */
function die(e) {
  return new Take(Ex.die(e))
}
exports.die = die
/**
 * Creates a failing `Take<never, never>` with the specified error message.
 */
function dieMessage(msg) {
  return new Take(Ex.die(new C.RuntimeError(msg)))
}
exports.dieMessage = dieMessage
/**
 * Creates a `Take<E, A>` from `Exit<E, A>`.
 */
function fromExit(exit) {
  return new Take(Ex.map_(Ex.mapError_(exit, O.some), A.single))
}
exports.fromExit = fromExit
/**
 * End-of-stream marker
 */
exports.end = new Take(Ex.fail(O.none))
//# sourceMappingURL=Take.js.map
