"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.checkPlatform =
  exports.yieldNow =
  exports.unitTraced =
  exports.unit =
  exports.tryOrElse =
  exports.tryOrElse_ =
  exports.tryCatchSuspend =
  exports.suspend =
  exports.supervised_ =
  exports.supervised =
  exports.succeed =
  exports.result =
  exports.provideAll_ =
  exports.provideAll =
  exports.tracingStatus_ =
  exports.tracingStatus =
  exports.interruptStatus_ =
  exports.interruptStatus =
  exports.haltWith =
  exports.halt =
  exports.forkReport_ =
  exports.forkReport =
  exports.fork =
  exports.foldCauseM_ =
  exports.foldCauseM =
  exports.succeedWith =
  exports.try =
  exports.tryCatch =
  exports.effectAsyncOptionBlockingOn =
  exports.effectAsyncOption =
  exports.traced =
  exports.untraced =
  exports.checkTraced =
  exports.trace =
  exports.checkInterruptible =
  exports.descriptorWith =
  exports.chain_ =
  exports.chain =
  exports.accessM =
  exports.access =
    void 0
const core_1 = require("../Cause/core")
const Exit = __importStar(require("../Exit/core"))
const Function_1 = require("../Function")
const O = __importStar(require("../Option"))
const primitives_1 = require("./primitives")
/**
 * Effectfully accesses the environment of the effect.
 */
function access(f, __trace) {
  return new primitives_1.IRead((_) => new primitives_1.ISucceed(f(_)), __trace)
}
exports.access = access
/**
 * Effectfully accesses the environment of the effect.
 */
function accessM(f, __trace) {
  return new primitives_1.IRead(f, __trace)
}
exports.accessM = accessM
/**
 * Returns an effect that models the execution of this effect, followed by
 * the passing of its value to the specified continuation function `f`,
 * followed by the effect that it returns.
 *
 * @dataFirst chain_
 */
function chain(f, __trace) {
  return (val) => new primitives_1.IFlatMap(val, f, __trace)
}
exports.chain = chain
/**
 * Returns an effect that models the execution of this effect, followed by
 * the passing of its value to the specified continuation function `f`,
 * followed by the effect that it returns.
 */
function chain_(val, f, __trace) {
  return new primitives_1.IFlatMap(val, f, __trace)
}
exports.chain_ = chain_
/**
 * Constructs an effect based on information about the current fiber, such as
 * its identity.
 */
function descriptorWith(f, __trace) {
  return new primitives_1.IDescriptor(f, __trace)
}
exports.descriptorWith = descriptorWith
/**
 * Checks the interrupt status, and produces the effect returned by the
 * specified callback.
 */
function checkInterruptible(f, __trace) {
  return new primitives_1.ICheckInterrupt(f, __trace)
}
exports.checkInterruptible = checkInterruptible
/**
 * Capture trace at the current point
 */
exports.trace = new primitives_1.ITrace()
/**
 * Checks the tracing status, and produces the effect returned by the
 * specified callback.
 */
function checkTraced(f) {
  return new primitives_1.ICheckTracingStatus(f)
}
exports.checkTraced = checkTraced
/**
 * Disables Effect tracing facilities for the duration of the effect.
 *
 * Note: Effect tracing is cached, as such after the first iteration
 * it has a negligible effect on performance of hot-spots (Additional
 * hash map lookup per flatMap). As such, using `untraced` sections
 * is not guaranteed to result in a noticeable performance increase.
 */
function untraced(self) {
  return new primitives_1.ITracingStatus(self, false)
}
exports.untraced = untraced
/**
 * Enables Effect tracing for this effect. Because this is the default, this
 * operation only has an additional meaning if the effect is located within
 * an `untraced` section, or the current fiber has been spawned by a parent
 * inside an `untraced` section.
 */
function traced(self) {
  return new primitives_1.ITracingStatus(self, true)
}
exports.traced = traced
/**
 * Imports an asynchronous effect into a pure `Effect` value, possibly returning
 * the value synchronously.
 *
 * If the register function returns a value synchronously, then the callback
 * function `AsyncRE<R, E, A> => void` must not be called. Otherwise the callback
 * function must be called at most once.
 *
 * The list of fibers, that may complete the async callback, is used to
 * provide better diagnostics.
 */
function effectAsyncOption(register, __trace) {
  return new primitives_1.IEffectAsync(register, [], __trace)
}
exports.effectAsyncOption = effectAsyncOption
/**
 * Imports an asynchronous effect into a pure `Effect` value, possibly returning
 * the value synchronously.
 *
 * If the register function returns a value synchronously, then the callback
 * function `AsyncRE<R, E, A> => void` must not be called. Otherwise the callback
 * function must be called at most once.
 *
 * The list of fibers, that may complete the async callback, is used to
 * provide better diagnostics.
 */
