"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Runtime = void 0;

var tsplus_module_1 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/TraceElement/definition"));

var tsplus_module_2 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/FiberId/operations/make"));

var tsplus_module_3 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/FiberRef/operations/currentEnvironment"));

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Tuple/make"));

var tsplus_module_5 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/List/definition"));

var tsplus_module_6 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/definition"));

var tsplus_module_7 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/ImmutableMap/make"));

var tsplus_module_8 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Stack"));

var tsplus_module_9 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/InterruptStatus/definition"));

var tsplus_module_10 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/FiberScope/definition"));

var tsplus_module_11 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Supervisor/operations/none"));

var tsplus_module_12 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Maybe/definition"));

var tsplus_module_13 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Exit/operations/flatten"));

var tsplus_module_14 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Function"));

var tsplus_module_15 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Cause/operations/squashWith"));

var _context = /*#__PURE__*/require("@effect/core/io/Fiber/_internal/context");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

const fileName_1 = "(@effect/core) _src/io/Runtime/definition.ts";

class Runtime {
  constructor(environment, runtimeConfig) {
    this.environment = environment;
    this.runtimeConfig = runtimeConfig;

    this.unsafeRunWith = (effect, k, __tsplusTrace) => {
      const fiberId = tsplus_module_2.unsafeMake(tsplus_module_1.parse(__tsplusTrace));
      const children = new Set();
      const supervisor = this.runtimeConfig.value.supervisor;
      const fiberRefLocals = tsplus_module_7.make(tsplus_module_4.make(tsplus_module_3.currentEnvironment.value, tsplus_module_5.cons(tsplus_module_4.make(fiberId, this.environment), tsplus_module_5.nil())), tsplus_module_4.make(tsplus_module_6.services.value, tsplus_module_5.cons(tsplus_module_4.make(fiberId, tsplus_module_6.liveServices.value), tsplus_module_5.nil())));
      const context = new _context.FiberContext(fiberId, children, fiberRefLocals, this.runtimeConfig, new tsplus_module_8.Stack(tsplus_module_9.Interruptible.toBoolean));
      tsplus_module_10.globalScope.value.unsafeAdd(this.runtimeConfig, context, fileName_1 + ":32:38");

      if (supervisor !== tsplus_module_11.none) {
        supervisor.unsafeOnStart(this.environment, effect, tsplus_module_12.none, context);
        context.unsafeOnDone(exit => supervisor.unsafeOnEnd(tsplus_module_13.flatten(exit), context));
      }

      context.nextEffect = effect;
      context.run();
      context.unsafeOnDone(exit => {
        k(tsplus_module_13.flatten(exit));
      });
      return id => k => this.unsafeRunAsyncWith(context._interruptAs(id), exit => k(tsplus_module_13.flatten(exit)), fileName_1 + ":46:50");
    };
    /**
     * Executes the effect asynchronously, discarding the result of execution.
     *
     * This method is effectful and should only be invoked at the edges of your
     * program.
     */


    this.unsafeRunAsync = (effect, __tsplusTrace) => {
      return this.unsafeRunAsyncWith(effect, tsplus_module_14.constVoid, fileName_1 + ":56:35");
    };
    /**
     * Executes the effect asynchronously, eventually passing the exit value to
     * the specified callback.
     *
     * This method is effectful and should only be invoked at the edges of your
     * program.
     */


    this.unsafeRunAsyncWith = (effect, k, __tsplusTrace) => {
      this.unsafeRunWith(effect, k, fileName_1 + ":71:23");
    };
    /**
     * Runs the `Effect`, returning a JavaScript `Promise` that will be resolved
     * with the value of the effect once the effect has been executed, or will be
     * rejected with the first error or exception throw by the effect.
     *
     * This method is effectful and should only be used at the edges of your
     * program.
     */


    this.unsafeRunPromise = (effect, __tsplusTrace) => {
      return new Promise((resolve, reject) => {
        this.unsafeRunAsyncWith(effect, exit => {
          switch (exit._tag) {
            case "Success":
              {
                resolve(exit.value);
                break;
              }

            case "Failure":
              {
                reject(tsplus_module_15.squashWith_(exit.cause, tsplus_module_14.identity));
                break;
              }
          }
        }, fileName_1 + ":87:30");
      });
    };
    /**
     * Runs the `Effect`, returning a JavaScript `Promise` that will be resolved
     * with the `Exit` state of the effect once the effect has been executed.
     *
     * This method is effectful and should only be used at the edges of your
     * program.
     */


    this.unsafeRunPromiseExit = (effect, __tsplusTrace) => {
      return new Promise(resolve => {
        this.unsafeRunAsyncWith(effect, exit => {
          resolve(exit);
        }, fileName_1 + ":114:30");
      });
    };
  }

}

exports.Runtime = Runtime;
//# sourceMappingURL=definition.js.map