import * as tracing_1 from "../../../Tracing"
const fileName_1 = "(@effect-ts/system): _src/Experimental/Stream/_internal/Take.ts"
import * as C from "../../../Cause"
import * as A from "../../../Collections/Immutable/Chunk"
import * as T from "../../../Effect"
import { _A, _E } from "../../../Effect/commons"
import * as Ex from "../../../Exit"
import * as O from "../../../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.
 */

export class Take {
  constructor(exit) {
    this.exit = exit
  }
}
/**
 * Transforms `Take[E, A]` to `Effect[R, E, B]`.
 */

export function done(self) {
  return T.done(self.exit, fileName_1 + ":25:16")
}
/**
 * Folds over the failure cause, success value and end-of-stream marker to
 * yield a value.
 */

export function fold_(self, end, error, value) {
  return Ex.fold_(
    self.exit,
    (_) => O.fold_(C.flipCauseOption(_), () => end, error),
    value
  )
}
/**
 * Folds over the failure cause, success value and end-of-stream marker to
 * yield a value.
 *
 * @dataFirst fold_
 */

export function fold(end, error, value) {
  return (self) => fold_(self, end, error, value)
}
/**
 * Effectful version of `Take#fold`.
 *
 * Folds over the failure cause, success value and end-of-stream marker to
 * yield an effect.
 */

export function foldM_(self, end, error, value) {
  return Ex.foldM_(
    self.exit,
    (_) => O.fold_(C.flipCauseOption(_), () => end, error),
    value
  )
}
/**
 * Effectful version of `Take#fold`.
 *
 * Folds over the failure cause, success value and end-of-stream marker to
 * yield an effect.
 *
 * @dataFirst foldM_
 */

export function foldM(end, error, value) {
  return (self) => foldM_(self, end, error, value)
}
/**
 * Checks if this `take` is done (`Take.end`).
 */

export function isDone(self) {
  return Ex.fold_(
    self.exit,
    (_) => O.isNone(C.flipCauseOption(_)),
    (_) => false
  )
}
/**
 * Checks if this `take` is a failure.
 */

export function isFailure(self) {
  return Ex.fold_(
    self.exit,
    (_) => O.isSome(C.flipCauseOption(_)),
    (_) => false
  )
}
/**
 * Checks if this `take` is a success.
 */

export function isSuccess(self) {
  return Ex.fold_(
    self.exit,
    (_) => false,
    (_) => true
  )
}
/**
 * Transforms `Take<E, A>` to `Take<E, B>` by applying function `f`.
 */

export function map_(self, f) {
  return new Take(Ex.map_(self.exit, A.map(f)))
}
/**
 * Transforms `Take<E, A>` to `Take<E, B>` by applying function `f`.
 *
 * @dataFirst map_
 */

export function map(f) {
  return (self) => map_(self, f)
}
/**
 * Returns an effect that effectfully "peeks" at the success of this take.
 */

export function tap_(self, f) {
  return T.asUnit(Ex.forEach_(self.exit, f), fileName_1 + ":151:18")
}
/**
 * Returns an effect that effectfully "peeks" at the success of this take.
 *
 * @dataFirst tap_
 */

export function tap(f) {
  return (self) => tap_(self, f)
}
/**
 * Creates a `Take<never, A>` with a singleton chunk.
 */

export function single(a) {
  return new Take(Ex.succeed(A.single(a)))
}
/**
 * Creates a `Take[Nothing, A]` with the specified chunk.
 */

export function chunk(as) {
  return new Take(Ex.succeed(as))
}
/**
 * Creates a failing `Take<E, unknown>` with the specified failure.
 */

export function fail(e) {
  return new Take(Ex.fail(O.some(e)))
}
/**
 * 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.
 */

export function fromEffect(effect) {
  return T.foldCause_(effect, (cause) => halt(cause), single, fileName_1 + ":189:22")
}
/**
 * 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`.
 */

export function fromPull(pull) {
  return T.foldCause_(
    pull,
    (_) => O.fold_(C.flipCauseOption(_), () => end, halt),
    chunk,
    fileName_1 + ":197:22"
  )
}
/**
 * Creates a failing `Take<E, never>` with the specified cause.
 */

export function halt(c) {
  return new Take(Ex.halt(C.map_(c, O.some)))
}
/**
 * Creates a failing `Take<never, never>` with the specified throwable.
 */

export function die(e) {
  return new Take(Ex.die(e))
}
/**
 * Creates a failing `Take<never, never>` with the specified error message.
 */

export function dieMessage(msg) {
  return new Take(Ex.die(new C.RuntimeError(msg)))
}
/**
 * Creates a `Take<E, A>` from `Exit<E, A>`.
 */

export function fromExit(exit) {
  return new Take(Ex.map_(Ex.mapError_(exit, O.some), A.single))
}
/**
 * End-of-stream marker
 */

export const end = /*#__PURE__*/ new Take(/*#__PURE__*/ Ex.fail(O.none))
//# sourceMappingURL=Take.js.map