function effectAsyncOptionBlockingOn(register, blockingOn, __trace) {
  return new primitives_1.IEffectAsync(register, blockingOn, __trace)
}
exports.effectAsyncOptionBlockingOn = effectAsyncOptionBlockingOn
/**
 * Imports a synchronous side-effect into a pure value, translating any
 * thrown exceptions into typed failed effects creating with `halt`.
 */
function tryCatch(effect, onThrow, __trace) {
  return new primitives_1.IEffectPartial(effect, onThrow, __trace)
}
exports.tryCatch = tryCatch
/**
 * Imports a synchronous side-effect into a pure value, translating any
 * thrown exceptions into typed failed effects creating with `halt`.
 */
function try_(effect, __trace) {
  return new primitives_1.IEffectPartial(effect, Function_1.identity, __trace)
}
exports.try = try_
/**
 * Imports a synchronous side-effect into a pure value
 */
function succeedWith(effect, __trace) {
  return new primitives_1.IEffectTotal(effect, __trace)
}
exports.succeedWith = succeedWith
/**
 * A more powerful version of `foldM` that allows recovering from any kind of failure except interruptions.
 *
 * @dataFirst foldCauseM_
 */
function foldCauseM(failure, success, __trace) {
  return (value) => new primitives_1.IFold(value, failure, success, __trace)
}
exports.foldCauseM = foldCauseM
/**
 * A more powerful version of `foldM` that allows recovering from any kind of failure except interruptions.
 */
function foldCauseM_(value, failure, success, __trace) {
  return new primitives_1.IFold(value, failure, success, __trace)
}
exports.foldCauseM_ = foldCauseM_
/**
 * Returns an effect that forks this effect into its own separate fiber,
 * returning the fiber immediately, without waiting for it to begin
 * executing the effect.
 *
 * The returned fiber can be used to interrupt the forked fiber, await its
 * result, or join the fiber. See `Fiber` for more information.
 *
 * The fiber is forked with interrupt supervision mode, meaning that when the
 * fiber that forks the child exits, the child will be interrupted.
 */
function fork(value, __trace) {
  return new primitives_1.IFork(value, O.none, O.none, __trace)
}
exports.fork = fork
/**
 * Returns an effect that forks this effect into its own separate fiber,
 * returning the fiber immediately, without waiting for it to begin
 * executing the effect.
 *
 * The returned fiber can be used to interrupt the forked fiber, await its
 * result, or join the fiber. See `Fiber` for more information.
 *
 * The fiber is forked with interrupt supervision mode, meaning that when the
 * fiber that forks the child exits, the child will be interrupted.
 *
 * @dataFirst forkReport_
 */
function forkReport(reportFailure, __trace) {
  return (value) =>
    new primitives_1.IFork(value, O.none, O.some(reportFailure), __trace)
}
exports.forkReport = forkReport
/**
 * Returns an effect that forks this effect into its own separate fiber,
 * returning the fiber immediately, without waiting for it to begin
 * executing the effect.
 *
 * The returned fiber can be used to interrupt the forked fiber, await its
 * result, or join the fiber. See `Fiber` for more information.
 *
 * The fiber is forked with interrupt supervision mode, meaning that when the
 * fiber that forks the child exits, the child will be interrupted.
 */
function forkReport_(value, reportFailure, __trace) {
  return new primitives_1.IFork(value, O.none, O.some(reportFailure), __trace)
}
exports.forkReport_ = forkReport_
/**
 * Returns an effect that models failure with the specified `Cause`.
 */
function halt(cause, __trace) {
  return new primitives_1.IFail(() => cause, __trace)
}
exports.halt = halt
/**
 * Returns an effect that models failure with the specified `Cause`.
 *
 * This version takes in a lazily-evaluated trace that can be attached to the `Cause`
 * via `Cause.Traced`.
 */
function haltWith(cause, __trace) {
  return new primitives_1.IFail(cause, __trace)
}
exports.haltWith = haltWith
/**
 * Switches the interrupt status for this effect. If `true` is used, then the
 * effect becomes interruptible (the default), while if `false` is used, then
 * the effect becomes uninterruptible. These changes are compositional, so
 * they only affect regions of the effect.
 *
 * @dataFirst interruptStatus_
 */
function interruptStatus(flag, __trace) {
  return (effect) => new primitives_1.IInterruptStatus(effect, flag, __trace)
}
exports.interruptStatus = interruptStatus
/**
 * Switches the interrupt status for this effect. If `true` is used, then the
 * effect becomes interruptible (the default), while if `false` is used, then
 * the effect becomes uninterruptible. These changes are compositional, so
 * they only affect regions of the effect.
 */
