"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.unsafeMainProvider = exports.MainProvider = void 0
const tracing_1 = __importStar(require("../Tracing"))
const tracing_2 = __importStar(require("../Tracing"))
const fileName_1 = "(@effect-ts/system): _src/Layer/unsafe.ts"
const Tp = __importStar(require("../Collections/Immutable/Tuple"))
const Ex = __importStar(require("../Exit"))
const F = __importStar(require("../Fiber"))
const ReleaseMap_1 = require("../Managed/ReleaseMap")
const P = __importStar(require("../Promise"))
const Ref = __importStar(require("../Ref"))
const L = __importStar(require("./core"))
const T = __importStar(require("./deps-effect"))
class MainProvider {
  constructor(allocate, release, provide) {
    this.allocate = allocate
    this.release = release
    this.provide = provide
  }
}
exports.MainProvider = MainProvider
/**
 * Unsafely returns a `MainProvider` to be used in frontend-like
 * contexts where initialization needs to be global and sync
 */
function unsafeMainProvider(self) {
  const promise = P.unsafeMake(F.None)
  const relMap = new ReleaseMap_1.ReleaseMap(
    Ref.unsafeMakeRef(new ReleaseMap_1.Running(0, new Map()))
  )
  return new MainProvider(
    T.foldCauseM_(
      T.map_(
        T.provideSome_(
          L.build(self["+++"](L.identity())).effect,
          (r) => Tp.tuple(r, relMap),
          fileName_1 + ":32:21"
        ),
        (_) => _.get(1),
        fileName_1 + ":31:11"
      ),
      (cause) =>
        T.chain_(
          P.halt_(promise, cause),
          () => T.halt(cause, fileName_1 + ":39:70"),
          fileName_1 + ":39:57"
        ),
      (r) => P.succeed(r)(promise),
      fileName_1 + ":38:19"
    ),
    T.descriptorWith(
      (d) =>
        T.asUnit(
          (0, ReleaseMap_1.releaseAll)(
            Ex.interrupt(d.id),
            T.sequential,
            fileName_1 + ":44:17"
          )(relMap),
          fileName_1 + ":44:74"
        ),
      fileName_1 + ":43:21"
    ),
    (self) =>
      T.chain_(
        P.await(promise),
        (env) => T.provide(env, fileName_1 + ":46:60")(self),
        fileName_1 + ":46:23"
      )
  )
}
exports.unsafeMainProvider = unsafeMainProvider
//# sourceMappingURL=unsafe.js.map