function interruptStatus_(effect, flag, __trace) {
  return new primitives_1.IInterruptStatus(effect, flag, __trace)
}
exports.interruptStatus_ = interruptStatus_
/**
 * Toggles Effect tracing support for this effect. If `true` is used, then the
 * effect will accumulate traces, while if `false` is used, then tracing
 * is disabled. These changes are compositional, so they only affect regions
 * of the effect.
 *
 * @dataFirst tracingStatus_
 */
function tracingStatus(flag) {
  return (effect) => new primitives_1.ITracingStatus(effect, flag)
}
exports.tracingStatus = tracingStatus
/**
 * Toggles Effect tracing support for this effect. If `true` is used, then the
 * effect will accumulate traces, while if `false` is used, then tracing
 * is disabled. These changes are compositional, so they only affect regions
 * of the effect.
 */
function tracingStatus_(effect, flag) {
  return new primitives_1.ITracingStatus(effect, flag)
}
exports.tracingStatus_ = tracingStatus_
/**
 * Provides the `Effect` effect with its required environment, which eliminates
 * its dependency on `R`.
 *
 * @dataFirst provideAll_
 */
function provideAll(r, __trace) {
  return (next) => new primitives_1.IProvide(r, next, __trace)
}
exports.provideAll = provideAll
/**
 * Provides the `Effect` effect with its required environment, which eliminates
 * its dependency on `R`.
 */
function provideAll_(next, r, __trace) {
  return new primitives_1.IProvide(r, next, __trace)
}
exports.provideAll_ = provideAll_
/**
 * Returns an effect that semantically runs the effect on a fiber,
 * producing an `Exit` for the completion value of the fiber.
 */
function result(value, __trace) {
  return new primitives_1.IFold(
    value,
    (cause) => succeed(Exit.halt(cause)),
    (succ) => succeed(Exit.succeed(succ)),
    __trace
  )
}
exports.result = result
/**
 * Lift a pure value into an effect
 */
function succeed(a, __trace) {
  return new primitives_1.ISucceed(a, __trace)
}
exports.succeed = succeed
/**
 * Returns an effect with the behavior of this one, but where all child
 * fibers forked in the effect are reported to the specified supervisor.
 *
 * @dataFirst supervised_
 */
function supervised(supervisor, __trace) {
  return (fa) => new primitives_1.ISupervise(fa, supervisor, __trace)
}
exports.supervised = supervised
/**
 * Returns an effect with the behavior of this one, but where all child
 * fibers forked in the effect are reported to the specified supervisor.
 */
function supervised_(fa, supervisor, __trace) {
  return new primitives_1.ISupervise(fa, supervisor, __trace)
}
exports.supervised_ = supervised_
/**
 * Returns a lazily constructed effect, whose construction may itself require effects.
 * When no environment is required (i.e., when R == unknown) it is conceptually equivalent to `flatten(succeedWith(io))`.
 */
function suspend(factory, __trace) {
  return new primitives_1.ISuspend(factory, __trace)
}
exports.suspend = suspend
/**
 * Returns a lazily constructed effect, whose construction may itself require effects.
 * When no environment is required (i.e., when R == unknown) it is conceptually equivalent to `flatten(tryCatch(orThrow, io))`.
 */
function tryCatchSuspend(factory, onThrow, __trace) {
  return new primitives_1.ISuspendPartial(factory, onThrow, __trace)
}
exports.tryCatchSuspend = tryCatchSuspend
/**
 * Executed `that` in case `self` fails with a `Cause` that doesn't contain defects,
 * executes `success` in case of successes
 */
function tryOrElse_(self, that, success, __trace) {
  return new primitives_1.IFold(
    self,
    (cause) => O.fold_((0, core_1.keepDefects)(cause), that, halt),
    success,
    __trace
  )
}
exports.tryOrElse_ = tryOrElse_
/**
 * Executed `that` in case `self` fails with a `Cause` that doesn't contain defects,
 * executes `success` in case of successes
 *
 * @dataFirst tryOrElse_
 */
function tryOrElse(that, success, __trace) {
  return (self) => tryOrElse_(self, that, success, __trace)
}
exports.tryOrElse = tryOrElse
/**
 * Returns the effect resulting from mapping the success of this effect to unit.
 */
exports.unit = new primitives_1.ISucceed(undefined)
/**
 * Returns the effect resulting from mapping the success of this effect to unit.
 */
const unitTraced = (__trace) => new primitives_1.ISucceed(undefined, __trace)
exports.unitTraced = unitTraced
/**
 * Returns an effect that yields to the runtime system, starting on a fresh
 * stack. Manual use of this method can improve fairness, at the cost of
 * overhead.
 */
exports.yieldNow = new primitives_1.IYield()
/**
 * Checks the current platform
 */
function checkPlatform(f, __trace) {
  return new primitives_1.IPlatform(f, __trace)
}
exports.checkPlatform = checkPlatform
//# sourceMappingURL=core.js.map
